我把程序贴出,请各位大侠帮忙看看:
#include <pic.h>
__CONFIG(XT&WDTDIS&LVPDIS);
#define RSPIN RE0
#define RWPIN RE1
#define EPIN RE2
#define D_0 RA5
#define D_1 RB1
#define D_2 RB2
#define D_3 RB3
#define D_4 RB4
#define D_5 RB5
#define D_6 RB6
#define D_7 RB7
#define sce RD5
#define rst RD4
#define dc RC4
#define sdin RC5
#define sclk RC3
#define uchar unsigned char
#define uint unsigned int
uchar disbuf;
uchar shutab[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x00};
//延时
void delayus(uchar us)
{
while(--us){asm("NOP");}
}
void delay(void)
{
uchar d=100;
while(--d);
}
void delayms(uchar ms)
{
while(--ms){delay();}
}
//ADC
void adinit(void)
{
ADCS0=0;
ADCS1=1;
PCFG0=0;
PCFG1=0;
PCFG2=1;
PCFG3=0;
ADIE=0;
ADFM=0;
}
//ADC转换
uchar adc_read(uchar channel)
{
uchar adbuf;
ADCON0=(channel<<3)+0x81; //0000 1000
asm("CLRWDT");
delayus(4);
ADGO=1;
while(ADGO);
//adbuf=ADRESH*256+ADRESL;
adbuf=ADRESH;
return adbuf;
}
//算术平均值
uchar Filter(void)
{
uchar fval[32];
int ret=0;
uchar i;
for(i=0;i<32;i++)
{
fval[i]=adc_read(0);
}
for(i=0;i<32;i++)
{
ret+=fval[i];
}
ret=ret/32;
return (uchar)ret;
}
// 写入数据
void write_bus1(unsigned char buf)
{
if(buf&0x01) D_0=1;
else D_0=0;
if(buf&0x02) D_1=1;
else D_1=0;
if(buf&0x04) D_2=1;
else D_2=0;
if(buf&0x08) D_3=1;
else D_3=0;
if(buf&0x10) D_4=1;
else D_4=0;
if(buf&0x20) D_5=1;
else D_5=0;
if(buf&0x40) D_6=1;
else D_6=0;
if(buf&0x80) D_7=1;
else D_7=0;
}
//显示屏忙检测
void lcdbusy(void)
{
TRISB7=1;
RSPIN=0;
RWPIN=1;
EPIN=1;
while(D_7==1);
EPIN=0;
TRISB7=0;
}
//写入指令
void lcdwcom(uchar combuf)
{
RSPIN=0;
RWPIN=0;
write_bus1(combuf);
EPIN=1;
delayus(4);
EPIN=0;
}
//写数据
void lcdwdata(uchar databuf)
{
RSPIN=1;
RWPIN=0;
write_bus1(databuf);
EPIN=1;
delayus(4);
EPIN=0;
}
//数据显示地址
void lcdaddress(uchar x,uchar y)
{
x&=0x0f;
y&=0x01;
if(y==0x00)
{
lcdbusy();
lcdwcom(x|0x80);
}
else
{
lcdbusy();
lcdwcom((x+0x40)|0x80);
}
}
//显示一个字符
void lcdwchar(uchar x,uchar y,uchar databuf)
{
lcdaddress(x,y);
lcdbusy();
lcdwdata(databuf);
}
//初始化显示屏
void lcdreset(void)
{
delayms(15);
lcdwcom(0x38);
delayms(5);
lcdwcom(0x38);
delayms(5);
lcdwcom(0x38);
lcdbusy();
lcdwcom(0x38);
lcdbusy();
lcdwcom(0x08);
lcdbusy();
lcdwcom(0x01);
lcdbusy();
lcdwcom(0x06);
lcdbusy();
lcdwcom(0x0c);
}
//系统初始化
void sysinit(void)
{
asm("CLRWDT");
OPTION=0x81;
TRISA=0x00;
TRISB=0x00;
TRISC=0x00;
TRISD=0x00;
TRISE=0x00;
RD2=0;
RD3=0;
RA1=0;
RA2=0;
RA3=0;
RBIE=0;
}
//主函数
void main(void)
{
sysinit();
adinit();
lcdreset();
lcdwchar(0,0,0x53);
lcdwchar(1,0,0x45);
lcdwchar(2,0,0x54);
lcdwchar(3,0,0x3a);
while(1)
{
asm("CLRWDT");
disbuf=Filter();
delayus(4);
lcdwchar(4,0,(disbuf/100)+0x30);
lcdwchar(5,0,0x2e);
lcdwchar(6,0,(disbuf%100)/10+0x30);
lcdwchar(7,0,disbuf%10+0x30);
lcdwchar(8,0,0x20);
lcdwchar(9,0,0x76);
}
} |