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.

ADC codes to blink 4 LEDS to indicate the percentage of battery

Status
Not open for further replies.

Vinay Hebbar

Newbie level 4
Newbie level 4
Joined
Apr 25, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,340
The main objective of the program is to take an analog input from the battery having max voltage of 4.7v and min of 2.75v. when the percentage of battery is below 25%, LED blinks and when its 25 or greater, it will glow and the same principle goes for 50%, 75% and 100%. For 25% i have configured RB3 and 50%, RB2, 75%, RB1 and for 100%, RB0 in PIC16F877A. Following is the code i burnt into the microcontroller. Firstly i need to know is the header files included are enough. Secondly, kindly state whats the use of configuration bits and what i have to declare in the program. And lastly, any error in the program, please state as soon as possible. I'm using MPLAB v 8.89 and to burn hex file I'm using PICkit2 v 2.61. Kindly analyse and report the problems. And building it fetched no errors. The problem is while interfacing. The following is the code i have written:


#include<pic.h>
void delay(void);
#define adc25 PORTBbits.RB3
#define adc50 PORTBbits.RB2
#define adc75 PORTBbits.RB1
#define adc100 PORTBbits.RB0
void main(void)
{
unsigned char i,j;
TRISAbits.TRISA0=0;
TRISBbits.TRISB0=0;
TRISBbits.TRISB1=0;
TRISBbits.TRISB2=0;
TRISBbits.TRISB3=0;
ADCON0 = 0x81;
ADCON1 = 0x8E;
while(1)
{
delay();
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
for(i=0;i<5;i++)
{
delay();
}
while(ADRESH==0x02)
{
if(ADRESL>0x34 && ADRESL<0x7E)
{
adc25=1;
for(i=0;i<10;i++)
{
delay();
}
adc25=0;
for(j=0;j<10;j++)
{
delay();
}
}
if(ADRESL>=0x7E)
{
adc25=1;
}
if(ADRESL>0x81 && ADRESL<0xc8)
{
adc50=1;
for(i=0;i<10;i++)
{
delay();
}
adc50=1;
for(j=0;j<10;j++)
{
delay();
}
}
if(ADRESL>=0xc8)
{
adc50=1;
}
if(ADRESL>0xcB && ADRESL<0xFF)
{
adc75=1;
for(i=0;i<10;i++)
{
delay();
}
adc75=0;
for(j=0;j<10;j++)
{
delay();
}
}
}
while(ADRESH==0x03)
{
if(ADRESL>0x00 && ADRESL<0x12)
{
adc75=1;
for(i=0;i<10;i++)
{
delay();
}
adc75=0;
for(j=0;j<10;j++)
{
delay();
}
}
if(ADRESL>=0x12)
{
adc75=1;
}
if(ADRESL>0x15 && ADRESL<0x5D)
{
adc100=1;
for(i=0;i<10;i++)
{
delay();
}
adc100=0;
for(j=0;j<10;j++)
{
delay();
}
}
if(ADRESL==0x5D)
{
adc100=1;
}
}
}
}

void delay()
{
T1CON=0x24;
TMR1H=0xF4;
TMR1L=0x24;
T1CONbits.TMR1ON=1;
while(PIR1bits.TMR1IF==0);
T1CONbits.TMR1ON=0;
PIR1bits.TMR1IF=0;
}
 

See the section void delay. I have configured 16 bit timer 1 for delay. and with that im indirectly generating a square pulse of duty cycle 50% with Ton=Toff=500ms
 

Dude if u turn on led for 500 ms and if u turn off for 500 ms, what its gonna be? Square wave. I used that logic
 

My problem is when i connect it to my developement board, im not getting the result. While burning through pic kit 2, it asks for configuration bits i have no clue if its a must to set that. Or i first wanna know if there is any problem in the program. Not compilation. The other errors. So i want u people if there is any problem with the code so that i can rely peacefully in correcting the software part.
 

Please tell what configuration i have to add for my program to work. And why is it that essential?
 

ADCON0 and ADCON1 need to be within while(1) loop. And i hadn't considered the possibility from 58% to 61% or so which i corrected. And hardware implementation i had some problem as its not indicating. Proteus simulation is perfect for the new code. I will still test and see if i have missed other possibilities
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top