Jim2214
Newbie level 2
I'm trying to monitor the 12v battery powering my circuit. I've pieced together enough code that it works as intended with the exception that my reading is just over half of what it should be. I'm using a 12v wal-wart and with circuit running I measure 11.54v with a multimeter.
Reading the lcd in circuit I get 6.39v. After the voltage divider and at pin 4 (RA2) I measure 3.70v. I've searched for many hours trying to correct this but came up empty. The snippets below are just what appears to be related for the sake of space. It's so close to being right I'm thinking the answer is simple for someone that knows what they're looking at. Thanks for your time to read.
Using Hi-Tech C MPLAB.
Reading the lcd in circuit I get 6.39v. After the voltage divider and at pin 4 (RA2) I measure 3.70v. I've searched for many hours trying to correct this but came up empty. The snippets below are just what appears to be related for the sake of space. It's so close to being right I'm thinking the answer is simple for someone that knows what they're looking at. Thanks for your time to read.
Using Hi-Tech C MPLAB.
Code:
############## MAIN #################
void
init_TMR1() {
tmr1H2 = 256-15;
TMR1IF = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
T1CON = TMR1_SET;
//=============== INITIALIze the PORTS I/O
init_system(void){
PORTA = 0;
PORTB = 0;
PORTC &= 0xC0;
PORTD = 0;
PORTE = 0;
ADCON0 = 0x85;
ADCON1 = 0b10000010;
TRISA = TRISACONF;
TRISB = TRISBCONF;
TRISC = TRISCCONF;
TRISD = TRISDCONF;
TRISE = TRISECONF;
OPTION = 0b00000000;
//========= CONFIGURE THE INTERRUPTS ==========//
PIE1 = 0b00100001;
PIE2 = 0b00000000;
RCIF = 0;
INTCON = 0b01000000;
//=========== Conversion ANA 12V =================
adc_read(ANA_12V, &ana_12v); // monitor +12V (battery)
lcd_clear();
lcd_puts("12V=");
lcd_uint(ana_12v, 4);
################ lcd.c ###################
//========================================================
// ANA Mode 8 bits (justify)
// returns the voltage supply 12V
//========================================================
unsigned char
ana12(void){
unsigned char volt;
ADGO = 1;
while(ADGO);
volt = ADRESH + (ADRESH>>3);
return volt;
}
//================= value ANA Mode 10bits=============
void
adc_read(unsigned char channel, unsigned int* val)
{
ADCON0 = (channel << 3) + 0x81;
ADGO = 1;
while(ADGO);
*val = (unsigned int)(ADRESH<<8) + ADRESL;
}
################### port.h ###################
#include <htc.h>
#include <pic.h>
#include <string.h>
#define FOSC 7372800L // 7.3728MHz
################## lcd.h ##################
//==================================================
unsigned char ana12(void);
//=================================================
// ANA Mode 10bits
//===================================================
void adc_read(unsigned char channel, unsigned int* val);