Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F877A ADC Battery Monitor

Status
Not open for further replies.

Jim2214

Newbie level 2
Joined
Sep 12, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
24
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.
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);
 

Hi,

A bit of description would be helpful.

*******
volt = ADRESH + (ADRESH>>3);
What do you want this to do?
In my eyes: volt = ADRESH x 1.125
(With the error of lost bits and no rounding)

Klaus
 

Just an example:
Code:
float SupplyVoltage (void)
{
	return (float)ADC_GenericRead(ADC1, SUPPLY_VOLTAGE_CH) * GetIntVDD() * ((R1 + R2) / R2) / 4096;
}
Function returns voltage as float variable.
ADC_GenericRead - function that return RAW value of adc in bits.
GetIntVDD - supply voltage of your mcu in volts (3.3 or 5.0 volts). In my case I can measure it internally.
R1 and R2 is a devider values. (10k and 1k in my case).
4096 is an ADC resolution. 1024 for you (10bit) or 256 (8bit).
 

Off topic slightly and I hope you have thought of this already, but make sure that you have a large enough capacitor after the regulator feeding the MCU so that it stays at a working level long enough to notice the main PSU drop and complete whatever tasks it needs to before it will, itself, stop.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top