DS3231 RTC with alarms and temperature monitor using mikroC

This article shows how to build a real time clock and calendar with 2 alarm functions and temperature monitor using PIC16F887 microcontroller and mikroC PRO for PIC compiler.

Last time I built the same project but using CCS C compiler, you can find CCS C code at the link below:
DS3231 RTC with alarm function and temperature monitor – CCS C

Hardware Required:

  • PIC16F887 microcontroller   —>  datasheet
  • DS3231 board                   —>  DS3231 datasheet
  • 20×4 LCD screen
  • 10k ohm variable resistor
  • 2 x 330 ohm resistor
  • LED
  • 3 x push button
  • 3V coin cell battery
  • 5V supply source
  • Breadboard
  • Jumper wires

PIC16F887 + DS3231 circuit:
Circuit schematic diagram is shown below.

PIC16F887 with DS3231 RTC and 20x4 LCD - MPLAB XC8 - mikroC

In this project I used the DS3231 board, this board basically contains the main chip which is the DS3231, two 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 2004 LCD and the PIC16F887 microcontroller, there are 3 data lines connected between this board and the microcontroller, SCL line is connected to pin RC3, SDA is connected to pin RC4 and INT line is connected to external interrupt pin RB0. The DS3231 interrupts the microcontroller when there is an alarm (alarm 1 or alarm 2).

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 of the week, date, month and year) and B2 increments the selected parameter. Buttons B3 and B2 adjust alarm 1 and alarm 2 parameters (hours, minutes and ON/OFF), button B3 selects the parameter and B2 increments the selected parameter.

Also, there is an LED connected to PIC16F887 pin RB4, this LED is used as an alarm indicator (alarm 1 or alarm 2), so if there is an alarm, the DS3231 pulls down the INT pin (RB0) which interrupts the microcontroller and the microcontroller turns the LED ON, here button B2 turns both the LED and the occurred alarm OFF.

PIC16F887 + DS3231 mikroC code:
The C code below was tested with mikroC PRO for PIC compiler version 7.1.0.
By reading the datasheet of the DS3231 RTC the code will be more easier!

Programming hints:
The DS3231 works with BCD format only (except the temperature) and to convert the BCD to decimal and vise versa I used the following commands (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 date (Monday, Tuesday …) by calling a function named void day_display() .

void alarms_read_display() : basically this functions reads alarm 1 and alarm 2 minutes and hours. It also reads the DS3231 control register, status register and temperature registers (2 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.

char edit(byte x, byte y, byte parameter) : 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 microcontroller turns the LED ON (connected to pin RB4) when it is interrupted by the DS3231, the DS3231 sends the interrupt signal (pulls down the INT line) when there has been an alarm (alarm 1 or alarm 2). Button B2 resets and turns OFF the alarm. If both alarms are active, button B2 will reset and turn OFF the occurred alarm only and keep the other as it is. To do that we’ve to detect which alarm has been 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:
I2C1_Wr(4 | (!(status_reg & 1) & alarm1_status) | ((!((status_reg >> 1) & 1) & alarm2_status) << 1));
alarm1_status and alarm2_status are 1-bit variables (can be 1 or 0), for example if alarm1_status is 1 ==> alarm 1 is ON and if alarm1_status is 0 ==> alarm 1 is OFF. The same thing for alarm 2.
The complete mikroC code is below.

The video below shows how the project works in hardware circuit (in this video the microcontroller used 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