DS1302三键4位1.5寸数码管时钟

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

说明:最初跑得不太准,把1302的电容换成7p以后,精度非常好,具体没有查误差,反正好几个月与家里的石英钟很对时。5v供电,亮度不高,室内用正好。本人程序基本抄袭的,其中秒是用定时器模拟闪动,各位老师多多指教,原来是显示带拐角的7,让我改了,第一位为零时也改作不显示了,请各位大大批评指正!随便挣俩钱儿(感谢江顺万老师的细心批评指正,由于自己不够严谨导致一点错误,已经在原文改过,深表歉意!)先上电路图:再上个实物图:分解图:最后上c程序,不用下载,照顾没米的大大,欢迎批评,砸点儿币过来。三鞠躬!/**********************************DS1302四位数码管***********************************/#includereg52.h#defineucharunsignedchar#defineuintunsignedint#defineDP2//段选#defineDP0//位选sbitIO=P1^1;//数据口sbitSCLK=P1^2;//控制数据时钟sbitRST=P1^0;//使能端、复位端/************按键引脚定义***********/sbits1=P1^5;//按键加sbits2=P1^3;//按键减sbits3=P1^4;//按键选择sbitled=P1^7;//闪烁的小数点uchars,knum=0,snum,fnum;bitflag;//就是定义了一个bit类型的变量flag,flag的取值只能是0或1,这种类型的变量一般都是用来做标志。/***********写时分秒地址************/#definewrite_shi0x84#definewrite_fen0x82#definewrite_miao0x80/***********读时分秒地址************/#defineread_shi0x85#defineread_fen0x83#defineread_miao0x81bitmiao_flag;charmiao_num;charDmiao;charshi,fen,miao;//读出数据存储变量uchard[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90};//不带小数点,共阳uchardd[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//带小数点,共阳voiddelay(uintz)//延时函数,z的取值为这个函数的延时ms数,如delay(200);大约延时200ms.{//delay(500);大约延时500ms.uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidt0_init()//定时50ms一个中断{TMOD=0X01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}ucharread_1302(ucharadd)//读函数{uchari,date;RST=0;//禁止读写数据for(i=0;i8;i++){RST=1;SCLK=0;IO=add&0x01;add=1;SCLK=1;}for(i=0;i8;i++){if(IO)date=date|0x80;elsedate=date|0x00;SCLK=1;date=1;SCLK=0;}RST=0;date=date/16*10+date%16;//进制转换16转10进制IO=0;//数据读完后,IO口必须为0,不然小时一直显示85returndate;}voidwrite_1302(ucharadd,uchardat)//写函数{uchari;RST=0;//禁止读写数据SCLK=0;for(i=0;i8;i++)//写地址{RST=1;//允许读写数据SCLK=0;IO=add&0x01;add=1;SCLK=1;}for(i=0;i8;i++)//写数据{RST=1;//允许读写数据SCLK=0;IO=dat&0x01;dat=1;SCLK=1;}RST=0;}voidinit_1302()//初始化函数设置时间{flag=read_1302(0x81);if(flag&0x80)write_1302(0x8e,0x00);//保护取消,可以进行读写操作write_1302(write_miao,0x56);write_1302(write_fen,0x49);write_1302(write_shi,0x14);write_1302(0x90,0xa5);write_1302(0x8e,0x80);//保护启动,不能进行读写操作}voiddisplay(ucharshi,ucharfen)//显示函数{if(knum==0){snum=30;fnum=30;}if(knum==1){fnum++;snum=30;}if(knum==2){snum++;fnum=30;}if(snum=30){W=0xF7;//位选s=d[shi/10];//段码先给s,判断其值是否为0,if(s==0xc0){W=0xff;D=0xff;}D=s;//s不为零则给D,让其显示delay(5);D=0Xff;//消隐if(miao_flag)//小数点闪烁{miao_flag=0;W=0xFb;D=dd[shi%10];delay(5);D=0Xff;//消隐}else{W=0xFb;D=d[shi%10];delay(5);D=0Xff;}if(snum==60)snum=0;}if(fnum=30){W=0xFd;D=d[fen/10];delay(5);D=0Xff;//消隐W=0xFe;D=d[fen%10];delay(5);D=0Xff;//消隐if(fnum==60)fnum=0;}}voidread_sf(){miao=read_1302(read_miao);/*if(miao!=Dmiao){Dmiao=miao;TR0=1;miao_flag=1;}*/fen=read_1302(read_fen);shi=read_1302(read_shi);display(shi,fen);}voidkeyscan()//按键扫描函数{if(s3==0)//选择按键按下{delay(10);if(s3==0){while(!s3)display(shi,fen);//加上这句,按键按下就不会闪knum++;if(knum==1)//分闪烁{write_1302(0x8e,0x00);//保护取消write_1302(write_miao,0x80);}if(knum==3)//时钟启动{knum=0;write_1302(write_miao,0x00);write_1302(0x8e,0x80);//保护启动}}}if(knum==1)//分钟调节{if(s1==0)//加{delay(10);if(s1==0){while(!s1)display(shi,fen);//加上这句,按键按下就不会闪fen++;if(fen==60)fen=0;write_1302(write_fen,fen/10*16+fen%10);//写入1302//read_sf();//读出时间,然后显示}}if(s2==0){delay(10);if(s2==0){while(!s2)display(shi,fen);//加上这句,按键按下就不会闪fen--;if(fen==-1)fen=59;write_1302(write_fen,fen/10*16+fen%10);read_sf();}}}if(knum==2){if(s1==0){delay(10);if(s1==0){while(!s1)display(shi,fen);//加上这句,按键按下就不会闪shi++;if(shi==24)shi=0;write_1302(write_shi,shi/10*16+shi%10);read_sf();}}if(s2==0){delay(10);if(s2==0){while(!s2)display(shi,fen);//加上这句,按键按下就不会闪shi--;if(shi==-1)shi=23;write_1302(write_shi,shi/10*16+shi%10);read_sf();}}}}voidmain(){init_1302();t0_init();while(1){if(miao_num10)led=0;if(miao_num=10)led=1;read_sf();keyscan();}}voidt0_timer()interrupt1{TMOD=0X01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;miao_num++;if(miao_num=20){miao_num=0;miao_flag=1;}}

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功