自己写的电子表程序
该程序已经在实验板上测试过,绝对正确至于端口的设置可以改为自己实验板相应的端口即可
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6; //1602设置
sbit lcde=P2^7;
sbit rw=P3^6;
sbit sw1=P3^2;//功能按键,按下使对应的秒,分,时处闪烁,以便对时
sbit sw2=P3^3;//对时时候,按下该键一次可以使秒,分,时自动加一
sbit sw3=P3^4;//对时时候,按下该键一次可以使秒,分,时自动减一
uchar code table[]="2008-10-27";
uchar code table1[]="10-55-00";
uchar num,t,num1;
uchar sec,min,hour,g,s;
void init();
void write_cmd(uchar cmd);
void write_date(uchar date);
void display(uchar add,uchar date);
void delay(uint z)//延时
{
uint x,y;
for(x=115;x>0;x--)
for(y=z;y>0;y--);
}
void init()//初始化
{ sec=50;
min=55;
hour=10;
write_cmd(0x38);
write_cmd(0x0C);
write_cmd(0x06);
write_cmd(0x80+3);
for(num=0;num<10;num++)
{
write_date(table[num]);
delay(1);
}
write_cmd(0x80+0x40+4);
for(num=0;num<8;num++)
{
write_date(table1[num]);
delay(1);
}
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
EA=1;
ET0=1;
TR0=1;
}
void write_cmd(uchar cmd)//1602写命令
{
rs=0;
rw=0;
P0=cmd;
lcde=0;
delay(1);
lcde=1;
delay(1);
lcde=0;
delay(1);
}
void write_date(uchar date)//1602写数据
{
rs=1;
rw=0;
P0=date;
lcde=0;
delay(1);
lcde=1;
delay(1);
lcde=0;
delay(1);
}
void display(uchar add,uchar date)//1602显示
{
g=date%10;
s=date/10;
write_cmd(0x80+0x40+add);
write_date(0x30+s);
write_date(0x30+g);
}
void keyscan()//按键扫描
{
if(sw1==0)
{
delay(5);
if(sw1==0)
{
num1++;
while(!sw1);
if(num1==1)
{
TR0=0;
write_cmd(0x80+0x40+11);
write_cmd(0x0f);
}
if(num1==2)
{
write_cmd(0x80+0x40+8);
write_cmd(0x0f);
}
if(num1==3)
{
write_cmd(0x80+0x40+5);
write_cmd(0x0f);
}
if(num1==4)
{
num1=0;
TR0=1;
write_cmd(0x0c);
}
}
}
if(num1!=0)
{
if(sw2==0)
{
delay(5);
if(sw2==0)
{ while(!sw2);
if(num1==1)
{
sec++;
if(sec==60)
sec=0;
//write_cmd(0x80+0x40+10);
display(10,sec);
}
if(num1==2)
{
min++;
if(min==60)
min=0;
//write_cmd(0x80+0x40+10);
display(7,min);
}
if(num1==3)
{
hour++;
if(hour==24)
hour=0;
//write_cmd(0x80+0x40+10);
display(4,hour);
}
}
}
}
if(num1!=0)
{
if(sw3==0)
{
delay(5);
if(sw3==0)
{ while(!sw3);
if(num1==1)
{
sec--;
if(sec==-1)
sec=59;
//write_cmd(0x80+0x40+10);
display(10,sec);
}
if(num1==2)
{
min--;
if(min==-1)
min=59;
//write_cmd(0x80+0x40+10);
display(7,min);
}
if(num1==3)
{
hour--;
if(hour==-1)
hour=23;
//write_cmd(0x80+0x40+10);
display(4,hour);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(t==20)
{
t=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
display(4,hour);
}
display(7,min);
}
display(10,sec);
}
}
}
void timer0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
t++;
}
[[it] 本帖最后由 huizi0711 于 2008-10-27 13:45 编辑 [/it]]