ADC problem with PIC16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by MaxD, May 5, 2012.

  1. MaxD

    Thread Starter New Member

    May 5, 2012
    11
    0
    ADCON0:
    bit7 - ADCS1
    bit6 - ADCS0
    bit5 - CHS2
    bit4 - CHS1
    bit3 - CHS0
    bit2 - GO/DONE
    bit1 - unimplemented bit
    bit0 - ADON
    ----------------------------------------------------------
    ADCON1:
    bit7 - ADFM
    bit6 - unimplemented bit
    bit5 - unimplemented bit
    bit4 - unimplemented bit
    bit3 - PCFG3
    bit2 - PCFG2
    bit1 - PCFG1
    bit0 - PCFG0
    ---------------------------------------------------------
    According to the registers above, I set RA3 as Vref+ and RA2 as analog input:
    Code ( (Unknown Language)):
    1. TRISA = 0b11111;
    2. TRISE = 0b111;
    3.  
    4. ADCON0 = 0b10010001;
    5. ADCON1 = 0b10000001;
    6.  
    7. __delay_ms(2);
    8.    ADGO=1;
    9.    while(ADGO==1)
    10.       continue;
    11.    return(256*ADRESH+ADRESL);
    I can't get any power supply from RA3, why? If there is no power supply from RA3, then why LCD didn't show 0 value? Any problem with my register setting?
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    I think
    Code ( (Unknown Language)):
    1. TRISA = 0b11111;
    Sets RA0-RA4 as input. To get RA3 as output, try this:

    Code ( (Unknown Language)):
    1. TRISA = 0b11110111;
    I might be wrong...
     
  3. whatsthatsmell

    Active Member

    Oct 9, 2009
    102
    4
    Vref+ is an input for an external voltage reference if you want the ADC to use a reference voltage less than VDD of the uc.

    This is equivalent to (256 * ADRESH) + ADRESL. Is that what you want?
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In a microcontroller world. You do want to avoid doing math unless needed. And in this case it would be much more efficient to use an union like this. The game is not to write code that looks compact on the paper. But use the instruction set of your PIC in the best way.
    Code ( (Unknown Language)):
    1. union ad_res {
    2.  unsigned int adc;
    3.  unsigned char ad_bytes[2];
    4. }ad_data;
    5. main(void)
    6. {
    7.   unsigned int data;
    8.   unsigned char lowByte, highByte;  
    9.   data = 301;
    10.   ad_data.adc = data;
    11.   highByte = ad_data.ad_bytes[1];
    12.   lowByte = ad_data.ad_bytes[0];  
    13.  
    14.   //or.
    15.   HighByte = 0xf1;
    16.   LowByte = 0x01;
    17.   ad_data.ad_bytes[1] = highByte;
    18.   ad_data.ad_bytes[0] = lowByte;
    19.   data = ad_data.adc;
    20. }
    perhaps a more practical style
    Code ( (Unknown Language)):
    1. typedef union
    2.    {
    3.    unsigned short u16;
    4.    unsigned char u8[2];
    5.    } U16_U8;
    6.  
    7.  U16_U8 ptr;
    8.  // Do something to set the variable ptr
    9. ptr.u16 = ?;
    10. ptr.u8[0]=ADRESL;
    11. ptr.u8[1]=ADRESH;
    12.  
     
    absf likes this.
Loading...