Hello everybody,
I'am trying to use the ADC for reading the voltage of the battery.
But with some cards it's working, and with other cards it's not working.
for this I use a function find in a project of TI(but I don't remember its name):
#define HAL_ADC_REF_125V 0x00 /* Internal 1.25V Reference */
#define HAL_ADC_DEC_064 0x00 /* Decimate by 64 : 8-bit resolution */
#define HAL_ADC_DEC_128 0x10 /* Decimate by 128 : 10-bit resolution */
#define HAL_ADC_DEC_512 0x30 /* Decimate by 512 : 14-bit resolution */
#define HAL_ADC_CHN_VDD3 0x0f /* Input channel: VDD/3 */
#define HAL_ADC_CHN_TEMP 0x0e /* Temperature sensor */
uint16 Get_Val_Battery(void)
{
uint16 value_bat;
/* Clear ADC interrupt flag */
ADCIF = 0;
ADCCON3 = (HAL_ADC_REF_125V| HAL_ADC_DEC_128 | HAL_ADC_CHN_VDD3);
/* Wait for the conversion to finish */
while ( !ADCIF );
/* Get the result */
value_bat = ADCL;
value_bat |= ((uint16) ADCH) << 8;
i++;
/*
* value now contains measurement of Vdd/3
* 0 indicates 0V and 32767 indicates 1.25V
* voltage = (value*3*1.25)/32767 volts
* we will multiply by this by 10 to allow units of 0.1 volts
*/
value_bat = value_bat >> 6; // divide first by 2^6
value_bat = (uint16)(value_bat * 37.5);
value_bat = value_bat >> 9; // ...and later by 2^9...to prevent overflow during multiplication
return value_bat;
}
*The ref is the internal ref, so 1.25V (or 1.15V, I'am not certain)
*the resolution is 9Bits
*I'am measuring the VDD/3
With this function the value_bat is more or less 32170 before the calculation of the voltage. My battery is 3V.
Do you have an idea?
thank you!