DS3231 RTC with alarm function and temperature monitor – CCS C

The last interfacing of the PIC16F887 microcontroller and DS3231 RTC is the building of a simple real time clock and calendar with two buttons for setting time and date. Project link is the one below:
Interfacing DS3231 with PIC16F887 microcontroller

In this topic I’m going to add 2 alarms and temperature monitor to the previous project since the DS3231 has 2 alarm functions and temperature sensor with an accuracy of ±3°C (for more information read the DS3231 datasheet).

Hardware Required:

  • PIC16F887 microcontroller
  • 20×4 LCD screen
  • 10K ohm variable resistor
  • 330 ohm resistor
  • LED
  • 3 x push button
  • 5V supply source
  • Breadboard
  • Jumper wires

DS3231 board contains the following components:

  • DS3231 RTC – datasheet
  • 3 x 4.7K ohm resistors
  • 0.1uF ceramic capacitor
  • 3V coin cell battery

The circuit:
Circuit schematic diagram is shown below.

DS3231 RTC alarm temperature circuit PIC16F887

In this project I used the DS3231 board, this board basically contains the main chip which is the DS3231, pull-up resistors (4.7K) of SCL, SDA and INT/SQW lines and coin cell battery holder. There is also 24C32 EEPROM and some other resistors (not used in this project).
The DS3231 board is supplied with 5V as the microcontroller and the 2004 LCD, there are 3 data lined connected between this board and the PIC16F887 MCU, SCL line is connected to pin RC3, SDA is connected to pin RC4 and INT line is connected to pin RB0 which is the external interrupt pin of the PIC16F887 MCU. The DS3231 interrupts the microcontroller when there is an alarm.
In the circuit there are 3 push buttons: B1, B2 and B3. These buttons are used to set time, calendar and alarms. Time and calendar can be adjusted with B1 and B2, button B1 selects time or date parameter (time parameters: hours and minutes; calendar parameters: day, date, month and year) and B2 increments the selected parameter. The button B3 and B2 adjust alarm1 and alarm2 parameters (hours, minutes and ON/OFF), button B3 selects the parameter and B2 increments the selected parameter.

There is an LED connected to pin RB4, this LED is used as an alarm indicator (alarm1 or alarm2), so if there is an alarm the DS3231 pulls down the INT pin which interrupts the microcontroller and the microcontroller turns the LED ON, here button B2 turns both the LED and the occurred alarm OFF.
In this project the PIC16F887 MCU uses its internal oscillator and MCLR pin function is disabled.

CCS C code:
The C code below was tested with CCS PIC C compiler version 5.051.
By reading the datasheet of the DS3231 RTC the code will be more easier!
The hardware I2C module of the MCU is initialized and configured using the following CCS C function with a speed of 100KHz:
#use I2C(master, I2C1, FAST = 100000)
master: set the microcontroller to the master mode
I2C1: use first I2C module.
The DS3231 works with BCD format only (except the temperature) and to convert the BCD to decimal and vise versa I used the following functions (example for minute variable):
minute = (minute >> 4) * 10 + (minute & 0x0F);                                  // Convert BCD to decimal
minute = ((minute / 10) << 4) + (minute % 10);                                     // Convert decimal to BCD

Code functions:
void DS3231_read() : this function reads time and calendar data from the DS3231 (seconds, minutes, hours, day, date, month and year).
void DS3231_display() : displays time and calendar data, before displaying time and calendar data are converted from BCD format to decimal format. This function displays the calendar by calling a function named void calendar_display() .
void alarms_read_display() : basically this functions reads alarm1 and alarm2 minutes and hours. It also reads the DS3231 control register, status register and 2 temperature registers.
The other job of this function is to display alarms data (hours, minutes and status) and the temperature value. The alarm status are extracted from the control register.
int8 edit(parameter, x, y) : I used this function to edit time, calendar and alarm parameters except the day. I used a variable named i to distinguish between the parameters:
i = 0, 1 : time hours and minutes respectively
i = 2, 3, 4: calendar date, month and year respectively
i = 5, 6: alarms hours and minutes respectively
i = 7: alarm status (ON or OFF)
After the edit of time/calendar/alarms the data have to be converted back to BCD format and written to the DS3231.

The MCU turns the LED ON when it interrupted by the DS3231, the DS3231 sends the interrupt signal (pulls down the INT line) when there has been an alarm. Button B2 resets and turns OFF the alarm. If both alarms are active, button B2 will resets and turns OFF the occurred alarm only and keeps the other as it is. To do that we’ve to detect which alarm was occurred which can be easily done by reading the status register of the DS3231 (A1IF and A2IF flag bits). Turning ON or OFF an alarm is done by writing to the control register (bits: INTCN, A1IE and A2IE). Always INTCN bit should be 1. I used the following line to write 1 to the INTCN bit and to turn OFF the occurred alarm:
i2c_write(4 | (!bit_test(status_reg, 0) & alarm1_status) | ((!bit_test(status_reg, 1) & alarm2_status) << 1));
alarm1_status and alarm2_status are 1-bit variables, for example if alarm1_status is 1 ==> alarm1 is ON and  if alarm1_status is 0 ==> alarm1 is OFF. The same thing for alarm2.
The complete C code is below.

Video:
After connecting the circuit and burning the HEX file into the MCU, I got the result shown similar to what’s in the video below where the used microcontroller is PIC16F877A.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top