I'm using an ADC pin to measure 9v battery voltage.
I have a 4x voltagedivider so the battery never exceeds 2.5v -> ADC pin
I use a vref+ external reference of 2.5V reference diode.
All ADC values are too high ie 9.6v for an 8.6v battery.
Using mikroc library or my own function doesnt change result .
See code below.
I have a 4x voltagedivider so the battery never exceeds 2.5v -> ADC pin
I use a vref+ external reference of 2.5V reference diode.
All ADC values are too high ie 9.6v for an 8.6v battery.
Using mikroc library or my own function doesnt change result .
See code below.
Code:
unsigned myreadADC(unsigned short channel)
{
char local;
local = channel << 3; // Adjust for correct offset
ADCON0=local; // Set required channel
ADCON0.ADON = 1; // Enable ADC
delay_us(50); // Wait for cap to charge
ADCON0.GO_DONE = 1; // Start conversion
while(ADCON0.GO_DONE == 1); // Wait here until complete
ADCON0.ADON = 0; // Disable ADC
return ADRESL | (ADRESH<<8); // Return result
}
void main(void)
{
char txt[8];
unsigned ADval;
unsigned result;
INTCON = 0; // Interrupts are off (Soft I2C)
OPTION_REG = 0b00000000; // Pullups on by Latch
PORTB = 0b00000000; // All OFF
TRISB = 0b00000000; // All outputs
PORTA = 0b00000000; // All OFF
TRISA = 0b00001011; // All outputs except A0 & A1 & A3
CMCON = 7; // comparator is Off
CVRCON = 0; // Comparator refs are off
ANSEL = 0b00001011; // AN3=Vref+ AN1,AN0 are Analog
OSCCON = 0b01101000; // 4 Mhz / INTOSC / INTRC
ADCON0 = 0b01000001; // Fosc/8 & ADON=1
ADCON1 = 0b10100000; // Configure Vref+ = on Vref-=Gnd ADC2=0
//ADC_Init();
Lcd_Init(); // Initialize LCD on PORTB
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn off cursor
Lcd_Cmd(_LCD_CLEAR);
// Lcd_Out(1,1,"2500m 23.0v 9.6v"); // Show on LCD
Delay_ms(5000);
while(1)
{
// ADval=ADC_Read(CURRENT);
// result=( (long) 2500 * ADval)/1023;
// IntToStr(result, txt);
// Lcd_Out(1,1,txt); // Show Current
ADval=ADC_Get_Sample(BATTERY);
result=( (long) 10000 * ADval)/1023;
IntToStr(ADval, txt);
Lcd_Out(1,1,txt); // Show Currentin 10bit
ADval=myreadADC(BATTERY);
IntToStr(ADval, txt);
Lcd_Out(2,1,txt); // Show Current
Delay_ms(250);
}
}