|
分享一個(gè)51單片機(jī)DS1302的時(shí)鐘電路(串行傳輸顯示方式),下面是原理圖:
0.png (31.82 KB, 下載次數(shù): 44)
下載附件
2016-10-10 12:58 上傳
0.png (53.97 KB, 下載次數(shù): 29)
下載附件
2016-10-10 12:58 上傳
仿真工程文件及所有完整程序等資料下載地址:
http://www.torrancerestoration.com/bbs/dpj-56298-1.html
單片機(jī)源程序:
- /********************************************************************
- 天馬電子
- *********************************************************************/
- #include <reg52.h>
- #include <stdio.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //寫(xiě)的時(shí)候,是低電平改變數(shù)據(jù),上升沿送入數(shù)據(jù)
- //讀的時(shí)候,是低電平讀入數(shù)據(jù),下降沿使得從設(shè)備把數(shù)據(jù)放到總線(xiàn)上
- //讀的時(shí)候,是低電平讀,但沒(méi)有讀出,提供下降沿使得從設(shè)備把數(shù)據(jù)放到總線(xiàn)上
- sbit P11=P1^1; //定時(shí)和時(shí)鐘切換
- sbit P12=P1^2; //時(shí)調(diào)整
- sbit P13=P1^3; //分調(diào)整
- sbit P14=P1^4; //秒調(diào)整
- sbit CLK =P3^5;
- sbit IO =P3^6;
- sbit RST =P3^7;
- uchar m,m10,f,f10,s,s10;
- unsigned char data discode[10]={0XFC,0X60,0XDA,0XF2,0X66,0XB6,0XBE,0XE0,0XFE,0XF6};//共陰串行數(shù)組代表0~9
- unsigned char leddis[6]={1,2,3,4,5,6};//顯存,有幾個(gè)數(shù)碼管可以定義幾個(gè)
- /***************************延時(shí)函數(shù)*************************************/
- del() /*延時(shí)0.2秒*/
- {
- unsigned int i,j,k;
- for(i=100;i>0;i--)
- for(j=20;j>0;j--)
- for(k=248;k>0;k--);
- }
- /****************************************************************/
- /////////////////////DS1302////////////////////////
- //--------------WriteOneByteData-----------------/
- void WriteByte(uchar ucda)
- {
- uchar i;
- for(i=8;i>0;i--)
- {
- IO=(bit)(ucda&0x01);
- CLK=1;
- CLK=0;
- ucda>>=1;
- }
- }
- //--------------ReadOneByteData-----------------/
- uchar ReadByte(void)
- {
- uchar i,a;
- for(i=8;i>0;i--)
- {
- a>>=1;
- if(IO)a|=0x80;
- CLK=1;
- CLK=0;
- }
- return(a);
- }
- //--------------WriteAddrFist,ThenWriteData-----------------/
- void write1302(uchar Addr,uchar Data)
- {
- RST=0;
- CLK=0;
- RST=1;
- WriteByte(Addr);
- WriteByte(Data);
- CLK=1;
- RST=0;
- }
- //--------------WriteAddrFirst,ThenReadData-----------------/
- uchar read1302(uchar Addr)
- {
- uchar Data;
- RST=0;
- CLK=0;
- RST=1;
- WriteByte(Addr);
- Data=ReadByte();
- CLK=1;
- RST=0;
- return(Data);
- }
- /************************************************************************/
- ////////////////ds1302初始化//////////////////調(diào)整時(shí)間時(shí)用
- void csh1302(void)
- {
- CLK=0;
- RST=0;
- write1302(0x8e,0x00);//打開(kāi)寫(xiě)保護(hù)
- write1302(0x90,0x06);//禁止充電
- /////////////////////////////////////////////////
- write1302(0x80,0x20);//預(yù)置秒
- write1302(0x82,0x30);//預(yù)置分
- write1302(0x84,0x08);//預(yù)置時(shí)
- write1302(0x86,0x22);//預(yù)置日期
- write1302(0x88,0x07);//預(yù)置月
- //write1302(0x8a,0x02);//預(yù)置星期
- write1302(0x8c,0x08);//預(yù)置年
- ////////////////////////////////////////////////
- write1302(0x80,0x00);//啟動(dòng)時(shí)鐘
- write1302(0x8e,0x80);//關(guān)閉寫(xiě)保護(hù)
- }
- /************************************************************************/
- //////////////////顯示函數(shù)////////////////////////////
- void xianshi(void)
- {
- s10=read1302(0x85);
- s=read1302(0x85)&0x0f; //時(shí)
- s10=s10>>4;
- s10=s10&0x07;
- ////////////////////////////////////
- f10=read1302(0x83); //分
- f=read1302(0x83)&0x0f;
- f10=f10>>4;
- f10=f10&0x07;
- ////////////////////////////////////
- m10=read1302(0x81); //秒
- m=read1302(0x81)&0x0f;
- m10=m10>>4;
- m10=m10&0x07;
- }
- /**************************************************************************/
- void display(void) //數(shù)碼管顯示函數(shù)
- {
- unsigned char count;//數(shù)碼管個(gè)數(shù)
-
- for (count=6;count>0;count--)
- {
- SBUF=discode[leddis[count-1]];
- while(TI==0);
- TI=0;
- }
- }
- void show(void) //顯示時(shí)間函數(shù)
- {
- xianshi();
- leddis[0]=s10;
- leddis[1]=s;
- leddis[2]=f10;
- leddis[3]=f;
- leddis[4]=m10;
- leddis[5]=m;
-
- display(); // 顯示函數(shù)
- }
- /************************************************************************
- 主函數(shù)
- ************************************************************************/
- void main()
- {
- SCON=0x00; //設(shè)定UART的工作方式為MODEO
-
- csh1302(); //初始化
- while(1)
- {
- show();
- del();
- }
- }
復(fù)制代碼
|
|