PIC16F877A + DS3231 with alarms and temperature monitor – CCS C

Interfacing PIC16F877A with DS3231 RTC (2)

After I built a simple real time clock and calendar using PIC16F877A microcontroller and DS3231 RTC boards, I improved the previous project and I added alarm functions (alarm1 and alarm2) and temperature monitor.

RTCC with PIC16F877A and DS3231 topic:
Simple real time clock and calendar using DS3231 and PIC16F877A

The DS3231 RTC has a built-in 2 alarm functions and a digital temperature sensor with an accuracy of ±3°C.

Hardware Required:

  • PIC16F877A microcontroller
  • 20×4 LCD screen
  • DS3231 RTC board  — DS3231 datasheet
  • 8 MHz crystal
  • 2 x 22pF ceramic capacitor
  • 10K ohm resistor
  • 10K ohm variable resistor
  • 330 ohm resistor
  • LED
  • 3 x push button
  • 5V supply source
  • Breadboard
  • Jumper wires

Project circuit:

PIC16F877A DS3231 alarms real time clock with alarms circuit

To simplify the circuit, 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 PIC16F877A, SCL line is connected to RC3, SDA is connected to RC4 and INT line is connected to pin RB0 which is the external interrupt pin of the PIC16F877A. 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, 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 MCU runs with 8MHz crystal oscillator.

Project 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 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: date month, year respectively
i = 5, 6: alarms hours and minutes respectively
i = 7: alarms 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 was 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.

Videos:
The following video shows a simple hardware circuit of the project.

And the second video shows a simulation of the project with Proteus.

2 comments

    1. That function is just a simple delay but it can be interrupted by the 3 buttons B1, B2 and B3. Without pressing any button the total time of the delay = 10 x 25ms = 250 milliseconds.
      You can use Timer module to do the same job.

Leave a Reply

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