adcin reading


Closed Thread
Results 1 to 3 of 3

Thread: adcin reading

  1. #1
    Join Date
    Jun 2008
    Posts
    30

    Default adcin reading

    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

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    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...
    Code:
    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

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mitchf14 View Post
    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,
    DT

Similar Threads

  1. ADCIN and PIC18F4331 Arghhhhh !
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th December 2010, 20:27
  2. ADCIN Problem
    By JavPar in forum General
    Replies: 33
    Last Post: - 20th January 2009, 03:36
  3. Adcin or Pot
    By lerameur in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 11th January 2007, 16:46
  4. ADCIN - AD settings are wrong
    By teverett in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th December 2006, 16:32
  5. help using adcin
    By harryweb in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2006, 07:46

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts