help:ADC programming with 16F877A

Status
Not open for further replies.

Anson

New Member
hi guys, i am trying to measure an ac voltage and disply the value on the LCD. while the LCD keeps on show me"Voltage=000". anybody can come and save me. here is my programme.


#include<pic.h>
__CONFIG(0x1832);

union adres
{int y1;
unsigned char adre[2];
}adresult;

unsigned int ADC=0;
void adinitial_voltage(void);
const char TABLE[]={'0','1','2','3','4','5','6','7','8','9'};

#define rs RA1
#define rw RA2
#define e RA3
const char voltage[ ]={'V','O','L','T','A','G','E','='};
unsigned char A=0,B=0,C=0;
void init();
void lcd_init();
void lcd_dis();
void write_voltage();
void write(char x);
void lcd_enable();
void delay();

void main()
{
unsigned char a;
init();
while(1)
{
adinitial_voltage();
lcd_init();
for(a=10;a>=1;a--)
{
PORTD=0X80;
lcd_enable();
write_voltage();
PORTD=TABLE[C];
lcd_dis();
PORTD=TABLE;
lcd_dis();
PORTD=TABLE[A];
lcd_dis();
}
}
}


void init()
{
ADCON1=0X0;
TRISA=0B00000001;
TRISD=0X00;
}

void lcd_init()
{
PORTD=0X1;
lcd_enable();
PORTD=0X38;
lcd_enable();
PORTD=0X0c;
lcd_enable();
PORTD=0X06;
lcd_enable();

}


void write_voltage()
{
unsigned char i;
for(i=0;i<=7;i++)
{
write(voltage);
}
}

void write(char x)
{
PORTD=x;
lcd_dis();
}

void lcd_enable()
{
rs=0;
rw=0;
e=0;
delay();
e=1;
}
void lcd_dis()
{
rs=1;
rw=0;
e=0;
delay();
e=1;
}


void delay()
{
int i;
for(i=0;i<50;i++);
}

void adinitial_voltage(void)
{
unsigned char AD_data[18];
unsigned int i=0,temp=0;
for(i=0;i<=18;++i)
{
ADCON0=0B01000001;
ADCON1=0B11101110;
ADGO=1;
while(ADGO);
adresult.adre[0]=ADRESL;
adresult.adre[1]=ADRESH;
AD_data=adresult.y1;
}
unsigned char AD_max=AD_data[0];
unsigned char AD_min=AD_data[0];
ADC=0;
for(i=0;i<18;i++)
{
if(AD_data>AD_max)
{
AD_max=AD_data;
}
else if(AD_data<AD_min)
{
AD_min=AD_data;
}
ADC+=AD_data;
}
ADC=ADC-AD_max-AD_min;
ADC=ADC/16;
C=ADC%1000/100;
B=ADC%100/10;
A=ADC%10;
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…