//實驗名稱:DS1302電子時鐘
//實驗現象:1、將DS1302 的底層驅動代碼文件正確移植到工程中。
//2、初始化DS1302的默認啟動參數為:2024年3月2日 星期六 20時28分29秒
//3、系統上電后,DS1302實時時鐘從默認參數啟動運行,并將當前的時、分、秒顯示在數碼管上,時分秒之間用“-”分隔。
// 數碼管顯示:秒-分-時
//注意:(1)采用IO擴展外部內存時,需要將電路版中的模式調為MM模式
// (2)添加比賽的底層驅動模塊時,需要檢查兩點,一是參數、函數是否完整;二是驅動函數頻率和芯片頻率是否一致,如果不一致注意延時函數倍數關系
#include<reg52.h>
#include<ds1302.h>
#include<absacc.h>
#include<smg_active.h>
unsigned char write_ds1302[8]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e};
unsigned char read_ds1302[8]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d,0x8f};
//2024年3月2日 星期六 20時28分29秒 數碼管先顯示1分鐘2024 2024 然后03-02-20-28
unsigned char timer_ds1302[8]={0x29,0x28,0x20,0x02,0x03,0x06,0x24,0x20}; //采用BCD碼,即一個數字對應八個二進制數碼
//所以對于十進制的8421BCD來說,就是十六進制
void init_ds1302() //初始化DS1302
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00); //在底層驅動模塊中有,
for(i=0;i<7;i++)
{
Write_Ds1302_Byte( write_ds1302[i],timer_ds1302[i]); // 依次讀取數組中內容
}
Write_Ds1302_Byte(0x8e,0x80);
}
void read_time_ds1302()
{
unsigned char j;
for(j=0;j<7;j++)
{
timer_ds1302[j]=Read_Ds1302_Byte ( read_ds1302[j]);
}
}
void time_show()
{
SMG_single(7,SMG_nodot[timer_ds1302[0]%16]); //秒,數組中數據是十六進制,將其轉化為十進制
delay(200);
SMG_0xff();
SMG_single(6,SMG_nodot[timer_ds1302[0]/16]);
delay(200);
SMG_0xff();
SMG_single(5,0xbf); //-
delay(200);
SMG_0xff();
SMG_single(4,SMG_nodot[timer_ds1302[1]%16]); //分
delay(200);
SMG_0xff();
SMG_single(3,SMG_nodot[timer_ds1302[1]/16]);
delay(200);
SMG_0xff();
SMG_single(2,0xbf); //-
delay(200);
SMG_0xff();
SMG_single(1,SMG_nodot[timer_ds1302[2]%16]); //時
delay(200);
SMG_0xff();
SMG_single(0,SMG_nodot[timer_ds1302[2]/16]);
delay(200);
SMG_0xff();
}
void main()
{
init_ds1302();
while(1)
{
read_time_ds1302();
time_show();
}
}
*******************DS1302電子時鐘***********************
|