Maker Pro
Maker Pro

ADC Reading Voltage on PIC16f1847

sideburn

Jun 14, 2013
75
Joined
Jun 14, 2013
Messages
75
Hey guys,

I am a total newb when it comes to reading analog inputs so I was hoping to get some help here.

My goal is to monitor the battery level being supplied to my circuit and then send out an alert when the level gets low.

What I've got so far is a POT connected between VSS and RB1 and I am printing out the value through the USART to a terminal app on my laptop.

With a fresh battery at 3.7v, I can turn the 500k POT and get an output ranging from around 8 to 240..When I pull the pin to GND I get a 0 and when I pull it up to VCC I get 255. All seems good so far..

But then when I connect a nearly dead battery (around 3.2 volts) I am getting hte same readings..

Confused... What am I missing here?

Code:
ANSELB  = 0x20; //select RB1 as input (AN11)
while(1){
 ADCON0      = 0x2F; // start ADC watching AN11
 while (ADCON0 == 0x2F);// wait for conversion to complete
 DisplayVolt =  ADRESH; //store ADC input value
 sprintf(outputBuffer, "battery: %d;\r\n", DisplayVolt);
 WriteString(outputBuffer); //result is same regardless of battery level
}
 

p.erasmus

Jul 18, 2013
60
Joined
Jul 18, 2013
Messages
60
In my opinion to get the correct AD conversion result you should read both ADRESL and ADRESH as the AD module returs a 10 bit value and from these 2 register you should make 1 value
10bits number which range between 0 and 1023 to have the number in Voltage you should Convert the ADC bits to Voltage


unsigned int myADC = 0;
myADC = ((ARESH<<8)+ ADRESL);

I did not check how you configured the AD module registers but you should check it
 
Last edited:

sideburn

Jun 14, 2013
75
Joined
Jun 14, 2013
Messages
75
Thanks,

I wanted to do that but didnt know how exactly..

I am now thinking the problem is related to the voltage reference being that same source as the input.

I am experimenting with the fixed voltage reference now (FVR).

EDIT: Thanks that helped.. Now, when I set ADCON0 = 0xFF; so that it uses the FVR and don't connect anything to any pins, i am getting a value off ADRESH and ADRESL of around 19000 - 20000 on a dead battery and around 16000 - 17000 on a good battery.

I dont know what I am doing and how you are supposed to use the FVR but this is working!
I can just check when the level gets above 19000 I know my battery is low..
 
Last edited:

p.erasmus

Jul 18, 2013
60
Joined
Jul 18, 2013
Messages
60
This is also possible another thing I would check for the DONE bit to check if the conversion is done
These values sound not correct however I never used these 16F pics ,I need to check the datasheet to beable to help you more
I will try to fined some time to read the datasheet about this
 
Last edited:

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
What are you using for the reference voltage for the ADC? If you are using the battery, you will always get a max reading when reading the battery voltage, no matter what it is. To measure battery voltage you need an independent voltage reference. If the chip has an internal 2.0V reference, you could use that. The use a divider to bring the battery voltage into the range of 0 to 2V, and you will get a reading on the actual battery voltage.

Edited: Yes, the chip has an internal 2.048 voltage reference. Use it as the ADC reference voltage.

Bob
 
Last edited:
Top