电子时钟设计 要求:1.用单片机完成时钟功能,并通过LED数码管显示出来。 2.可通过键盘来修改时、分、秒。
发布网友
发布时间:2022-09-26 05:22
我来回答
共4个回答
热心网友
时间:2023-09-27 06:16
只有程序,从程序中可以看出电路图。不过使用1602显示的,可以显示年,月,日,星期,时,分秒,同时可以修改。
#include "stc12c5a.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^3;//;模拟I2C 数据传送位
sbit scl=P2^4;//;模拟I2C 时钟控制状态标志
sbit lcrs=P2^2;//数据/命令
sbit lcwr=P2^1;//读/写
sbit lcden=P2^0;//使能
sbit key1=P4^4;
sbit key2=P4^5;
sbit key3=P2^7;
sbit key4=P4^6;
uchar now[7];
uchar code table[]={48,49,50,51,52,53,54,55,56,57};
uchar code nl[]={"alarm clock"};
uchar code on[]={"ON "};
uchar code off[]={"OFF"};
uchar dis[16];
uchar H=19;//定义小时
uchar Mi=15;//定义分钟
uchar S=0;//定义秒
uchar Y=12;//定义年
uchar Mo=4;//定义月
uchar D=21;//定义日期
uchar W=6;//定义星期
uchar ke1,ke2,ke3,ke4;
uchar nl1h=0,nl1m=0,nl2h=0,nl2m=0;
uchar bcdto(uchar temp);
////////////////////// 延时子程序/////////////////////////////
void delayms(uint ms) //在11.0592M晶振下,单周期指令的ms级延时
{
uint i;
while(ms--)
{
for(i = 0; i < 850; i++);
}
}
void delay()//5us延时
{
_nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();
}
//////1062/////////
void ydelay(uchar x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void write_com(uchar com)
{
P0=com;
delay();
lcwr=0;
delay();
lcrs=0;
delay();
lcden=0;
ydelay(40);
lcden=1;
ydelay(40);
lcden=0;
delay();
lcwr=1;
}
void write_date(uchar date)
{
P0=date;
delay();
lcwr=0;
delay();
lcrs=1;
delay();
lcden=0;
ydelay(40);
lcden=1;
ydelay(40);
lcden=0;
delay();
lcwr=1;
}
void init1602()
{
write_com(0x38);//设置显示模式
ydelay(40);
write_com(0x0c);//开显示
ydelay(40);
write_com(0x06);//指针和光标自动加一
ydelay(40);
write_com(0x01);//清屏指令
ydelay(40);
}
void display(uchar a)//显示
{
uint i,j;
if(ke2==0)
{
j=W;
if(j==7)
j=8;
dis[0]='2';dis[1]='0';dis[2]=table[Y/10];dis[3]=table[Y%10];
dis[4]='.';dis[5]=table[Mo/10];dis[6]=table[Mo%10];
dis[7]='.';dis[8]=table[D/10];dis[9]=table[D%10];dis[10]=' ';
dis[11]='W';dis[12]='e';dis[13]='e';dis[14]=' ';
dis[15]=table[j];
if(a==3)
{dis[2]=' ';dis[3]=' ';}
else if(a==4)
{dis[5]=' ';dis[6]=' ';}
else if(a==5)
{dis[8]=' ';dis[9]=' ';}
else if(a==6)
{dis[15]=' ';}
write_com(0xc0);
for(i=0;i<16;i++)
write_date(dis[i]);
dis[0]=table[H/10];dis[1]=table[H%10];dis[2]=':';
dis[3]=table[Mi/10];dis[4]=table[Mi%10];dis[5]=':';
dis[6]=table[S/10];dis[7]=table[S%10];dis[8]=' ';
if(a==1)
{
dis[0]=' ';dis[1]=' ';
}
else if(a==2)
{
dis[3]=' ';dis[4]=' ';
}
write_com(0x84);
for(i=0;i<9;i++)
write_date(dis[i]);
}
else
{
write_com(0x80);
for(i=0;i<11;i++)
write_date(nl[i]);
if((ke2==1)||(ke2==2))
dis[0]=table[1];
else if((ke2==3)||(ke2==4))
dis[0]=table[2];
write_com(0x8c);
write_date(dis[0]);
if((ke2==1)||(ke2==2))
{
dis[0]=table[nl1h/10];dis[1]=table[nl1h%10];dis[2]=':';
dis[3]=table[nl1m/10];dis[4]=table[nl1m%10];
if(ke3==0)
{
write_com(0xcb);
for(i=0;i<3;i++)
write_date(off[i]);
}
else if(ke3==1)
{
write_com(0xcb);
for(i=0;i<3;i++)
write_date(on[i]);
}
}
else if((ke2==3)||(ke2==4))
{
dis[0]=table[nl2h/10];dis[1]=table[nl2h%10];dis[2]=':';
dis[3]=table[nl2m/10];dis[4]=table[nl2m%10];
if(ke4==0)
{
write_com(0xcb);
for(i=0;i<3;i++)
write_date(off[i]);
}
else if(ke4==1)
{
write_com(0xcb);
for(i=0;i<3;i++)
write_date(on[i]);
}
}
if(a==1)
{dis[0]=' ';dis[1]=' ';}
else if(a==2)
{dis[3]=' ';dis[4]=' ';}
write_com(0xc4);
for(i=0;i<5;i++)
write_date(dis[i]);
}
}
///////////IIC//////////////////
void init() //初始化
{
sda=1;
delay();
scl=1;
delay();
}
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void write_byte(uchar date) // 写数据子函数
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte() // 读数据子函数
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write(uchar address,uchar date,uchar add) // 写数据函数
{
start();
write_byte(add);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read(uchar address,uchar addw,uchar addr) //读数据函数
{
uchar date;
start();
write_byte(addw);
respons();
write_byte(address);
respons();
start();
write_byte(addr);
respons();
date=read_byte();
stop();
return date;
}
uchar Decimal_to_BCD(uchar temp)//十进制转换成BCD码
{
uchar a,b,c;
a=temp;
b=0;
if(a>=10)
{
while(a>=10)
{
a=a-10;
b=b+16;
c=a+b;
temp=c;
}
}
return temp;
}
uchar BCD_to_Decimal(uchar temp)//BCD码转换成十进制
{
uchar a,b,c;
a=temp;
b=0;
if(a>=16)
{
while(a>=16)
{
a=a-16;
b=b+10;
c=a+b;
temp=c;
}
}
return temp;
}
void keyf(uchar *num,uchar up,uchar )
{
uint z;
if(key2==0)
{
delayms(10);
if(key2==0)
{
if(*num>=up)
{
if((ke1==4)||(ke1==5)||(ke1==6))
*num=1;
else
*num=0;
}
else
*num=*num+1;
for(z=0;z<50;z++)
{
display(0);
if(key2!=0)
break;
}
while(!key2)
{
for(z=0;z<25;z++)
{display(0);}
if(*num>=up)
{
if((ke1==4)||(ke1==5)||(ke1==6))
*num=1;
else
*num=0;
}
else
*num=*num+1;
}
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
if(*num==)
{
if((ke1==1)||(ke2==1)||(ke2==3))
*num=23;
else if((ke1==2)||(ke2==2)||(ke2==4))
*num=59;
else if(ke1==3)
*num=99;
else if(ke1==4)
*num=12;
else if(ke1==5)
*num=31;
else if(ke1==6)
*num=7;
}
else
*num=*num-1;
for(z=0;z<50;z++)
{
display(0);
if(key3!=0)
break;
}
while(!key3)
{
for(z=0;z<25;z++)
display(0);
if(*num==)
{
if((ke1==1)||(ke2==1)||(ke2==3))
*num=23;
else if((ke1==2)||(ke2==2)||(ke2==4))
*num=59;
else if(ke1==3)
*num=99;
else if(ke1==4)
*num=12;
else if(ke1==5)
*num=31;
else if(ke1==6)
*num=7;
}
else
*num=*num-1;
}
}
}
}
void keyjc()
{
uint i,j;
if(key1==0)
{
for(i=0;i<30;i++)
display(0);
if(key1==0)
ke1++;
if(ke1>=7)
{
ke1=0;
display(0);
}
while(!key1);
if(ke1==1)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(1);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&H,23,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=2;
while(!key1);
break;
}
}
}
}
if(ke1==2)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(2);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&Mi,59,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=3;
while(!key1);
break;
}
}
}
}
if(ke1==3)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(3);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&Y,99,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=4;
while(!key1);
break;
}
}
}
}
if(ke1==4)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(4);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&Mo,12,1);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=5;
while(!key1);
break;
}
}
}
}
if(ke1==5)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(5);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&D,31,1);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=6;
while(!key1);
break;
}
}
}
}
if(ke1==6)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(6);
if((key2==0)||(key3==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&W,7,1);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
ke1=0;
while(!key1);
break;
}
}
}
}
Mi=Decimal_to_BCD(Mi);
write(0x01,Mi,0xd0);//写入分
H=Decimal_to_BCD(H);
write(0x02,H,0xd0);//写入时
W=Decimal_to_BCD(W);
write(0x03,W,0xd0);//写入星期
D=Decimal_to_BCD(D);
write(0x04,D,0xd0);//写入日期
Mo=Decimal_to_BCD(Mo);
write(0x05,Mo,0xd0);//写入月
Y=Decimal_to_BCD(Y);
write(0x06,Y,0xd0);//写入年
S=Decimal_to_BCD(S);
write(0x00,S,0xd0);//写入秒
ke1=0;
}
if(key4==0)
{
for(i=0;i<30;i++)
display(0);
if(key4==0)
ke2++;
if(ke2>=5)
ke2=0;
while(!key4);
write_com(0x01);
}
if(ke2==1)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(1);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&nl1h,23,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
if(ke3==0)
ke3=1;
else
ke3=0;
while(!key1);
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
ke2=2;
while(!key4);
break;
}
}
}
write_com(0x01);
}
if(ke2==2)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(2);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&nl1m,59,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
if(ke3==0)
ke3=1;
else
ke3=0;
while(!key1);
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
ke2=3;
while(!key4);
break;
}
}
}
write_com(0x01);
}
if(ke2==3)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(1);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&nl2h,23,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
if(ke4==0)
ke4=1;
else
ke4=0;
while(!key1);
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
ke2=4;
while(!key4);
break;
}
}
}
write_com(0x01);
}
if(ke2==4)
{
for(i=0;i<25;i++)
{
for(j=0;j<20;j++)
{
display(2);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
for(j=0;j<20;j++)
{
display(0);
if((key2==0)||(key3==0)||(key4==0)||(key1==0))
break;
}
if((key2==0)||(key3==0))
{
delayms(10);
if((key2==0)||(key3==0))
{
i=0;
keyf(&nl2m,59,0);
}
}
if(key1==0)
{
delayms(10);
if(key1==0)
{
if(ke4==0)
ke4=1;
else
ke4=0;
while(!key1);
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
ke2=0;
while(!key4);
break;
}
}
}
write(0x00,nl1h,0xa0);
delayms(10);
write(0x01,nl1m,0xa0);
delayms(10);
write(0x02,nl2h,0xa0);
delayms(10);
write(0x03,nl2m,0xa0);
delayms(10);
write(0x04,ke3,0xa0);
delayms(10);
write(0x05,ke4,0xa0);
write_com(0x01);
}
}
void nz()
{
if(ke3==1)
{
if(nl1h==H)
{
if(nl1m==Mi)
key1=1;
else;
}
}
if(ke4==1)
{
if(nl2h==H)
{
if(nl2m==Mi)
key1=1;
else;
}
}
}
void main()
{
P4SW=0x70;
init1602();
init();
write(0x07,0x10,0xd0);
delayms(11);
nl1h=read(0x00,0xa0,0xa1);
delayms(11);
nl1m=read(0x01,0xa0,0xa1);
delayms(11);
nl2h=read(0x02,0xa0,0xa1);
delayms(11);
nl2m=read(0x03,0xa0,0xa1);
delayms(11);
ke3=read(0x04,0xa0,0xa1);
delayms(11);
ke4=read(0x05,0xa0,0xa1);
while(1)
{
S=read(0x00,0xd0,0xd1);
S=BCD_to_Decimal(S);//BCD码转换成十进制
Mi=read(0x01,0xd0,0xd1);
Mi=BCD_to_Decimal(Mi);//BCD码转换成十进制
H=read(0x02,0xd0,0xd1);
H=BCD_to_Decimal(H);//BCD码转换成十进制
W=read(0x03,0xd0,0xd1);//星期为:1~7 不需要进制的转换
W=BCD_to_Decimal(W);
D=read(0x04,0xd0,0xd1);
D=BCD_to_Decimal(D);//BCD码转换成十进制
Mo=read(0x05,0xd0,0xd1);
Mo=BCD_to_Decimal(Mo);//BCD码转换成十进制
Y=read(0x06,0xd0,0xd1);
Y=BCD_to_Decimal(Y);//BCD码转换成十进制
display(0);//显示
keyjc();
nz();
delayms(10);
}
}
热心网友
时间:2023-09-27 06:16
t单片机是很不错的
热心网友
时间:2023-09-27 06:17
网上有做好的 还自己编程呀 ,厉害
热心网友
时间:2023-09-27 06:17
你要那多啊!我以前做过这个设计,不知道放哪了,我电脑更换了!那个我仿真是正确的,你留个邮箱吧,我找到了发给你!追问1030855164@qq.com能不能这几天就给我回话?