PDA

View Full Version : adcin reading



mitchf14
- 29th June 2008, 05:22
Hi guys I'm trying to read the voltage from a 12 v battery i have a perfect 3:1 voltage divider but i can't seem to get any accurate readings i think something is wrong with my formulas and i can't seem to grasp it. here is what i have so far if anyone can help.


TRISA = %11111111
TRISB = %00000000
TRISC = %00000000


DEFINE OSC 20

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 4 'LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 1 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 0 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 0 ' Set clock source
Define ADC_SAMPLEUS 50' Set sampling time in uS
ADCON1 = 142

bat_volts var word

Start:


ADCIN porta.0, bat_volts
bat_volts = (bat_volts */500)>> 2


LCDout $FE, 1 , " Voltage In"
LCDout $FE, $C0, " ", dec(bat_volts/100 *3),".",dec1 bat_volts, " V"

pause 200

goto start

end

skimask
- 29th June 2008, 06:53
For starters...forget the formulas for just a little bit...look at raw numbers.
Do the raw numbers make sense?
For instance, if your Vref is 5v, and the Vin at the analog port is 2.5v, is the raw number 512 (for a 10 bit input) or 128 (for an 8 bit input) ?
If those numbers make sense, then work on the formulas. If they don't, well, then you've probably got something else goofy going on...ports not set up right, TRIS not set right, input circuit not set up right...something...
Some of the changes are marked in bold...some aren't...


DEFINE OSC 20
DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 4 'LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 1 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 0 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 0 ' Set clock source
Define ADC_SAMPLEUS 50' Set sampling time in uS
trisa=$ff:trisb=0:trisc=0:adcon1=142:bat_volts var word:raw_in var word
Start: ADCIN porta.0, raw_in
bat_volts = (raw_in */ 375)
LCDout $FE, 1 , "Raw = ", DEC5 raw_in
LCDout $FE, $C0, "Volts=", DEC2 (bat_volts/100),".",dec2 (bat_volts//100, " V"
pause 200:goto start
end

Darrel Taylor
- 29th June 2008, 09:10
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 0 ' Set clock source
Define ADC_SAMPLEUS 50' Set sampling time in uS

With ADC_CLOCK 0 (2TOSC), the maximum device frequency is 1.25 Mhz

With a 20 Mhz OSC, you need to use ADC_CLOCK 2, 6 or 3.

2 is the fastest (32TOSC).

hth,