|
@@ -0,0 +1,228 @@
|
|
|
+#include "Rtcx.h"
|
|
|
+#include "string.h"
|
|
|
+#include "gpio.h"
|
|
|
+#include "stdio.h"
|
|
|
+#include "ac780x.h"
|
|
|
+
|
|
|
+DateTime g_dateTime;
|
|
|
+//DateTime tmpdateTime;
|
|
|
+
|
|
|
+#define ISL1208_IIC_ADDR (0x6F)
|
|
|
+
|
|
|
+
|
|
|
+#define ISL1208_SEC_Reg 0x00
|
|
|
+#define ISL1208_MIN_Reg 0x01
|
|
|
+#define ISL1208_HOUR_Reg 0x02
|
|
|
+#define ISL1208_DATE_Reg 0x03
|
|
|
+#define ISL1208_MONTH_Reg 0x04
|
|
|
+#define ISL1208_YEAR_Reg 0x05
|
|
|
+#define ISL1208_WEEK_Reg 0x06
|
|
|
+#define ISL1208_CFG_Reg 0x07
|
|
|
+
|
|
|
+#define FLAG_Reg (0x12)
|
|
|
+#define FLAG_VAL (0xAA)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static uint8_t bin2bcd(uint8_t bin)
|
|
|
+{
|
|
|
+ return (((bin/10) << 4) + bin%10);
|
|
|
+}
|
|
|
+
|
|
|
+static uint8_t bcd2bin(uint8_t bcd)
|
|
|
+{
|
|
|
+ return ((bcd/16)*10 + bcd%16);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+* @prototype I2C_ParaInit(void)
|
|
|
+*
|
|
|
+* @param[in] void
|
|
|
+* @return void
|
|
|
+*
|
|
|
+* @brief 初始化I2C.
|
|
|
+*/
|
|
|
+void I2C_ParaInit(void)
|
|
|
+{
|
|
|
+ I2C_ConfigType i2cConfig;
|
|
|
+
|
|
|
+ /*初始化引脚功能为I2C.*/
|
|
|
+ //GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
|
|
|
+ //GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);
|
|
|
+
|
|
|
+ /*清零结构体变量.*/
|
|
|
+ memset(&i2cConfig, 0x00, sizeof(i2cConfig));
|
|
|
+
|
|
|
+ /*无论是主机还是从机模式都需要配置的参数.*/
|
|
|
+ i2cConfig.mode = I2C_MASTER;/*设置主从机模式.*/
|
|
|
+ i2cConfig.extAddrEn = DISABLE;/*10bit扩展地址禁能.*/
|
|
|
+ i2cConfig.dmaRxEn = DISABLE;/*设置DMA接收数据.*/
|
|
|
+ i2cConfig.dmaTxEn = DISABLE;/*设置DMA发送数据.*/
|
|
|
+ i2cConfig.interruptEn = DISABLE;/*I2C中断,BND/SAMF/ARBLOST.*/
|
|
|
+ i2cConfig.nackInterruptEn = DISABLE;/*NACK中断.*/
|
|
|
+ i2cConfig.ssInterruptEn = DISABLE;/*总线start或stop中断.*/
|
|
|
+ i2cConfig.i2cEn = ENABLE;/*使能模块.*/
|
|
|
+ i2cConfig.callBack = NULL;/*中断回调函数.*/
|
|
|
+
|
|
|
+ /*主机模式需要配置的参数,配置成从机模式可忽略.*/
|
|
|
+ i2cConfig.masterConfig.sampleCnt = 9;/*设置波特率为100Kbps,bandrate=(24M/(10*12*2))=100Kbps.*/
|
|
|
+ i2cConfig.masterConfig.stepCnt = 11;
|
|
|
+ i2cConfig.masterConfig.ARBEn = ENABLE;/*设置主机仲裁功能.*/
|
|
|
+ i2cConfig.masterConfig.SYNCEn = ENABLE;/*设置主机SCL同步功能.*/
|
|
|
+
|
|
|
+ /*从机模式需要配置的参数,配置成主机模式可忽略.*/
|
|
|
+
|
|
|
+ //i2cConfig.slaveConfig.slaveAddr = AT24C02_DEV_ADDR;/*从机地址.*/
|
|
|
+ //i2cConfig.slaveConfig.slaveRangeAddr = 0;/*从机范围地址.*/
|
|
|
+ //i2cConfig.slaveConfig.glitchFilterCnt= 0;/*毛刺过滤.*/
|
|
|
+ //i2cConfig.slaveConfig.stretchEn = DISABLE;/*从机SCL延伸功能.*/
|
|
|
+ //i2cConfig.slaveConfig.rangeAddrEn = DISABLE;/*禁能范围地址.*/
|
|
|
+ //i2cConfig.slaveConfig.monitorEn = DISABLE;/*禁能从机监测功能.*/
|
|
|
+ //i2cConfig.slaveConfig.generalCallEn = DISABLE;/*从机SCL广播地址.*/
|
|
|
+ //i2cConfig.slaveConfig.wakeupEn = DISABLE;/*唤醒功能,仅从机时有效.*/
|
|
|
+ //i2cConfig.slaveConfig.rxOFInterruptEn = DISABLE;/*接收缓存溢出中断.*/
|
|
|
+ //i2cConfig.slaveConfig.txUFInterruptEn = DISABLE;/*发送缓存溢出中断.*/
|
|
|
+
|
|
|
+
|
|
|
+ I2C_Init(I2C1, &i2cConfig);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static uint8_t isl1208_readbyte(uint8_t addr)
|
|
|
+{
|
|
|
+ uint8_t data;
|
|
|
+
|
|
|
+ I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
|
|
|
+ I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, &data, 1);//读取数据
|
|
|
+
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void isl1208_writebyte(uint8_t addr, uint8_t data)
|
|
|
+{
|
|
|
+ uint8_t buffer[2];
|
|
|
+ buffer[0] = addr;
|
|
|
+ buffer[1] = data;
|
|
|
+
|
|
|
+ I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 2, ENABLE);//发送读取地址
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void RTCx_SetTime(DateTime* pDateTime)
|
|
|
+{
|
|
|
+
|
|
|
+ uint8_t addr = 0x00;
|
|
|
+ uint8_t buffer[8];
|
|
|
+
|
|
|
+ pDateTime->year-=2000; //年默认2000年开始
|
|
|
+ if(pDateTime->year > 100) pDateTime->year = 0;
|
|
|
+ pDateTime->week -= 1;
|
|
|
+
|
|
|
+ //cfg = ht1382_readbyte(HT1382_CFG_Reg);
|
|
|
+ buffer[0] = addr;
|
|
|
+
|
|
|
+ buffer[1] = bin2bcd(pDateTime->second);
|
|
|
+ buffer[2] = bin2bcd(pDateTime->minute);
|
|
|
+ buffer[3] = bin2bcd(pDateTime->hour);
|
|
|
+ buffer[3] |= 0x80; //时 24小时模式
|
|
|
+ buffer[4] = bin2bcd(pDateTime->day);
|
|
|
+ buffer[5] = bin2bcd(pDateTime->month);
|
|
|
+ buffer[6] = bin2bcd(pDateTime->year);
|
|
|
+ buffer[7] = bin2bcd(pDateTime->week);
|
|
|
+
|
|
|
+ isl1208_writebyte(ISL1208_CFG_Reg,0x90); //写控制寄存器、 开启写使能、暂停时钟晶振、状态自动复位
|
|
|
+
|
|
|
+ I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 8, ENABLE);//发送读取地址
|
|
|
+
|
|
|
+ printf("RTC_SetTime end \r\n");
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+static void RTCx_GetTime(void)
|
|
|
+{
|
|
|
+
|
|
|
+ uint8_t addr = 0x00;
|
|
|
+ uint8_t data[7];
|
|
|
+
|
|
|
+ I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
|
|
|
+ I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, data, 7);//读取数据
|
|
|
+
|
|
|
+ data[2] = 0x7F&data[2];
|
|
|
+
|
|
|
+
|
|
|
+ //BCD码转十进制
|
|
|
+ g_dateTime.second = bcd2bin(data[0]);
|
|
|
+ g_dateTime.minute = bcd2bin(data[1]);
|
|
|
+ g_dateTime.hour = bcd2bin(data[2]);
|
|
|
+ g_dateTime.day = bcd2bin(data[3]);
|
|
|
+ g_dateTime.month = bcd2bin(data[4]);
|
|
|
+ g_dateTime.year = bcd2bin(data[5])+2000;
|
|
|
+ g_dateTime.week = bcd2bin(data[6])+1;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void RTCx_UpdateTime(void)
|
|
|
+{
|
|
|
+ RTCx_GetTime();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void RTCx_PrintDateTime(void)
|
|
|
+{
|
|
|
+ RTCx_GetTime();
|
|
|
+ printf("DateTime:[%d][%d][%d] [%d][%d][%d] week[%d] \r\n",g_dateTime.year, g_dateTime.month, g_dateTime.day, g_dateTime.hour, g_dateTime.minute, g_dateTime.second, g_dateTime.week);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+*********************************************************************************************************
|
|
|
+* 函 数 名: void DS1307_Init_Time(void)
|
|
|
+* 功能说明: 第一次上电时,需要初始化时间,初始化一次后就不必重复初始化
|
|
|
+* 形 参:无
|
|
|
+* 返 回 值: 无
|
|
|
+*********************************************************************************************************
|
|
|
+*/
|
|
|
+void RTCx_Init(void)
|
|
|
+{
|
|
|
+
|
|
|
+ I2C_ParaInit();
|
|
|
+
|
|
|
+ if(FLAG_VAL != isl1208_readbyte(FLAG_Reg))//如果未初始化,执行初始化时间
|
|
|
+ {
|
|
|
+ g_dateTime.year = 2024;
|
|
|
+ g_dateTime.month = 7;
|
|
|
+ g_dateTime.day = 12;
|
|
|
+ g_dateTime.hour = 11;
|
|
|
+ g_dateTime.minute = 56;
|
|
|
+ g_dateTime.second = 20;
|
|
|
+ g_dateTime.week = 5;
|
|
|
+
|
|
|
+ RTCx_SetTime(&g_dateTime);
|
|
|
+
|
|
|
+ isl1208_writebyte(FLAG_Reg, FLAG_VAL);
|
|
|
+
|
|
|
+ } else{
|
|
|
+
|
|
|
+ RTCx_PrintDateTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void RTCx_DeInit(void)
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|