#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit rs=P1^0;//H数据,L命令
sbit lcden=P1^1;//高脉冲使能,使能脉宽最小150ns
sbit kong=P1^2;
sbit ok=P1^3;
sbit jia=P1^4;
uchar code table1[]="error!";
uchar code table2[]="0123456789";
uchar code table3[]="SetSpeed:";
uchar code table4[]=" OK! ";
uchar code table5[]="Return.";
uchar pp=0;
uchar biaozhi=0;
uchar jishu=0;
uchar oksign=0;
uchar anjiang=0;
uchar dingshi=0;
void zhongduan_init(void)
{
IT0=1;
EX0=1;
TMOD=0x02;
ET0=1;
TH0=TL0=(256-200);
//EA=1;
}
void delay(uchar x)//1MS定时
{ uint y;
for(;x>0;x--)
{
for(y=123;y>0;y--)
{};
}
}
void lcdwrdata(uchar dat)
{ rs=1;//写数据时RS拉高
lcden=1;
P2=dat;
delay(6);
lcden=0;
delay(6);
}
void lcdwrcom(uchar cdat)//写指令数据到LCD
{ rs=0;//写指令时RS拉低
lcden=1;
P2=cdat;//
delay(6);//不加此延时将导致不能写入指令,但能写入显示数据
lcden=0;
delay(6);
}
void lcd_init()
{
lcdwrcom(0x38);//0x38设置显示模式为:16X2 显示,5X7 点阵,8 位数据接口
lcdwrcom(0x0c);//打开显示光标闪烁
lcdwrcom(0x06);//
lcdwrcom(0x01);//
}
void key(void)
{
jia=1;
ok=1;
if(anjiang==0)
{
if(jia==0)
{
jia=1;
if(jia==0)
{
delay(70);
if(jia==0)
jishu++;
}
}
}
if(ok==0)
{
ok=1;
if(ok==0)
{
delay(70);
if(ok==0)
oksign=1;
}
}
}
void lcd_dong(uchar aj,uchar ao)
{
uchar n=0;
static uchar xiaobiao=0;
if(jishu>5)
{
lcdwrcom(0x01);
lcdwrcom(0x00|0x80);
for(n=0;n<6;n++)
{
lcdwrdata(table1[n]);
//delay(30);
}
lcdwrcom(0x45|0x80);
for(n=0;n<7;n++)
{
lcdwrdata(table4[n]);
// delay(30);
}
jishu=0;
xiaobiao=1;
}
else
{
if(xiaobiao==0)
{
lcdwrcom(0x09|0x80);
lcdwrdata(table2[aj]);
//delay(100);
if(anjiang==0)
{
if(ao==1)
{
oksign=0;
anjiang=1;
lcdwrcom(0x45|0x80);
for(n=0;n<7;n++)
{
lcdwrdata(table5[n]);
// delay(30);
}
EA=1;
}
}
else if(anjiang==1)
{
if(ao==1)
{
oksign=0;
anjiang=0;
lcdwrcom(0x45|0x80);
for(n=0;n<7;n++)
{
lcdwrdata(table4[n]);
// delay(30);
}
kong=1;
EA=0;
}
}
}
else
{
jishu=0;
if(ao==1)
{
lcdwrcom(0x00|0x80);
for(n=0;n<9;n++)
{
lcdwrdata(table3[n]);
// delay(30);
}
xiaobiao=0;
oksign=0;
}
}
}
}
void main()
{ uchar n;
kong=1;
jishu=0;
zhongduan_init();
delay(200);
lcden=0;
lcd_init();
lcdwrcom(0x00|0x80);
for(n=0;n<9;n++)
{
lcdwrdata(table3[n]);
delay(100);
}
lcdwrcom(0x45|0x80);
for(n=0;n<7;n++)
{
lcdwrdata(table4[n]);
delay(100);
}
while(1)
{
/* if(biaozhi==1)
{
delay(3);
if(pp>=1)
kong=0;
}
delay(3);
kong=1;
biaozhi=0;*/
lcd_dong(jishu,oksign);
// if(anjiang==0)
key();
}
}
void guoling(void) interrupt 0
{
TR0=1;
kong=1;
//delay(10);
//kong=0;
//delay(9);
//kong=1;
//delay(1);
/*switch (jishu)
{
case 0: {kong=1;break;}
case 1: {delay(4);kong=0;delay(6);kong=1;break;}
case 2: {delay(3);kong=0;delay(7);kong=1;break;}
case 3: {delay(2);kong=0;delay(8);kong=1;break;}
case 4: {delay(1);kong=0;delay(9);kong=1;break;}
case 5: {kong=0;delay(10);kong=1;break;}
}*/
}
void us(void) interrupt 1 //200us
{
dingshi++;
/* if(dingshi==10)
{
kong=1;
}*/
if(dingshi==25)
{
kong=0;
}
if(dingshi==45)
{
TR0=0;
dingshi=1;
kong=1;
}
}
未命名.jpg (705.63 KB, 下载次数: 0)
下载附件
电路图
2012-4-6 14:15 上传
一周热门 更多>