PDA

View Full Version : 10bit A/d input



jrudd
- 4th March 2012, 04:34
I'm trying re-learn what I thought I understood 3 yrs ago.
I have a 16F877A with the A/D set-up for 10 Bits. Outputing the found value to an lcd.
For my 0-5v input, I have to do FoundValue=DEC adval0/64 in order for the 0-5vdc to equate to FoundValue of 0-1023 on the lcd.
Otherwise the DEC value goes up in increments of 64 to thirty some thousand.
I assume I have to use this FoundValue/64 in my calculations.
Thanks

Acetronics2
- 4th March 2012, 08:32
Hi,

Probably you just forgot the conversion result has to be RIGHT justified ...

Alain

jrudd
- 4th March 2012, 13:56
Thanks but it was right justified. I've added the code below. Sorry, I've forgotten how to use a code window and can't find the thread instructions.

'* This sample program is supplied courtesy of microEngineering Labs Inc *
'************************************************* ****************************

' PicBasic Pro program to display result of
' 10-bit A/D conversion on LCD
' Connect analog input to channel-0 (RA0)

' Define LCD registers and bits
@ DEVICE HS_OSC

DEFINE OSC 20
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTD
Define LCD_RSBIT 2
Define LCD_EREG PORTE
Define LCD_EBIT 1
define LCD_RWREG PORTD
define LCD_RWBIT 3

' Define ADCIN parameters
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
duty var word
'************************************************* ***************
'Set up (2) Channel Analog input
TRISD=0
TRISE=0
TRISA = %00000001 ' Set PORTA.0 to input,ALL THE REST OUTPUTS
ADCON1 =%00101110 'Set PORTA analog and right justify result
PORTE.2=0 ' Turn off leds so you can use the lcd display
PORTD.3=0 ' LCD R/W line low (W)
Pause 500 ' Wait .5 second
LCDOUT 254,1
PAUSE 500
'************************************************* *****************
'Set up PWM
PR2=9
TRISC.2=0 'Set CCP1 to Output
CCP1CON=%00001100 'SET CCP1 TO PWM
T2CON=%00000100 'SET TIMER2 TO "ON";PRESCALE=1

duty=5


loop:
ADCIN 0, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval/64 ' Display the decimal value

Pause 100 ' Wait .1 second

Goto loop ' Do it forever
End

Acetronics2
- 4th March 2012, 17:46
you really sure ???



ADCON1 =%00101110 'Set PORTA analog and right justify result


I' not .... :p

Alain

Acetronics2
- 4th March 2012, 17:55
Post editor somewhat " Rock and Roll " ... Heeeeellllp, Lester :livid:





REGISTER 11-2: ADCON1 REGISTER (ADDRESS 9Fh)
bit 7 ADFM: A/D Result Format Select bit
1 = Right justified. Six (6) Most Significant bits of ADRESH are read as ‘0’.0 = Left justified. Six (6) Least Significant bits of ADRESL are read as

jrudd
- 5th March 2012, 01:49
Ace, thanks, I've been using a Microchip A/D addendum manual I downloaded years ago and I guess it sort of worked because I never noticed severe problems. (http://ww1.microchip.com/downloads/e...Doc/31023a.pdf (http://ww1.microchip.com/downloads/en/DeviceDoc/31023a.pdf))
I'm going to play with the ADCON1 settings shown in the proper data sheet for the 16F877 this evening.

surfies
- 11th August 2012, 16:36
Hi,
Ace can you please explain right and left justified to me in plain English, what is the difference? What do I get back from a ADCIN PORTA.0, VAR, what is in
that VAR now, if you have a 10K pot with 5V on, into PORTA.0 ??

Thanks
Willie

HenrikOlsson
- 11th August 2012, 17:16
Hi,
If your reference voltage is 5V and you're feeding in 5V on the analog input the ADC will return the value 1023. Since a 10 bit value need two bytes (ADRESH and ADRESL) to "fit" you can choose if the result should be left or right justified within those two bytes containing the result.


ADRESH ADRESL
00000011 11111111 <-This is right justified, result is "pushed toward the right" within the two bytes
11111111 11000000 <-This is left justified, result is "pushed towards the left" within the two bytes

The red digits represnts the most significant bit in the result

If you read the result by combining the high and low byte into a word when the result is left justified you'll get 65572 instead 1023.

/Henrik.

surfies
- 12th August 2012, 08:45
Thanks a lot Henrik, I think I understand it a bit better now.

Willie