资源描述:
《ds1302数码管显示应用》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、DS1302应用刚学单片机,好多好奇,所以想做个简单的时钟。下面是PROTEUS仿真电路和电路图,简单易懂。文笔不好,说了多余。下面是程序。#include#include#include#defineuintunsignedint#defineucharunsignedchar#defineBCDTUAN(str)(str/10*16+str%10)//定义宏,将要写入DS1302的时间转化为BCD码#defineWrite_Sec0x80#defineWrite_Min0x82#defineWrite_Hou0x84#de
2、fineWrite_Dat0x86#defineWrite_Mon0x88#defineWrite_day0x8a#defineWrite_Yea0x8c#defineWrite_WP0x8e//写保护位#defineWrite_TCR0x90#defineRead_Sec0x81#defineRead_Min0x83#defineRead_Hou0x85#defineRead_Dat0x87#defineRead_Mon0x89#defineRead_Day0x8b#defineRead_Yea0x8d#defineCLK_BurstW0xbf//时钟突发模式写#defineCLK_
3、BurstR0xbf//时钟突发模式读#defineWrite_RAM_Begin0xc0//RAM第一个字节写指令#defineRead_RAM_Begin0xc1//RAM第一个字节读指令#defineRAM_BurstW0xfe//突发模式写RAM#defineRAM_BurstR0xff//突发模式读RAMsbit_74hc154_A=P1^0;sbit_74hc154_B=P1^1;sbit_74hc154_C=P1^2;sbit_74hc154_D=P1^3;sbitCE=P1^6;sbitSCLK=P1^5;sbitIO=P1^4;ucharcodescan[][4]={
4、{0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},{0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1},{1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1},{1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}};ucharscanbuff[]={0,0,0,0,0,0,0,0,0,0,0,0};ucharcodedispdate[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};voidWriteDS1302(void)
5、;//向DS1302写入时间。voidReadDS1302(void);//读取DS1302中的年,月,日,星期几,小时,分钟和秒.voidSendCmd(unsignedcharcmd);//传送地址voiddelayus(unsignedchartt);voiddelay(ucharz){ucharx,y;for(x=z;x>0;x--)for(y=120;y>0;y--);}void_74hc154(uchark)//位扫描设定{_74hc154_A=scan[k][0];_74hc154_B=scan[k][1];_74hc154_C=scan[k][2];_74hc154_D
6、=scan[k][3];}voidmain(){uchari;while(1){WriteDS1302();ReadDS1302();for(i=0;i<12;i++){_74hc154(i);//位扫描P0=~dispdate[scanbuff[i]];//数据送P0口delay(10);}}}voidWriteDS1302(void){WriteByte(Write_Yea,BCDTUAN(13));//写13年}//读取DS1302中的年,月,日,星期几,小时,分钟和秒.voidReadDS1302(void){unsignedcharbuff;//将读取的数据送入缓存数组,住输
7、出数据为16进制buff=ReadByte(Read_Yea);//年scanbuff[10]=buff/16;//十位scanbuff[11]=buff%16;//个位buff=ReadByte(Read_Mon);//月scanbuff[6]=buff/16;//十位scanbuff[7]=buff%16;//个位buff=ReadByte(Read_Dat);//日scanbuff[8]=buff/16;//十位scanbuff[9]=b