#include "ds3231.h" #include "i2c.h" /** * @brief Инициализация RTC */ void RTC_Init(void) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_CONTROL_ADDR); I2C_WriteByte(DS3231_CONV); I2C_Stop(); } /** * @brief Чтение времени и календаря */ void RTC_ReadAll(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR); I2C_Stop(); I2C_Start(); I2C_WriteByte(DS3231_I2C_RDADDR); I2C_ReadByte(&data->Sec, TWI_ACK); I2C_ReadByte(&data->Min, TWI_ACK); I2C_ReadByte(&data->Hr, TWI_ACK); I2C_ReadByte(&data->WD, TWI_ACK); I2C_ReadByte(&data->Day, TWI_ACK); I2C_ReadByte(&data->Mon, TWI_ACK); I2C_ReadByte(&data->Year, TWI_NACK); I2C_Stop(); } /** * @brief Чтение времени */ void RTC_ReadTime(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR); I2C_Stop(); I2C_Start(); I2C_WriteByte(DS3231_I2C_RDADDR); I2C_ReadByte(&data->Sec, TWI_ACK); I2C_ReadByte(&data->Min, TWI_ACK); I2C_ReadByte(&data->Hr, TWI_NACK); I2C_Stop(); } /** * @brief Чтение календаря */ void RTC_ReadCalendar(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_CALENDAR_ADDR); I2C_Stop(); I2C_Start(); I2C_WriteByte(DS3231_I2C_RDADDR); I2C_ReadByte(&data->WD, TWI_ACK); I2C_ReadByte(&data->Day, TWI_ACK); I2C_ReadByte(&data->Mon, TWI_ACK); I2C_ReadByte(&data->Year, TWI_NACK); I2C_Stop(); } /** * @brief Запись времени и календаря */ void RTC_WriteAll(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR); I2C_WriteByte(data->Sec); I2C_WriteByte(data->Min); I2C_WriteByte(data->Hr); I2C_WriteByte(data->WD); I2C_WriteByte(data->Day); I2C_WriteByte(data->Mon); I2C_WriteByte(data->Year); I2C_Stop(); } /** * @brief Запись времени */ void RTC_WriteTime(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR); I2C_WriteByte(data->Sec); I2C_WriteByte(data->Min); I2C_WriteByte(data->Hr); I2C_Stop(); } /** * @brief Запись часов */ void RTC_WriteHH(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR + 2); I2C_WriteByte(data->Hr); I2C_Stop(); } /** * @brief Запись часов и минут */ void RTC_WriteHHMM(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_TIME_CAL_ADDR + 1); I2C_WriteByte(data->Min); I2C_WriteByte(data->Hr); I2C_Stop(); } /** * @brief Запись календаря */ void RTC_WriteCalendar(rtc_t * data) { I2C_Start(); I2C_WriteByte(DS3231_I2C_WRADDR); I2C_WriteByte(DS3231_CALENDAR_ADDR); I2C_WriteByte(data->WD); I2C_WriteByte(data->Day); I2C_WriteByte(data->Mon); I2C_WriteByte(data->Year); I2C_Stop(); } /** * @brief Convert BCD value to Binary */ uint8_t bcd2bin(uint8_t bcd) { return (10 * (bcd >> 4) + (bcd & 0x0f)); } /** * @brief Convert Binary value to BCD */ uint8_t bin2bcd(uint8_t bin) { return (((bin / 10 ) << 4) | (bin % 10)); }