目录
①拷贝官方底层代码到自己的文件夹(并ADD DS1302.c文件)
(1T、12T不用修改)
①拷贝官方底层代码到自己的文件夹(并ADD DS1302.c文件)
②代码
#include "reg52.h"
#include "ds1302.h"
unsigned char code Read_DS1302_adrr[7] = {
0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};//定义读(奇)操作的日历时钟存储器地址
unsigned char code Write_DS1302_adrr[7] = {
0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};//定义写(偶)操作的日历时间存储器地址
//20年4月18日,周六,23点59分24秒
unsigned char Timer[7] = {0x24,0x59,0x23,0x18,0x04,0x06,0x20};
// 秒 分 时 日 月 星期 年
//*********************************************************************
void DS1302_Config()//初始化日历时钟(设定时间参数)
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);//允许向内存写(偶)入数据(WP为0 可写)
for (i = 0; i < 7; i++)//写7个字节的时间参数
{
Write_Ds1302_Byte(Write_DS1302_adrr[i],Timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80);//禁止向内存写(偶)入数据(WP为1 禁写)
}
void Read_DS1302_Time()//读取日历时间(写数据有保护(第八位),读数据没有)
{
unsigned char i;
for (i = 0; i < 7; i++)
{
Timer[i] = Read_Ds1302_Byte (Read_DS1302_adrr[i]);
}
}
//*********************************************************************
unsigned char code SMG_duanma[18] = {
// 0 1 2 3 4 5 6 7
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
// 8 9 A10 B11 C12 D13 E14 F15
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
// -16 .17
0xbf,0x7f}; //共阳数码管
void InitHC138(unsigned char n)
{
switch (n)
{
case 4://LED
P2 = (P2 & 0x1f) | 0x80;
break;
case 5://蜂鸣器与继电器
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6://数码管位置
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7://数码管段码
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void SMG_Bit(unsigned pos, unsigned char dat)
{
InitHC138(6);
P0 = 0x01 << pos;
InitHC138(7);
P0 = dat;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
void ShowSMG()//数码管动态显示(时-分-秒)
{
SMG_Bit(0, SMG_duanma[Timer[2]/16]);
DelaySMG(500);
SMG_Bit(1, SMG_duanma[Timer[2]%16]);
DelaySMG(500);
SMG_Bit(2, SMG_duanma[16]);
DelaySMG(500);
SMG_Bit(3, SMG_duanma[Timer[1]/16]);
DelaySMG(500);
SMG_Bit(4, SMG_duanma[Timer[1]%16]);
DelaySMG(500);
SMG_Bit(5, SMG_duanma[16]);
DelaySMG(500);
SMG_Bit(6, SMG_duanma[Timer[0]/16]);
DelaySMG(500);
SMG_Bit(7, SMG_duanma[Timer[0]%16]);
DelaySMG(500);
}
void main()
{
DS1302_Config();
while(1)
{
Read_DS1302_Time();
ShowSMG();
}
}