ADC on PIC showing all 1's

Thread Starter

Guinness1759

Joined Dec 10, 2010
64
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:

Rich (BB code):
                TRISA1 = 1;  //Tempterature Sensor Port is Input

                 //ADCONO Set to AN1 (RA1)
		CHS0 = 1;
		CHS1 = 0;
		CHS2 = 0;
		CHS3 = 0;
		CHS4 = 0;

                ADON = 1;     // ADC is enabled

		ADPREF0 = 1;  // Use Fixed Voltage ...
		ADPREF1 = 1;  // Reference Module

		ADFM = 1;  // Right Justified

		ADGO = 1;     // Start ADC conversion cycle
		while(ADGO)
			continue;	// wait for conversion complete

		templ = ADRESL;
		temph = ADRESH;
Any ideas?
 

spinnaker

Joined Oct 29, 2009
7,830
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:

Rich (BB code):
                ADON = 1;     // ADC is enabled

        ADPREF0 = 1;  // Use Fixed Voltage ...
        ADPREF1 = 1;  // Reference Module

        ADFM = 1;  // Right Justified

        ADGO = 1;     // Start ADC conversion cycle
        while(ADGO)
            continue;    // wait for conversion complete

        templ = ADRESL;
        temph = ADRESH;
Any ideas?


I don't see where you are setting TRIS or ANSEL.
 

Thread Starter

Guinness1759

Joined Dec 10, 2010
64
Also post your config words setting . Then you ask questions. They are an important part part of PIC programming
Here is the entire block of code:

Rich (BB code):
#include <pic.h> //HI-TECH C header file

__CONFIG(WDTE_ON & CPD_OFF & PWRTE_OFF);
/* Watch Dog Off,  Code Protection Off, */
/* & PWRTE_OFF & IESO_ON & FOSC_XT & MCLRE_ON & LVP_OFF*/

volatile unsigned char	temph;
volatile unsigned char	templ;



//********************** CONSTANTS, VARIABLES ********************************
#define _XTAL_FREQ 400000 /*Define the Chrystal frequency for the 
//Hi-TECH C __delay_ms() function in Hz used in the pause function below */


//********************** MAIN FUNCTION **************************************
int main()
{
	
    TRISB = 0x00;	// Make Port B output
    PORTB = 0x00;	// All port B pins off
    TRISA1 = 1;  //Tempterature Sensor Port is Input
    ANSA1 = 1;
    
    
    
	while (1)
	{
	
		RB4 = 1;	// Sets PORT B BIT 0 high (+5V)
		__delay_ms(1500);	// waits for 500mS 
	
		RB4 = 0;
		__delay_ms(1500);


        //Temperature Sensor

        ADON = 1;     // Turn ADC On

        //ADCONO Set to AN1 (RA1)
		CHS0 = 1;
		CHS1 = 0;
		CHS2 = 0;
		CHS3 = 0;
		CHS4 = 0;
		


		

		ADPREF0 = 1;  // Use Fixed Voltage ...
		ADPREF1 = 1;  // Reference Module

		ADFM = 1;  // Right Justified

		ADGO = 1;     // Start ADC conversion cycle
		while(ADGO)
			continue;	// wait for conversion complete

		templ = ADRESL;
		temph = ADRESH;


		ADON = 0; // Turn ADC Off

		

	}
}
 

Thread Starter

Guinness1759

Joined Dec 10, 2010
64
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?
 

Thread Starter

Guinness1759

Joined Dec 10, 2010
64
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
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.

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?
 
Top