ADC on PIC showing all 1's

Discussion in 'Embedded Systems and Microcontrollers' started by Guinness1759, Jan 17, 2011.

  1. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Hi,
    All 10 bits from my ADC are showing all 1's when I watch the ADRESL and ADRESH registers using the debug mode. Here is the code:

    Code ( (Unknown Language)):
    1.  
    2.                 TRISA1 = 1;  //Tempterature Sensor Port is Input
    3.  
    4.                  //ADCONO Set to AN1 (RA1)
    5.         CHS0 = 1;
    6.         CHS1 = 0;
    7.         CHS2 = 0;
    8.         CHS3 = 0;
    9.         CHS4 = 0;
    10.  
    11.                 ADON = 1;     // ADC is enabled
    12.  
    13.         ADPREF0 = 1;  // Use Fixed Voltage ...
    14.         ADPREF1 = 1;  // Reference Module
    15.  
    16.         ADFM = 1;  // Right Justified
    17.  
    18.         ADGO = 1;     // Start ADC conversion cycle
    19.         while(ADGO)
    20.             continue;   // wait for conversion complete
    21.  
    22.         templ = ADRESL;
    23.         temph = ADRESH;
    24.  
    Any ideas?
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,018


    I don't see where you are setting TRIS or ANSEL.
     
    Guinness1759 likes this.
  3. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Yes, I just added that when I realized it was further up in the code. It's updated to what is in my program now, and still is not working.
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Also post your config words setting . Then you ask questions. They are an important part part of PIC programming
     
    Guinness1759 likes this.
  5. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Here is the entire block of code:

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h> //HI-TECH C header file
    3.  
    4. __CONFIG(WDTE_ON & CPD_OFF & PWRTE_OFF);
    5. /* Watch Dog Off,  Code Protection Off, */
    6. /* & PWRTE_OFF & IESO_ON & FOSC_XT & MCLRE_ON & LVP_OFF*/
    7.  
    8. volatile unsigned char  temph;
    9. volatile unsigned char  templ;
    10.  
    11.  
    12.  
    13. //********************** CONSTANTS, VARIABLES ********************************
    14. #define _XTAL_FREQ 400000 /*Define the Chrystal frequency for the
    15. //Hi-TECH C __delay_ms() function in Hz used in the pause function below */
    16.  
    17.  
    18. //********************** MAIN FUNCTION **************************************
    19. int main()
    20. {
    21.    
    22.     TRISB = 0x00;   // Make Port B output
    23.     PORTB = 0x00;   // All port B pins off
    24.     TRISA1 = 1;  //Tempterature Sensor Port is Input
    25.     ANSA1 = 1;
    26.    
    27.    
    28.    
    29.     while (1)
    30.     {
    31.    
    32.         RB4 = 1;    // Sets PORT B BIT 0 high (+5V)
    33.         __delay_ms(1500);   // waits for 500mS
    34.    
    35.         RB4 = 0;
    36.         __delay_ms(1500);
    37.  
    38.  
    39.         //Temperature Sensor
    40.  
    41.         ADON = 1;     // Turn ADC On
    42.  
    43.         //ADCONO Set to AN1 (RA1)
    44.         CHS0 = 1;
    45.         CHS1 = 0;
    46.         CHS2 = 0;
    47.         CHS3 = 0;
    48.         CHS4 = 0;
    49.        
    50.  
    51.  
    52.        
    53.  
    54.         ADPREF0 = 1;  // Use Fixed Voltage ...
    55.         ADPREF1 = 1;  // Reference Module
    56.  
    57.         ADFM = 1;  // Right Justified
    58.  
    59.         ADGO = 1;     // Start ADC conversion cycle
    60.         while(ADGO)
    61.             continue;   // wait for conversion complete
    62.  
    63.         templ = ADRESL;
    64.         temph = ADRESH;
    65.  
    66.  
    67.         ADON = 0; // Turn ADC Off
    68.  
    69.        
    70.  
    71.     }
    72. }
    73.  
    74.  
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Eh....Not meant to be picky. But wich PIC do you use in this case
     
    Guinness1759 likes this.
  7. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    pic16f1827
     
  8. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Update: I'm able to get it to read all 0's or all 1's if I hook it up to the power rails. So this leads me to believe that the pin is not setup for analog input, but I set ANSA1 = 1 in the code?
     
  9. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    64
    0
    Update: I took out the two lines that set it to use a fixed voltage reference and now it's working fine. I still need to get it to work with a fixed voltage reference, so if any of you have any ideas let me know. :D
     
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What are you setting the fixed reference to? Vdd, or a fraction thereof?

    I don't recall the exact options on the 16F627/8 right now, but you can usually choose various fractions in that register.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,018

    I think he is setting is to an external reference. That is something I have not done yet.

    Are you sure the external reference is working?
     
Loading...