A digital clock and calendar with alarm functionality developed on STM32F401 (ARM Cortex-M4) microcontroller using DS1307 RTC for time keeping. It includes a LCD based user interface to set time, date and alarm using interrupt based multiple push buttons.
- YouTube - https://youtu.be/AXyXmcp8b5Q
- 4 Push buttons to configure and control the system.
- Updated clock and calendar using DS1307 RTC and timer interrupt.
- User configurable clock modes to switch between 12h and 24h with instant time conversion.
- Clock, Calendar and Alarm setting in both clock modes.
- Alarm indicator using Led and Buzzer.
- Automated as well as button based manual control to stop alarm.
- LCD back light control using button.
Overview
Software Architechture
- STM32 F401 Nucleo Microcontroller
- DS1307 RTC Module
- Character LCD 16x2
- Potentiometer 10K
- Push Buttons
- Led
- Buzzer
To program the STM32F401xx microcontroller STM32Cube IDE is used without using the auto generated code. The purpose of using the CubeMX software is to generate the required HAL peripheral drivers for this application. The rest of the code is written manually.
In this application following Peripheral Drivers are used,
HAL Drivers | Description |
---|---|
stm32f4xx_hal_rcc | Implement the HSI clock and reset the peripherals. |
stm32f4xx_hal_gpio | Implement GPIO related features for external components. |
stm32f4xx_hal_i2c | Implement I2C protocol for DS1307 RTC. |
stm32f4xx_hal_timer | Implement timer interrupt to fetch current time and date. |
The external components are integrated with the microcontroller by developing the required device drivers.
Device Drivers | Description |
---|---|
rtc_ds1307 | Implement I2C communication between RTC and STM32 . |
lcd | Integrate LCD with STM32. |
push_buttons | Implement GPIO interrupts for controlling the system. |
led | Integrate LED for alarm functionality. |
buzzer | Integrate Buzzer for alarm functionality. |
The system is controlled and configured using 4 push buttons each of them having different functionality.
- This button is used to toggle between different user interfaces on LCD to set the clock, calendar and alarm.
- When the setting is completed the button returns back the main interface on LCD.
- Toggle the clock mode mode between 12h and 24h formats.
- Set am/pm status of clock & alarm in 12hr clock mode.
- Set the day of the week.
- Stop the alarm when ringing.
- Enable/ Disable alarm (Alarm will not ring when disabled).
- Set the hours value of the clock & alarm.
- Set the date of calendar.
- Enable/ Disable backlight of the LCD.
- Set the minutes value of the clock & alarm.
- Set the month of calendar.
The main interface displays following characteristics,
- Clock mode which displays either '24h>' in 24h format or 'am/pm' in 12h format.
- Ticking time in 'hh:mm:ss' format.
- Calendar in 'day dd/mm' format.
- Alarm enable/disable indicator which displays 'AL>' when enabled.
- This interface enables the user to set the hours and mins value.
- If the clock mode is 12h then there is an option to toggle between am/pm.
- This interface enables the user to set the day, date and week.
- This interface enables the user to set the hours and mins value.
- If the clock mode is 12h then there is an option to toggle between am/pm.
- It remembers the previous set values enabling the user to know the exact time the alarm is set on.
- To indicate the alarm, a buzzer and led is integrated which toggles on and off every second until the alarm stops.
- The alarm will stop automatically after 30sec or it can be stopped manually by using the dedicated push button.
The most important part of the clock is keep track of the time and date once the they are set by the user.
For this purpose an external RTC module DS1307 is integrated with the microcontroller using Inter-Integrated Circuit (I2C) communication protocol.
I2C operates in 2 modes โ Master mode and Slave Mode. In this application, STM32F401 acts as the master and DS1307 is the slave.The slave and master are connected using SDA and SCL lines where,
- SDA (Serial Data) : Used to read from and write to the required addresses of the DS1307 RTC module.
- SCL (Serial Clock) : Carries the clock signal based on which data transfer takes place.
This project makes use of STM32 HAL I2C drivers for I2C implementation. The API's used are as follows,
HAL API | Description |
---|---|
HAL_I2C_Mem_Write | Write 1 byte data to a particular address of the RTC. |
HAL_I2C_Mem_Read | Read 1 byte data from a the particular address of the RTC |
A driver for the RTC module is developed to expose following different API's for easy implementation by the user,
Driver API | Description |
---|---|
rtc_ds1307_init | Initialize the RTC module. |
rtc_ds1307_set_time | Set the time and clock mode (12h/24h). |
rtc_ds1307_get_time | Get the current time and clock mode (12h/24h).. |
rtc_ds1307_set_cal | Set the day, date, month and year. |
rtc_ds1307_get_cal | Get the curren day, date, month and year. |
The current time and date are fetched from RTC using timer interrupt which triggers every 1 sec.
A driver for the LCD Display is developed according to the datasheet to expose following different API's for easy implementation by the user,
Driver API | Description |
---|---|
lcd_init | Initialize the LCD display. |
lcd_send_command | Send instructions to the LCD according to the datasheet. |
lcd_send_data | Send data to the LCD. |
lcd_print_string | Display characters/strings on LCD . |
lcd_ctrl_backlight | Control LCD backlight. |
The following LCD command API's are exposed to the user,
LCD Command API | Description |
---|---|
lcd_display_clear | Clear the display. |
lcd_display_return_home | Shift display to original position. |
lcd_entry_mode_set | Sets cursor move direction and specifies display shift. |
lcd_display_on | Set the dsiplay to be on. |
lcd_display_off | Set the display to be off. |
lcd_display_on_cursor_on | Set the cursor to be on. |
lcd_display_on_cursor_off | Set the cursor to be off. |
lcd_display_on_blink_on | Set the blink to be on. |
lcd_display_on_blink_off | Set the blink to be off. |
lcd_function_set | Sets interface data length, number of display lines and character font. |
lcd_set_cursor | Sets the cursor position to particular row and column. |
To configure the different parameters like clock, calendar and alarm push buttons are implemented using the EXTI (External Interrupts) for faster and accurate response.
Since the timer interrupt executes every sec, it is required to get instant response from the button which cannot be done by simple polling mechanism.
The interrupt based system works accurately with almost no delay giving instant response when the button is pressed.
Parikshit Pagare