PDA

View Full Version : Interesting result on a 16F688 with ADC and LCD



wdmagic
- 24th April 2013, 09:51
Ive got some code I converted from a 18F4550 thermostat, want to use it on a 16F688, on running the RA4 port never goes high, and the LCD displays a block on row1 about location 14 on a 4x20 LCD. adjusting the pot on RA0 does nothing, ive tried messing with ADCON0 & 1, and ANSEL, but im doing something wrong.
MCP9700A on RA1
POT on RA0
RA4 has LED


include "ADC_Default.bas"
DEFINE LCD_DREG PORTC ' LCD Data bits on PORTC
DEFINE LCD_DBIT 2 ' PORTC starting address
DEFINE LCD_RSREG PORTC ' LCD RS bit on PORTC
DEFINE LCD_RSBIT 0 ' LCD RS bit address
DEFINE LCD_EREG PORTC ' LCD E bit on PORTC
DEFINE LCD_EBIT 1 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 4 ' LCD has 4 rows
ANSEL = %00000011
TRISA = %00000011
TRISC = 0 ' PORTC is output

LCDOUT $FE, 1 ' Clear LCD
PAUSE 500 ' Wait 0.5sec for LCD to initialize
SETT Var Word ' A/D converter result in 10bit
READT var word
AGAIN:
ADCIN 0, READT ' Read Channel 0 data
ADCIN 1, SETT ' Read Channel 0 data
ReadT = ReadT - 6500
SETT = SETT / 1024 'Output = 0 - 63
readT = ((((readT / 10) * 9) / 5) / 13) + 32 'Convert ANA0 to Farent.
setT = sett + 40 ' Temp Range will be 40 - 103
LCDOUT $FE, 2
LCDOUT $FE, $80
LCDOUT "Set Temp = ", DEC setT , $DF , "F "
LCDOUT $FE, $C0
LCDOUT "Temp = ", DEC READT , $DF , "F "
if SETT < (readt - 1) then PORTA.4 = 0
if sett > (readt + 1) then PORTA.4 = 1
pause 100
GOTO AGAIN ' Repeat
END

pedja089
- 24th April 2013, 11:22
Add 1K pull up resistor.

Dave
- 24th April 2013, 12:47
Hey Chris, Read the data sheet.... RA4 is open collector only when used as output..... Read the data sheet... It's your friend....

