我代码如下:
#include<reg51.h>
unsigned char tab[]=" 23:58:48";
unsigned char code tab2[]="SZ30329 ";
#define lcdp P0
sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;
sbit led=P1^7;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit RED=P3^4;
unsigned s=2;
unsigned flag=0;
unsigned char n,count,number;
char shi=23,fen=58,miao=55;
void delay(unsigned char z)
{
unsigned char i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
} //延时
void time_gai()
{
if(count==0)
s--;
}
void write_com(unsigned char com)//写指令
{
rs=0;
rw=0;
en=0;
lcdp=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_date(unsigned char date)//写数据
{
rs=1;
rw=0;
en=0;
lcdp=date;
delay(5);
en=1;
delay(5);
en=0;
}
void write_sj(unsigned add,unsigned date)
{
unsigned char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(shi+0x30);
write_date(ge+0x30);
}
void init() //初始化
{
write_com(0x38); //设置16x2显示,5x7点阵,8位数据接口
write_com(0x06); //写一个字符后地址指针自动加1
write_com(0x01); //显示清零,数据指针清零
write_com(0x0c); //开显示,不显示光标
//write_com(0x08);
//write_com(0x0e);//光标开启,但不闪烁
write_com(0x80); //显示位置
for(n=0;n<10;n++)
{
write_date(tab[n]);
delay(1);
}
write_com(0x80+0x40+3);//第二行显示
for(n=0;n<8;n++)
{
write_date(tab2[n]);
delay(1);
}
//定时器初始化
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
number++;
while(!s1);
}
if(number==1)
{
TR0=0;
write_com(0x80+9);
write_com(0x0f);//光标闪烁
}
if(number==2)
{
write_com(0x80+6);
write_com(0x0f);//光标闪烁
}
if(number==3)
{
write_com(0x80+3);
write_com(0x0f);//光标闪烁
}
}
if(number==4)
{
TR0=1;
number=0;
write_com(0x0c);//关闭光标
}
if(number!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(number==1)
{
miao++;
if(miao==60)
miao=0;
write_sj(8,miao);
write_com(0x80+9);
}
if(number==2)
{
fen++;
if(fen==60)
fen=0;
write_sj(5,fen);
write_com(0x80+6);
}
if(number==3)
{
shi++;
led=!led;
if(shi==24)
shi=0;
write_sj(2,shi);
write_com(0x80+3);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(number==1)
{
miao--;
if(miao==-1)
miao=59;
write_sj(8,miao);
write_com(0x80+9);
}
if(number==2)
{
fen--;
if(fen==-1)
fen=59;
write_sj(5,fen);
write_com(0x80+6);
}
if(number==3)
{
shi--;
led=!led;
if(shi==-1)
shi=23;
write_sj(2,shi);
write_com(0x80+3);
}
}
}
}
}
void main()
{
RED=0;
init();
while(1)
{
keyscan();
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sj(2,shi);
}
write_sj(5,fen);
}
write_sj(8,miao);
}
}
想加个倒计时秒表,四分钟(不用调的),按下开关就工作,求大神帮写下程序
|