PDA

View Full Version : Display the value of a resistance to LCD



joseph Degorio
- 12th November 2007, 07:24
Hi everyone,
I'm new PBP, have a 5k ohm potentiometer, and I'm trying to dispaly it to the lcd. But I wa hoping I can display it in terms in 0% - 100%. Please help me with my codes below.

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
PortB = $00 ' all outputs off to start
TrisB = %00000000 ' All of Port B is outputs
TRISA = %11111111 ' Set PORTA to all input
adval var byte ' ADC result variable
ADCON1 = %00000001 ' Set PORTA analog

loop:
ADCIN 0, adval ' Store ADC value to adval

Serout 4, 6, [254, 1]
pause 2
serout 4, 6, [#adval] 'display to the LCD
pause 250
Goto loop ' Do it forever
End



Thanks in advance,
joe

HenrikOlsson
- 12th November 2007, 11:27
Hi,
A quick'n'easy way could be if you changed your ADC resolution from 8 to 10 bits and then:

DEFINE ADC_BITS 10
ADVAL VAR WORD '<----Don't forget to change this to a WORD

loop:
ADCIN 0, adval 'Store ADC value to adval
ADVAL = ADVAL * 10 / 102 'ADVAL is now between 0-100
Serout 4, 6, [254, 1]
pause 2
serout 4, 6, [#adval] 'display to the LCD
pause 250
Goto loop 'Do it forever
End


/Henrik Olsson.

joseph Degorio
- 13th November 2007, 10:49
Hello Henrik,
Thanks for the reply, I tried you code, but if I turn the pot just a little to the right it gives me 663. Please see my modified code below...

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 'Set sampling time in uS
'
'
adval var word 'Create adval to store result
'
'
Init:
PortB = $00 ' all outputs off to start
TrisB = %00000000 ' All of Port B is outputs
'

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000001 ' Set PORTA analog
loop:
ADCIN 0, adval ' Read channel 0 to adval
Pause 500
Serout 4, 6, [$1b, $2a,$80] 'Turn backlight half broghtness
Serout 4, 6, [254, 1]
pause 2
adval = adval*10/102
serout 4, 6, [#adval]
'********** Drive LEDs *************************
LEDtst1:
if adval > 25 then tst2 'If A/D value is less than 10
portb = %00000001 ' then light LED0 only

tst2:
if adval > 75 then tst3 'If A/D value is between 25 and 75
portb = %00000011
goto cont 'continue with the program
tst3:
if adval > 99 then
portb = %00000111
goto cont 'continue with the program

ENdif
cont:
Pause 100 'wait 1 second
goto loop
end


Thanks in advance,
joe

HenrikOlsson
- 13th November 2007, 13:03
Hi Joe,
I'm not sure but try changing the left/right justified setting on the ADC. Your not saying which PIC you're using but on the 16F688 for example, you set that with bit0 of ADCON0 though it may be different on other PIC's - see the datasheet for your PIC.

Let me know how it goes.

/Henrik.

joseph Degorio
- 14th November 2007, 12:27
Hi Henrik,
I'm using 16F876A, I did some trial and error and finally got it. But I use this formula..
Adval=Adval*100/255 'gives the range 0-100
But I used Define ADC_BITS 8.

Thanks for your guidance.
joe