📄 rtc.c
字号:
#include "RTC.h"
volatile UINT currentYear;
volatile UINT currentMonth;
volatile UINT currentDay;
volatile UINT currentHour;
volatile UINT currentMinute;
volatile UINT currentSecond;
void ReadR2025(void)
{
BYTE ReadData[4];
I2C_ReceiveData(RTC2025_IIC_ADDRESS_R, ReadData, 4);
printf("ConReg1: 0x%X",ReadData[0]);
printf("Second: %d", BCDToDec(ReadData[1]));
printf("Minute: %d", BCDToDec(ReadData[2]));
printf("Hour : %d\n", BCDToDec(ReadData[3]));
}
BYTE BCDToDec(BYTE bcd_number)
{
return((bcd_number>>4)*10 + (bcd_number&0x0f));
}
BYTE DECToBCD(BYTE dec_number)
{
return(((dec_number/10)<<4) | (dec_number%10));
}
/*****************************************************
* Actual RTC functions *
*****************************************************/
/* setup RTC R2025S/D control register */
/* format: Write_Address, Internal_Pointer, ConReg1, ConReg2*/
/* call: PowerONSetRTCReg(0x20,0x10); is OK!!!!!!! */
void PowerONSetRTCReg(BYTE ConReg1, BYTE ConReg2)
{
BYTE WirteData[3];
WirteData[0] = INTERNAL_POINTER_ADDRESS;
WirteData[1] = ConReg1;
WirteData[2] = ConReg2;
I2C_SendDataNoStop(RTC2025_IIC_ADDRESS_W, WirteData, sizeof(WirteData));
}
/* IIC format:Write_Address, Internal_Poniter, re-Start, Read_Address, second, minute, hour*/
void GetSysTime(void)
{
BYTE WirteData[1];
BYTE ReadData[3];
WirteData[0] = READ_CUR_TIME_ADDRESS;
I2C_SendDataNoStop(RTC2025_IIC_ADDRESS_W, WirteData, sizeof(WirteData));
I2C_ReceiveData(RTC2025_IIC_ADDRESS_R, ReadData, sizeof(ReadData));
currentSecond = BCDToDec(ReadData[0]);
currentMinute = BCDToDec(ReadData[1]);
currentHour = BCDToDec(ReadData[2]);
#ifdef TO_DEBUG_CUR_TIME
PrintCurTime();
#endif
}
#ifdef TO_DEBUG_CUR_TIME
void PrintCurTime(void)
{
printf("H:%2d", currentHour);
printf("M:%2d", currentMinute);
printf("S:%2d\n", currentSecond);
}
void PrintCurDate(void)
{
printf("Y:%2d", currentYear);
printf("M:%2d", currentMonth);
printf("D:%2d\n", currentDay);
}
#endif
/* notice: entry parameters are DECIMAL */
void SetSysTime(UINT hours,UINT minutes,UINT seconds)
{
BYTE WirteData[4];
WirteData[0] = WIRTE_TIME_ADDRESS;
WirteData[1] = DECToBCD(seconds);
WirteData[2] = DECToBCD(minutes);
WirteData[3] = DECToBCD(hours);
I2C_SendDataNoStop(RTC2025_IIC_ADDRESS_W, WirteData, sizeof(WirteData));
}
void GetSysDate(void)
{
BYTE WirteData[1];
BYTE ReadData[3];
WirteData[0] = READ_CUR_DATE_ADDRESS;
I2C_SendDataNoStop(RTC2025_IIC_ADDRESS_W, WirteData, sizeof(WirteData));
I2C_ReceiveData(RTC2025_IIC_ADDRESS_R, ReadData, sizeof(ReadData));
currentDay = BCDToDec(ReadData[0]);
currentMonth = BCDToDec(ReadData[1]&0x1F);
currentYear = BCDToDec(ReadData[2]);
#ifdef TO_DEBUG_CUR_TIME
PrintCurDate();
#endif
}
/* notice: entry parameters are DECIMAL */
void SetSysDate(UINT years,UINT months,UINT days)
{
BYTE WirteData[4];
WirteData[0] = WIRTE_DATE_ADDRESS;
WirteData[1] = DECToBCD(days);
WirteData[2] = DECToBCD(months&0x1F);
WirteData[3] = DECToBCD(years);
I2C_SendDataNoStop(RTC2025_IIC_ADDRESS_W, WirteData, sizeof(WirteData));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -