DS1302引脚
DS1302可供操作的引脚有5,6,7号引脚,在蓝桥杯比赛中,底层文件已经提供,我们需要在DS1302.h文件中定义这5,6,7号引脚,翻阅原理图可知:
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
DS1302寄存器
DS1302寄存器图如上
翻阅芯片手册可知,DS1302存在写保护位WP,因此在对DS1302时钟进行任何写操作前应先清除WP保护位。
DS1302读写操作
通过配置寄存器,我们可以对ds1302进行读写操作,其中Write_Ds1302_Byte()与Read_Ds1302_Byte()函数均为竞赛给出的底层代码。
我们需要先定义一个数组来存放我们需要芯片处理的时间数据,例如需要处理时-分-秒的时候我们需要先定义一个unsigned char ucRTC[3] = {0x22,0x55,0x30},将0x22,0x55,0x33分别写入0x84,0x82,0x80处,写操作始末应注意WP保护位。(暂时没搞懂为什么这里要用十六进制)
具体代码如下:
void RTC_Set(unsigned char* ucRTC)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,ucRTC[0]); //时
Write_Ds1302_Byte(0x82,ucRTC[1]); //分
Write_Ds1302_Byte(0x80,ucRTC[2]); //秒
Write_Ds1302_Byte(0x8e,0x80);
}
void RTC_Read(unsigned char* ucRTC)
{
ucRTC[0] = Read_Ds1302_Byte(0x85); //时
ucRTC[1] = Read_Ds1302_Byte(0x83); //分
ucRTC[2] = Read_Ds1302_Byte(0x81); //秒
}
然后就可以在主程序的数据处理函数处使用RTC_Read(ucRTC)来实时读出DS1302中的数据,再配合数码管等外设进行显示了,注意在使用数码管显示时要明确数据是十六进制,因此应该做相对应的处理如/16,%16进行对应显示了。
具体代码如下:
/* 信息处理函数 */
void Seg_Proc()
{
if(Seg_Slow_Down) return;
Seg_Slow_Down = 1;
/* 信息读取区域 */
/* DS1302时钟 */
RTC_Read(ucRTC);
/* DS18B20温度 */
// temperature = Temperature_Read();
/* 数据处理区域 */
/* DS1302时钟 */
Seg_Buf[0] = ucRTC[0] / 16; //时第一位
Seg_Buf[1] = ucRTC[0] % 16;
Seg_Buf[2] = 11;
Seg_Buf[3] = ucRTC[1] / 16; //分第一位
Seg_Buf[4] = ucRTC[1] % 16;
Seg_Buf[5] = 11;
Seg_Buf[6] = ucRTC[2] / 16; //秒第一位
Seg_Buf[7] = ucRTC[2] % 16;
/*DS18B20温度 */
// Seg_Buf[0] = (unsigned char)temperature / 10 % 10;
// Seg_Buf[1] = (unsigned char)temperature % 10;
// Seg_Point[1] = 1;
// Seg_Buf[2] = (unsigned int)(temperature * 100) % 10;
// Seg_Buf[3] = (unsigned int)(temperature * 10) % 10;
}
以上仅作为自己学习记录,如有错误欢迎指正。