In this repository, i share a STM32 Firmware driver for the BU9873 RTC IC. I also supply the datasheet document provided by the manufacturer with notes i did that were important for the initialization of the RTC IC. You are also going to find instructions to use the code with a initialization example.
In depth tutorial video available on my youtube channel: https://www.youtube.com/watch?v=8UzbhbCSYwc
Note: if there is an issue with the code, let me know!
Firmware driver for the BU9873 RTC IC: - rtc_init() - set_time() - set_date() - get_time() - get_date() - set_alarm_A() - reset_alarm_A() - disable_alarm_A() - get_alarm_pin() (Alarm pin check in a polling mode) - binary_to_bcd() - bcd_to_binary()
Instructions:
-
Initialize RTC by passing the Rtc, I2C_HandleTypeDef and GPIO(PORT/PIN) struct addresses to rtc_init()
-
Pass Rtc struct address, second, minute and hour uint8_t values to set_time() and initialize IC Time registers
-
Pass Rtc struct address, day, month, year and weekday struct values to set_date() and initialize IC Date registers
-
Use get_time() to Write current time IC values to Time struct and access the values using the Rtc.Time.X where X is sec/min/hour values
-
Use get_date() to Write current date IC values to Date struct and access the values using the Rtc.Date.X where X is day/month/year/weekday values
-
Use set_alarm() to enable alarm and write desired time values to alarm IC registers
-
Use reset_alarm() to reset alarm FLG reg that triggers the interrupt pin
-
Use disable_alarm() to disable alarm completely
-
Use get_alarm_pin() to read interrupt pin (Use function in polling mode)
-
binary_to_bcd() and bcd_to_binary() are used for data conversion (IC needs BCD values)
NOTE: set_alarm_A() weekdays parameter is 7bit truth table like number from 0b0000000 (no alarm) to 0b1111111 (every day alarm).
Example: to set alarm for TUE and FRI, set weekday parameter to 0b0100100
Example Initialization:
rtc_init(&rtc, &hi2c1, RTC_INT_GPIO_Port, RTC_INT_Pin);
set_time(&rtc, 40, 59, 23);
set_date(&rtc, 2, 1, 1, 1);
set_alarm_A(&rtc, 0, 0, 0b001110);
while (1)
{
get_time(&rtc);
get_date(&rtc);
if (get_alarm_pin(&rtc) == 1){
HAL_Delay(5000);
reset_alarm_A(&rtc);
}
}