HenrikOlsson
- 24th April 2013, 17:17
Hi Dave,
That was actually my initial thought as well but what I did was what you suggest (but perhaps didn't do ?) and read the datasheet before posting - which resulted in me not posting that response ;-)
There's no mentioning of RA4 being open drain on the 16F688 as is the case on some other devices - at least not what I could find.

/Henrik.

wdmagic
- 24th April 2013, 19:23
Yep I looked at it last night. I might need to add a resistor. On page 33 though it says it has weak internal pullups unless its an output pin. However that doesnt solve the LCD issue?
I tried removeing all the ADC and output pin code, and just leave it barebones with the LCD code and LCD still doesnt work, weird.

I am not sure what it is as the code is pretty simple.


DEFINE LCD_DREG PORTC ' LCD Data bits on PORTD
DEFINE LCD_DBIT 2 ' PORTD starting address
DEFINE LCD_RSREG PORTC ' LCD RS bit on PORTD
DEFINE LCD_RSBIT 0 ' LCD RS bit address
DEFINE LCD_EREG PORTC ' LCD E bit on PORTD
DEFINE LCD_EBIT 1 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 4 ' LCD has 4 rows

'TRISA = %00000011
TRISC = 0 ' PORTC is output

LCDOUT $FE, 1 ' Clear LCD
PAUSE 500 ' Wait 0.5sec for LCD to initialize

AGAIN:
LCDOUT $FE, 2
LCDOUT $FE, $80
LCDOUT "Set Temp = "
LCDOUT $FE, $C0
LCDOUT "Temp = "
pause 100
GOTO AGAIN ' Repeat

END
Problem is its a removable PIC, LCD is tied to board with a premade cable so I cant change pins from LCD to PORTC

Henrik, I saw the chart on page

Demon
- 24th April 2013, 19:31
You removed ADC code, , but is ADC enabled on PortC for this PIC?

Robert

wdmagic
- 24th April 2013, 19:34
dang, I forgot C is adc ports too. Ive got an appointment I cant miss. Ill be back in a few hours. what command is it to turn adc off on portc. oh and since these are 6 bit registers do I use %000000 or %00000000 ? when doing settings?

wdmagic
- 24th April 2013, 19:37
PIC - LCD Pins
C0 - 4
C1 - 6
C3-6 - 11-14

Demon
- 24th April 2013, 19:37
Datasheet tells you what register to set.

I'd use 8 bits, no surprises.

Robert

Demon
- 24th April 2013, 19:39
PIC - LCD Pins
C0 - 4
C1 - 6
C3-6 - 11-14

Start pin is 2 or 3?

wdmagic
- 24th April 2013, 22:50
DEFINE LCD_DREG PORTC ' LCD Data bits on PORTC
DEFINE LCD_DBIT 2 ' PORTC starting address
DEFINE LCD_RSREG PORTC ' LCD RS bit on PORTC
DEFINE LCD_RSBIT 0 ' LCD RS bit address
DEFINE LCD_EREG PORTC ' LCD E bit on PORTC
DEFINE LCD_EBIT 1 ' LCD E bit address

Sorry I was in a hurry to leave, starting bit is 2.

(C2-5 - 11-14) corrected

andywpg
- 24th April 2013, 23:26
i
DEFINE LCD_DREG PORTC ' LCD Data bits on PORTC
DEFINE LCD_DBIT 2 ' PORTC starting address


The DBIT might be your problem - It has to be either 0 or 4 - you have to have your data lines as pins 0-3 or 4-7.

From the manual:

"If an 8-bit bus is used, all 8 bits must be on one port. If a 4-bit bus is used,
the top 4 LCD data bits must be connected to either the bottom 4 or top 4 bits of
one port. Enable and Register Select may be connected to any port pin. R/W may
be tied to ground if the LCDIN command is not used."

wdmagic
- 24th April 2013, 23:43
Ahhhh. ok, well dang, these are hardwired, I will have to move pins in the plug...
I figured thats why we could define a starting address was it started out on the one stated and counted up from there.

thanks andywpg

wdmagic
- 25th April 2013, 00:27
OK I now have something on LCD but its jibberish. I moved chip and lcd to breadboard for time being, PORTC 0-3 is now starting bits, RS is C.4 and E is C.5, nothing else is hooked up except LCD. I will need to turn on ADC for PORTA 0 & 1 after LCD starts working.

heres the code, notes are beside respective lines


DEFINE LCD_DREG PORTC ' LCD Data bits on PORTD
DEFINE LCD_DBIT 0 ' PORTD starting address
DEFINE LCD_RSREG PORTC ' LCD RS bit on PORTD
DEFINE LCD_RSBIT 4 ' LCD RS bit address
DEFINE LCD_EREG PORTC ' LCD E bit on PORTD
DEFINE LCD_EBIT 5 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 4 ' LCD has 4 rows
ADCON0 = 0 ' Tried it with and without this line
ANSEL = 0 ' Tried it with and without this line
TRISA = %00000011 ' Left this cause it shouldnt hurt LCD
TRISC = %00000000

LCDOUT $FE, 1 ' Clear LCD
PAUSE 500 ' Wait 0.5sec for LCD to initialize

AGAIN:
LCDOUT $FE, 2
LCDOUT $FE, $80
LCDOUT "This is a Test"
pause 1000
GOTO AGAIN ' Repeat

Theres not much here to go wrong, anyone see anything?

Demon
- 25th April 2013, 00:37
Pause 1000 before 1st LCDOUT to start.

andywpg
- 25th April 2013, 02:04
OK I now have something on LCD but its jibberish. I moved chip and lcd to breadboard for time being, PORTC 0-3 is now starting bits, RS is C.4 and E is C.5, nothing else is hooked up except LCD. I will need to turn on ADC for PORTA 0 & 1 after LCD starts working.

I had trouble without the following two defines (after the ones you already have):



' Set command delay time in us
DEFINE LCD_COMMANDUS 1500
' Set data delay time in us
DEFINE LCD_DATAUS 44


Then try this:



PAUSE 500 ' Wait 0.5sec for LCD to initialize (May need to be one second, depends on your LCD)
DO
LCDOUT $FE, 1, "This is a Test"
Pause 1000
LCDOUT $FE, 1 'clear the screen
Pause 250 'wait 1/4 second
Loop


No reason the above shouldn't work. Good luck

andywpg
- 25th April 2013, 02:26
OK I now have something on LCD but its jibberish. I moved chip and lcd to breadboard for time being, PORTC 0-3 is now starting bits, RS is C.4 and E is C.5, nothing else is hooked up except LCD. I will need to turn on ADC for PORTA 0 & 1 after LCD starts working.


This may sound like a dumb question, but it IS a Hitachi 44780-comapatable display, right?

Just a thought......

wdmagic
- 25th April 2013, 02:28
Agg i posted a reply but it didnt take.

Yes Demon, that was the problem, I copied and pasted backwards, I changed the pins in the plug so it does work now on the premade board. a second problem I found was the premade board had a 7805 on it and I had tied the input of that to 5volts, it barely lit the screen and did not turn on the PIC, been stressing alot here lately, lots going on. but I have learned a ton this week. so the thermostat works, thanks everyone

wdmagic
- 25th April 2013, 02:29
yes andywpg, it is, I posted earlier but aprently the page didnt save it. its working now.