123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #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));
- }
|