|
51單片機DS1302的時鐘電路(并行傳輸顯示方式)的proteus仿真,下面是原理圖:
仿真工程文件及所有完整程序等資料下載地址:
http://www.torrancerestoration.com/bbs/dpj-56298-1.html
單片機源程序如下:
- /********************************************************************
- 天馬
- *********************************************************************/
- #include <reg52.h>
- #include <stdio.h>
- #include<intrins.h>
- #define uchar unsigned char//宏定義無符號字符型
- #define uint unsigned int //宏定義無符號整型
- //寫的時候,是低電平改變數(shù)據(jù),上升沿送入數(shù)據(jù)
- //讀的時候,是低電平讀入數(shù)據(jù),下降沿使得從設(shè)備把數(shù)據(jù)放到總線上
- //讀的時候,是低電平讀,但沒有讀出,提供下降沿使得從設(shè)備把數(shù)據(jù)放到總線上
- sbit P11=P1^1; //定時和時鐘切換
- sbit P12=P1^2; //時調(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;
- /********************************************************************
- 初始定義
- *********************************************************************/
- code uchar seg7code[10]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //顯示段碼 數(shù)碼管字跟
- uchar wei[6]={0Xfb,0Xf7,0XEf,0XDf,0XBf,0X7f}; //位的控制端
- /********************************************************************
- 延時函數(shù)
- *********************************************************************/
- void delay(uchar t)
- {
- uchar i,j;
- for(i=0;i<t;i++)
- {
- for(j=13;j>0;j--);
- { ;
- }
- }
- }
- /********************************************************************
- 顯示函數(shù)
- *********************************************************************/
- void Led() //顯示函數(shù)
- {
- /*****************數(shù)據(jù)轉(zhuǎn)換*****************************/
- P2=0XFF;
- P0=seg7code[s10];
- P2=wei[0];
- delay(80);
- P2=0XFF;
- P0=seg7code[s];
- P2=wei[1];
- delay(80);
- P2=0XFF;
- P0=seg7code[f10];
- P2=wei[2];
- delay(80);
- P2=0XFF;
- P0=seg7code[f];
- P2=wei[3];
- delay(80);
- P2=0XFF;
- P0=seg7code[m10];
- P2=wei[4];
- delay(80);
- P2=0XFF;
- P0=seg7code[m];
- P2=wei[5];
- delay(80);
- P2=0XFF;
- }
- /****************************************************************
- DS1302函數(shù)
- ****************************************************************/
- /////////////////////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)整時間時用
- void csh1302(void)
- {
- CLK=0;
- RST=0;
- write1302(0x8e,0x00);//打開寫保護(hù)
- write1302(0x90,0x06);//禁止充電
- /////////////////////////////////////////////////
- write1302(0x80,0x00);//預(yù)置秒
- write1302(0x82,0x12);//預(yù)置分
- write1302(0x84,0x09);//預(yù)置時
- write1302(0x86,0x22);//預(yù)置日期
- write1302(0x88,0x07);//預(yù)置月
- //write1302(0x8a,0x02);//預(yù)置星期
- write1302(0x8c,0x08);//預(yù)置年
- ////////////////////////////////////////////////
- write1302(0x80,0x00);//啟動時鐘
- write1302(0x8e,0x80);//關(guān)閉寫保護(hù)
- }
- /************************************************************************/
- //////////////////顯示函數(shù)////////////////////////////
- void xianshi(void)
- {
- s10=read1302(0x85);
- s=read1302(0x85)&0x0f; //時
- 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;
- }
- /********************************************************************
- 主函數(shù)
- *********************************************************************/
- void main()
- {
- csh1302(); //初始化
- while(1)
- {
- xianshi();
- Led();//調(diào)用顯示函數(shù)顯示數(shù)據(jù)display_date
- }
- }
-
- /********************************************************************
- 結(jié)束
- *********************************************************************/
復(fù)制代碼
|
|