新手一个,用定时器做了一个1602的电子表,要的可以看看
时间:10-02
整理:3721RD
点击:
做了一个电子表,用定时器的,
现在想加一个闹钟设置,可是不知怎么做?
求解?
#include <reg52.h> //52寄存器文件
#define uchar unsigned char //用uchar代替unsigned char
#define uint unsigned int //uint代替unsigned int
void write_xingqi();
sbit rs = P2^6; //复位端
sbit rw = P2^5; //写数据端
sbit ep = P2^7; //使能端
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
uchar code dis1[] = "2013-08-07 XQi:";
uchar code dis2[] = " 10:12:30 ";
uchar code dis3[]={0x31,0x32,0x33,0x34,0x35,0x36,0x37};
uchar num,s1num,s2num,s3num,s4num,s5num;
char shi=10,fen=12,miao=30,xinqi=3;
char ri=7,yue=8,nian1=20,nian2=13;
/*******************延时函数********************/
void delay(uchar i)
{
uchar j;
for(;i>0;i--)
for(j=110;j>0;j--);
}
/******************写指令函数**********************/
void write_com(uchar com)
{ // 写入指令数据到LCD
rs = 0;
rw = 0;
ep = 0;
delay(1);
P0 = com; //将8位指令通过P0口传给1602
delay(5);
ep = 1;
delay(5);
ep = 0;
}
/*********************写数据函数******************/
void write_data(uchar dat)
{ //写入字符显示数据到LCD
rs = 1;
rw = 0;
ep = 0;
P0 = dat;//将8位数据通过P0口传给1602
delay(5);
ep = 1;
delay(5);
ep = 0;
}
/******************初始化函数*******************/
void init()
{
uchar i;
write_com(0x38); ////16*2显示,5*7点阵,8位数据
delay(1);
write_com(0x0c); //显示开,关光标 0x0f(开且闪烁) 0x0c(关) 0x0e(开但光标不闪)
delay(1);
write_com(0x06); //移动光标
delay(1);
write_com(0x01); //清除LCD的显示内容
delay(1);
write_com(0x80); //设置显示位置为第一行的第1个字符
for(i=0;i<15;i++)
{
write_data(dis1[i]);
delay(1); //显字时间
}
write_com(0x80+0x40); //设置显示位置为第二行第1个字符
for(i=0;i<16;i++)
{
write_data(dis2[i]);
delay(1);
}
write_xingqi(); //设置星期几
TMOD=0x01; //定时器初始化
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void write_sfm(uchar add,uchar date) //时分秒函数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_ri(uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+8);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_yue(uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+5);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_nian1(uchar date)
{
uchar shi,ge;
shi=date/10;
ge= date%10;
write_com(0x80);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_nian2(uchar date)
{
uchar shi,ge;
shi=date/10;
ge= date%10;
write_com(0x80+2);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_xingqi() //星期几函数
{
write_com(0x80+15);
write_data(dis3[xinqi]);
}
void ks()
{
if(s1==0)
{
delay(5); //消抖
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
write_com(0x80+15);
}
if(s1num==5)
{
write_com(0x80+9);
}
if(s1num==6)
{
write_com(0x80+6);
}
if(s1num==7)
{
write_com(0x80+3);
}
if(s1num==8)
{
write_com(0x80+1);
}
if(s1num==9)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if((s2==0)&&(s1num!=0))
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen++;
if(fen==60)
{
fen=0;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xinqi++;
if(xinqi==8)
{xinqi=1;}
write_xingqi();
write_com(0x80+15);
write_com(0x0f);
}
if(s1num==5)
{
ri++;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{if(ri==32)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
if(yue==4||yue==6||yue==9||yue==11)
{if(ri==31)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
if(yue==2)
{if(ri==29)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
}
if(s1num==6)
{
yue++;
if(yue==13)
{yue=1;}
write_yue(yue);
write_com(0x80+6);
}
if(s1num==7)
{
nian2++;
if(nian2==100)
{
nian2=0;
}
write_nian2(nian2);
write_com(0x80+3);
}
if(s1num==8)
{
nian1++;
if(nian1==100)
{
nian1=0;
}
write_nian1(nian1);
write_com(0x80+1);
}
}
}
if((s3==0)&&(s1num!=0))
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen--;
if(fen==-1)
{
fen=59;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi--;
if(shi==-1)
{
shi=23;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xinqi--;
if(xinqi==-1)
{xinqi=6;}
write_xingqi();
write_com(0x80+15);
write_com(0x0f);
}
if(s1num==5)
{
ri--;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{if(ri==0)
{ri=31;}
write_ri(ri);
write_com(0x80+9);}
if(yue==4||yue==6||yue==9||yue==11)
{if(ri==0)
{ri=30;}
write_ri(ri);
write_com(0x80+9);}
if(yue==2)
{if(ri==0)
{ri=28;}
write_ri(ri);
write_com(0x80+9);}
}
if(s1num==6)
{
yue--;
if(yue==0)
{yue=12;}
write_yue(yue);
write_com(0x80+6);
}
if(s1num==7)
{
nian2--;
if(nian2==-1)
{
nian2=99;
}
write_nian2(nian2);
write_com(0x80+3);
}
if(s1num==8)
{
nian1--;
if(nian1==-1)
{
nian1=99;
}
write_nian1(nian1);
write_com(0x80+1);
}
}
}
}
void main()
{
init(); //初始化LCD
while(1)
{
ks();
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
xinqi++;
if(xinqi==8)
{xinqi=1;}
ri++;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==32)
{
ri=1;
yue++;
if(yue==13)
{
yue=1;
nian2++;
if(nian2==100)
{ nian2=0;
nian1++;
write_nian1(nian1);
}
write_nian2(nian2);
}
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==31)
{
ri=1;
yue++;
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
if(yue==2)
{
if(ri==29)
{
ri=1;
yue++;
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
现在想加一个闹钟设置,可是不知怎么做?
求解?
#include <reg52.h> //52寄存器文件
#define uchar unsigned char //用uchar代替unsigned char
#define uint unsigned int //uint代替unsigned int
void write_xingqi();
sbit rs = P2^6; //复位端
sbit rw = P2^5; //写数据端
sbit ep = P2^7; //使能端
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
uchar code dis1[] = "2013-08-07 XQi:";
uchar code dis2[] = " 10:12:30 ";
uchar code dis3[]={0x31,0x32,0x33,0x34,0x35,0x36,0x37};
uchar num,s1num,s2num,s3num,s4num,s5num;
char shi=10,fen=12,miao=30,xinqi=3;
char ri=7,yue=8,nian1=20,nian2=13;
/*******************延时函数********************/
void delay(uchar i)
{
uchar j;
for(;i>0;i--)
for(j=110;j>0;j--);
}
/******************写指令函数**********************/
void write_com(uchar com)
{ // 写入指令数据到LCD
rs = 0;
rw = 0;
ep = 0;
delay(1);
P0 = com; //将8位指令通过P0口传给1602
delay(5);
ep = 1;
delay(5);
ep = 0;
}
/*********************写数据函数******************/
void write_data(uchar dat)
{ //写入字符显示数据到LCD
rs = 1;
rw = 0;
ep = 0;
P0 = dat;//将8位数据通过P0口传给1602
delay(5);
ep = 1;
delay(5);
ep = 0;
}
/******************初始化函数*******************/
void init()
{
uchar i;
write_com(0x38); ////16*2显示,5*7点阵,8位数据
delay(1);
write_com(0x0c); //显示开,关光标 0x0f(开且闪烁) 0x0c(关) 0x0e(开但光标不闪)
delay(1);
write_com(0x06); //移动光标
delay(1);
write_com(0x01); //清除LCD的显示内容
delay(1);
write_com(0x80); //设置显示位置为第一行的第1个字符
for(i=0;i<15;i++)
{
write_data(dis1[i]);
delay(1); //显字时间
}
write_com(0x80+0x40); //设置显示位置为第二行第1个字符
for(i=0;i<16;i++)
{
write_data(dis2[i]);
delay(1);
}
write_xingqi(); //设置星期几
TMOD=0x01; //定时器初始化
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void write_sfm(uchar add,uchar date) //时分秒函数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_ri(uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+8);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_yue(uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+5);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_nian1(uchar date)
{
uchar shi,ge;
shi=date/10;
ge= date%10;
write_com(0x80);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_nian2(uchar date)
{
uchar shi,ge;
shi=date/10;
ge= date%10;
write_com(0x80+2);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_xingqi() //星期几函数
{
write_com(0x80+15);
write_data(dis3[xinqi]);
}
void ks()
{
if(s1==0)
{
delay(5); //消抖
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
write_com(0x80+15);
}
if(s1num==5)
{
write_com(0x80+9);
}
if(s1num==6)
{
write_com(0x80+6);
}
if(s1num==7)
{
write_com(0x80+3);
}
if(s1num==8)
{
write_com(0x80+1);
}
if(s1num==9)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if((s2==0)&&(s1num!=0))
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen++;
if(fen==60)
{
fen=0;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xinqi++;
if(xinqi==8)
{xinqi=1;}
write_xingqi();
write_com(0x80+15);
write_com(0x0f);
}
if(s1num==5)
{
ri++;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{if(ri==32)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
if(yue==4||yue==6||yue==9||yue==11)
{if(ri==31)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
if(yue==2)
{if(ri==29)
{ri=1;}
write_ri(ri);
write_com(0x80+9);}
}
if(s1num==6)
{
yue++;
if(yue==13)
{yue=1;}
write_yue(yue);
write_com(0x80+6);
}
if(s1num==7)
{
nian2++;
if(nian2==100)
{
nian2=0;
}
write_nian2(nian2);
write_com(0x80+3);
}
if(s1num==8)
{
nian1++;
if(nian1==100)
{
nian1=0;
}
write_nian1(nian1);
write_com(0x80+1);
}
}
}
if((s3==0)&&(s1num!=0))
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen--;
if(fen==-1)
{
fen=59;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi--;
if(shi==-1)
{
shi=23;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xinqi--;
if(xinqi==-1)
{xinqi=6;}
write_xingqi();
write_com(0x80+15);
write_com(0x0f);
}
if(s1num==5)
{
ri--;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{if(ri==0)
{ri=31;}
write_ri(ri);
write_com(0x80+9);}
if(yue==4||yue==6||yue==9||yue==11)
{if(ri==0)
{ri=30;}
write_ri(ri);
write_com(0x80+9);}
if(yue==2)
{if(ri==0)
{ri=28;}
write_ri(ri);
write_com(0x80+9);}
}
if(s1num==6)
{
yue--;
if(yue==0)
{yue=12;}
write_yue(yue);
write_com(0x80+6);
}
if(s1num==7)
{
nian2--;
if(nian2==-1)
{
nian2=99;
}
write_nian2(nian2);
write_com(0x80+3);
}
if(s1num==8)
{
nian1--;
if(nian1==-1)
{
nian1=99;
}
write_nian1(nian1);
write_com(0x80+1);
}
}
}
}
void main()
{
init(); //初始化LCD
while(1)
{
ks();
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
xinqi++;
if(xinqi==8)
{xinqi=1;}
ri++;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==32)
{
ri=1;
yue++;
if(yue==13)
{
yue=1;
nian2++;
if(nian2==100)
{ nian2=0;
nian1++;
write_nian1(nian1);
}
write_nian2(nian2);
}
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==31)
{
ri=1;
yue++;
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
if(yue==2)
{
if(ri==29)
{
ri=1;
yue++;
write_yue(yue);
}
write_ri(ri);
write_xingqi();
}
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
闹钟不就是判断时间吗,你时间都能显示了还不能做个判断?
额~~我说的是,加个按键调出时间,然后时间调好后,又回去走表,走到了,也就是闹铃了,不是keil软件中加个判断就行的
这就是按键调整一个数据,然后利用时间和这个数据做判断啊
说的好轻巧~~再按下的时候,表时停的,但是调好后,表继续走,这里就有一个问题啊,