找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2187|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

數(shù)碼管萬年歷的一個(gè)程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:119266 發(fā)表于 2016-5-8 13:56 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

#include < reg52.h>

  #define uchar unsigned char

sbit ACC_0 = ACC^0;
sbit ACC_1 = ACC^1;
sbit ACC_2 = ACC^2;
sbit ACC_3 = ACC^3;
sbit ACC_4 = ACC^4;
sbit ACC_5 = ACC^5;
sbit ACC_6 = ACC^6;
sbit ACC_7 = ACC^7;
//**************************
sbit T_SCLK = P3^5;                    // DS1302時(shí)鐘信號(hào)       7腳
sbit T_DIO= P3^6;                      // DS1302數(shù)據(jù)信號(hào)       6腳
sbit T_CE = P3^7;
//**************************
uchar  sec,min,hour,day,month,week,year;   //時(shí)間變量
//**************************
uchar  DS1302_addr[]={
      0x80, //0,寫入秒(Second)寄存器
      0x81, //1,讀出秒(Second)寄存器
      0x82, //2,寫入分(Minute)寄存器
      0x83, //3,讀出分(Minute)寄存器
      0x84, //4,寫入小時(shí)(Hour)寄存器
      0x85, //5,讀出小時(shí)(Hour)寄存器
      0x86, //6,寫入日(Day)寄存器
      0x87, //7,讀出日(Day)寄存器
      0x88, //8,寫入月份(Month)寄存器
      0x89, //9,讀出月份(Month)寄存器
      0x8a, //10,寫入周(Week)寄存器
      0x8b, //11,讀出周(Week)寄存器
      0x8c, //12,寫入年份(Year)寄存器
      0x8d, //13,讀出年份(Year)寄存器
      0x8e, //14,寫保護(hù)_寄存器
      0x8f  //15,涓流充電

}  ;

//***************寫入一字節(jié)***********************
void DS1302_Input_Byte(uchar Input)  //向時(shí)鐘IC寫入一字節(jié)
{   
  uchar i;
    ACC =Input;
    for(i=8; i>0; i--)
    {  
        T_DIO = ACC_0;            //相當(dāng)于匯編中的 RRC
  T_SCLK = 1;
  T_SCLK = 0;
        ACC = ACC >> 1;
    }   
}
//**************讀取一字節(jié)()*************************
uchar DS1302_Output_Byte(void)      //從時(shí)鐘IC讀取一字節(jié)()
{
    uchar i;   
    for(i=8; i>0; i--)
    {  
     ACC>>=1; T_DIO= 1;
     ACC_7 = T_DIO;
   T_SCLK = 1;                 //相當(dāng)于匯編中的 RRC
         T_SCLK = 0;      
    }
    return(ACC);
}
//**************向時(shí)鐘IC寫數(shù)據(jù)*************************
void DS1302_Write_one( uchar addr,dat )       // 寫入地址、數(shù)據(jù)子程序
{
       T_CE=0;                             //T_CE引腳為低,數(shù)據(jù)傳送中止
       T_SCLK=0;                          //清零時(shí)鐘總線
       T_CE = 1;                          //T_CE引腳為高,邏輯控制有效
    DS1302_Input_Byte(addr);           // 地址,命令
       DS1302_Input_Byte(dat);          // 寫1Byte數(shù)據(jù)
       T_SCLK = 1;
       T_CE = 0;     
}
//************從數(shù)據(jù)讀取數(shù)據(jù)*****************************
uchar DS1302_Read ( uchar addr )    //數(shù)據(jù)讀取子程序
{
      uchar date;
       T_CE=0;           
       T_SCLK=0;            
       T_CE = 1;
  DS1302_Input_Byte(addr);        // 地址,命令
     date = DS1302_Output_Byte();         // 讀1Byte數(shù)據(jù)
     T_SCLK = 1;
     T_CE = 0;
     return(date);
}


//************從數(shù)據(jù)讀取數(shù)據(jù)****************************
void DS1302_Write( uchar sec_w,min_w,hour_w,day_w,month_w,week_w,year_w )
{
        DS1302_Write_one(0x8e,0x00);
  DS1302_Write_one(0x80,sec_w);
  DS1302_Write_one(0x82,min_w);
  DS1302_Write_one(0x84,hour_w);
  DS1302_Write_one(0x86,day_w);
  DS1302_Write_one(0x88,month_w);
  DS1302_Write_one(0x8a,week_w);
  DS1302_Write_one(0x8c,year_w);
  DS1302_Write_one(0x8e,0x80);
}



  
//************從數(shù)據(jù)讀取數(shù)據(jù)*****************************
/*void DS1302_readtime()                           
{
    sec=DS1302_Read(0x81);                    //讀秒
    min=DS1302_Read(0x83);                    //讀分
    hour=DS1302_Read(0x85);                   //讀時(shí)
    day=DS1302_Read(0x87);                    //讀日
    month=DS1302_Read(0x89);                  //讀月
    year=DS1302_Read(0x8d);     //讀年
    week=DS1302_Read(0x8b);      //讀星期
}*/
//************數(shù)碼管DS1302顯示*****************************
void DS1302_SMG_display(uchar *p)
{
/*  DS1302_readtime();
  SMG_display(6,sec%0x10);
  delay(1);
  SMG_display(5,sec/0x10);
  delay(1);
  SMG_display(4,min%0x10);
  delay(1);
  SMG_display(3,min/0x10);
   delay(1);
  SMG_display(2,hour%0x10);
  delay(1);
  SMG_display(1,hour/0x10);
  delay(1);
  */

////////////////////////////////////////////
   uchar i=0,a=6;
   p=DS1302_addr;
    a= *(p+1);
   for(i=0;i<3;i++)
   {
     //SMG_display(a,DS1302_Read(*p)%0x10);
   a--;
   //delay(0);
   //SMG_display(a,DS1302_Read(*p)/0x10);
   //delay(0);
   a--;

   }
  
   
}

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表