Vinay Hebbar
Newbie level 4
- Joined
- Apr 25, 2013
- Messages
- 7
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- 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;
}
#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;
}