標(biāo)題: DS1302時鐘+數(shù)碼管proteus仿真 [打印本頁]

作者: ccxzczx    時間: 2019-12-15 20:57
標(biāo)題: DS1302時鐘+數(shù)碼管proteus仿真
#include"reg51.h"
#include"intrins.h"
#define uchar unsigned char
uchar time1[8];
code seven_tab[15]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0--9,-,
sbit rtc_clk=P3^6;  //ds1302的串行時鐘,控制數(shù)據(jù)的輸入與輸出
sbit rtc_data=P3^4;  //雙向數(shù)據(jù)線
sbit rtc_rst=P3^5;  //復(fù)位引腳
sbit p2_2=P2^2;
sbit p2_3=P2^3;
sbit p2_4=P2^4;
       /*ds1302讀,寫都要先執(zhí)行讀地址
         ds1302的雙向數(shù)線是以為一位執(zhí)行
    */
void write1302(uchar address,uchar date)    //向ds1302寫
{
   uchar n;
   rtc_rst=0;  //復(fù)位引腳低電平
   _nop_();      //延遲一個周期
   rtc_clk=0;  //時鐘為低電平
   _nop_();
   rtc_rst=1;  //復(fù)位引腳置高,開始執(zhí)行
   _nop_();
  for(n=0;n<8;n++)           //開始傳送八位地址命令
  {
     rtc_data=address&0x01;   //  數(shù)據(jù)從低位開始傳送
  address>>=1;       //地址數(shù)據(jù)向右移一位
  rtc_clk=1;       //串行時鐘拉高,執(zhí)行寫入數(shù)據(jù)
  _nop_();
  rtc_clk=0;     //串行時鐘拉高,為下一個數(shù)據(jù)寫入做準(zhǔn)備
  _nop_();
  }
  for(n=0;n<8;n++)          //開始傳送八位地址數(shù)據(jù)
  {
     rtc_data=date&0x01;      //數(shù)據(jù)從低位開始傳送
  date>>=1;           //地址數(shù)據(jù)向右移一位
  rtc_clk=1;   
  _nop_();
  rtc_clk=0;
  _nop_();
  }
  rtc_rst=0;      //結(jié)束復(fù)位拉低
  _nop_();
}
uchar read1302(uchar address)  //向ds1302讀
{
   uchar n,dat,dat1;
   rtc_rst=0;
   _nop_();
   rtc_clk=0;
   _nop_();
   rtc_rst=1;
   _nop_();
   for(n=0;n<8;n++)              //開始傳送八位地址命令
   {
        rtc_data=address&0x01;   //數(shù)據(jù)從低位開始傳送
  address>>=1;       //地址數(shù)據(jù)向右移一位
  rtc_clk=1;   
  _nop_();
  rtc_clk=0;
  _nop_();
   }
    _nop_();
   for(n=0;n<8;n++)             //讀取8位數(shù)據(jù)
   {
     dat1=rtc_data;
  dat=(dat>>1)|(dat1<<7);
  rtc_clk=1;
  _nop_();
  rtc_clk=0;
  _nop_();
   }
   rtc_rst=0;
    _nop_();
rtc_clk=1;
_nop_();
rtc_data=0;
_nop_();
rtc_data=1;
_nop_();   
return dat;
}

ea5af1af5d3df22ab5e376c3b282c63.png (74.76 KB, 下載次數(shù): 64)

ea5af1af5d3df22ab5e376c3b282c63.png

ds1302時鐘.rar

26.15 KB, 下載次數(shù): 43, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1