Saturday, September 16, 2017

PIC12F1822 + DS3231 + Remote Control


PIC12F1822 is a small microcontroller has only 8 pins which is not enough for building a real time clock using DS3231 with set buttons because the DS3231 needs 2 pins and the LCD screen needs 3 pins, therefore I've only 1 free pin. I connected that pin to an IR receiver and I used a remote control with a lot of buttons where I used only 3 buttons and the problem solved.
This post shows how did I build a simple real time clock using PIC12F1822, 1602 LCD and DS3231 with Car MP3 IR remote control (NEC protocol RC).
Before this project I made a NEC remote control decoder using PIC12F1822 MCU:
Extended NEC Protocol Decoder Using PIC12F1822 Microcontroller
Time and calendar are displayed on 1602 LCD with the help of a serial-in parallel-out shift register (74HC595, 74HC164, CD4094 .....) as what I made in the example below:
Interfacing PIC12F1822 microcontroller with LCD display
The DS3231 uses I2C protocol to interface with the master device which is in our example the PIC12F1822 MCU which has one I2C module.
The I2C protocol uses only two lines: SCL (Serial Clock) and SDA (Serial Data) which are in the PIC12F1822 pin RA1 and pin RA2 respectively.
Hardware Required:
  • PIC12F1822 microcontroller
  • DS3231 board - datasheet
  • 1602 LCD screen
  • 74HC595 shift register (74HC164 and CD4094 can be used)
  • IR receiver
  • 10K ohm variable resistor
  • 47uF capacitor
  • 5V supply source
  • Breadboard
  • Jumper wires
 The Circuit:
PIC12F1822 + DS3231 + Car MP3 remote control circuit
The IR receiver has 3 pins: GND, VCC and OUT. Pin OUT is connected to pin RA3 of the PIC12F1822.
The LCD screen with the 74HC595 shift register are connected to pins: RA0 (Enable), RA4 (Data) and RA5 (Clock).
The DS3231 board SCL and SDA are connected to pins RA1 and RA2 respectively.
In this project the PIC12F1822 uses its internal oscillator and MCLR pin is configured as a digital pin.
The Code:
The C code below was tested with CCS C compiler version 5.051.
The hardware I2C module of the PIC12F1822 MCU is initialized and configured using the function below 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 2 functions below. Before displaying (after reading from DS3231), the data have to be converted from BCD to decimal, and before writing to the DS3231 (after editing the parameters) the data have to be converted from decimal to BCD:
int8 bcd_to_decimal(number)
int8 decimal_to_bcd(number)
Each function returns the converted value of the variable number.
void DS3231_display() : displays time and calendar data, before displaying time and calendar data are converted from BCD format to decimal format using the function bcd_to_decimal(number) .
int8 edit(x, y, parameter) : I used this function to edit time calendar parameters (minutes, hours, date, month and year). 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
After the edit of time and calendar, the data have to be converted back to BCD format using the function decimal_to_bcd(number) and written back to the DS3231.
void blink() : this small function works as a delay except that it is interrupted by the IR remote control codes for buttons 1, 2 and 3. When called and without pressing any of the three button the total time is 10 x 25ms = 250ms. With this function we can see the blinking of the selected parameter with a frequency of 2Hz. So a delay of 250ms comes after the print of the selected parameter and after that delay a 2 spaces is printed which makes the parameter disappears from the LCD and another 250ms delay comes after the print of the 2 spaces.
The remote control buttons and their codes are shown in the image below:
Car MP3 IR remote control button codes 
I used only 3 buttons where button 1 selects the parameter which we want to set (hours, minutes, date, month or year). Button 2 increments the selected parameter and button 3 decrements it. The rest of buttons have no effect on the circuit.
Finally the interfacing of the LCD with the PIC12F1822 needs a small driver which can be found in the the following topic:
3-Wire LCD driver for CCS PIC C compiler
The complete C code is the one below.
/* Interfacing PIC12F1822 MCU with DS3231 with NEC IR remote control (Car MP3)
   http://ccspicc.blogspot.com/
   electronnote@gmail.com
*/

//LCD module connections
#define LCD_DATA_PIN  PIN_A5
#define LCD_CLOCK_PIN PIN_A4
#define LCD_EN_PIN    PIN_A0
//End LCD module connections

#include <12F1822.h>
#fuses NOMCLR INTRC_IO PLL_SW
#use delay(clock=32000000)
#use fast_io(A)
#include <3WireLCD.c>                            // 3-Wire LCD driver source file
#define IR_Sensor PIN_A3
#use I2C(master, I2C1, FAST = 100000)

int1  nec_ok = 0;
int8  i, second, minute, hour, date, month, year;
int32 nec_code;
void nec_remote_read(){
  int8 j;
  int16 count = 0;
  // Check 9ms pulse (remote control sends logic high)
  while(!input(IR_Sensor) && (count < 9500))
    count = GET_TIMER1();
  if((count > 9499) || (count < 8500))
    return;
  // Check 4.5ms space (remote control sends logic low)
  SET_TIMER1(0);
  count = 0;
  while((input(IR_Sensor)) && (count < 5000))
    count = GET_TIMER1();
  if((count > 4999) || (count < 1500))
    return;
  if(count < 3000){                              // Check if previous code is repeated
    SET_TIMER1(0);
    count = 0;
    while(!input(IR_Sensor) && (count < 650))
      count = GET_TIMER1();
    if((count > 649) || (count < 500))
      return;
    // Check if the repeated code is for button 2 or 3
    if((nec_code == 0x40BF807F || nec_code == 0x40BF40BF)){
      nec_ok = 1;                                // Decoding process is finished with success
      disable_interrupts(INT_RA3);               // Disable the external interrupt
      return;
    }
  }
  // Read message (32 bits)
  for(j = 0; j < 32; j++){
    SET_TIMER1(0);
    count = 0;
    while(!input(IR_Sensor) && (count < 650))
      count = GET_TIMER1();
    if((count > 649) || (count < 500))
      return;
    count = 0;
    SET_TIMER1(0);
    while((input(IR_Sensor)) && (count < 1800))
      count = GET_TIMER1();
    if( (count > 1799) || (count < 400))
      return;
    if( count > 1000)                            // If space width > 1ms
      bit_set(nec_code, (31 - j));               // Write 1 to bit (31 - j)
    else                                         // If space width < 1ms
      bit_clear(nec_code, (31 - j));             // Write 0 to bit (31 - j)
  }
  disable_interrupts(INT_RA3);
  nec_ok = 1;
  return;
}
#INT_RA                                          // RA port interrupt on change
void ra_isr(void){
  SET_TIMER1(0);
  SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);      // Configure Timer 1 to increment every 1 us  
  nec_remote_read();
  SETUP_TIMER_1(T1_DISABLED);                    // Stop Timer1 module
  clear_interrupt(INT_RA);
}
int8 bcd_to_decimal(number){                     // Convert BCD to decimal function
  return((number >> 4) * 10 + (number & 0x0F));
}
int8 decimal_to_bcd(number){                     // Convert decimal to BCD function
  return(((number / 10) << 4) + (number % 10));
}
void ds3231_display(){
  // Convert data from BCD format to decimal format
  second = bcd_to_decimal(second);
  minute = bcd_to_decimal(minute);
  hour   = bcd_to_decimal(hour);
  date   = bcd_to_decimal(date);
  month  = bcd_to_decimal(month);
  year   = bcd_to_decimal(year);
  // End conversion
  lcd_goto(13, 1);   
  printf(lcd_out,"%02u", second);                // Display seconds
  lcd_goto(10, 1);   
  printf(lcd_out,"%02u", minute);                // Display minutes
  lcd_goto(7, 1);   
  printf(lcd_out,"%02u", hour);                  // Display hours
  lcd_goto(7, 2);   
  printf(lcd_out,"%02u", date);                  // Display date
  lcd_goto(10, 2);   
  printf(lcd_out,"%02u", month);                 // Display month
  lcd_goto(15, 2);   
  printf(lcd_out,"%02u", year);                  // Display year
}
void _reset(){
  nec_ok = 0;                                    // Reset decoding process
  enable_interrupts(INT_RA3_H2L);                // Enable RA3 interrupt (High to low)
}
void blink(){
  int8 j = 0;
  while((!nec_ok || (nec_code != 0x40BF00FF) && (nec_code != 0x40BF40BF) && (nec_code != 0x40BF807F)) && (j < 10)){
    j++;
    delay_ms(25);
  }
}
int8 edit(x, y, parameter){
  _reset();
  while(TRUE){
    if(nec_ok && nec_code == 0x40BF40BF){        // If button RB1 is pressed
      parameter++;
      if(i == 0 && parameter > 23)               // If hours > 23 ==> hours = 0
        parameter = 0;
      if(i == 1 && parameter > 59)               // If minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)               // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 3 && parameter > 12)               // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)               // If year > 99 ==> year = 0
        parameter = 0;
    }
    if(nec_ok && nec_code == 0x40BF807F){
      if(i == 0 && parameter < 1)
        parameter = 24;
      if(i == 1 && parameter < 1)
        parameter = 60;
      if(i == 2 && parameter < 2)
        parameter = 32;
      if(i == 3 && parameter < 2)
        parameter = 13;
      if(i == 4 && parameter < 1)
        parameter = 100;
      parameter--;
    }
    lcd_goto(x, y);
    printf(lcd_out,"%02u", parameter);
    if(nec_ok){
      delay_ms(200);
      _reset();
    }
    blink();
    lcd_goto(x, y);
    lcd_out("  ");                              // Display two spaces
    blink();
    if(nec_ok && nec_code == 0x40BF00FF){
      lcd_goto(x, y);
      printf(lcd_out,"%02u", parameter);
      i++;                                       // Increment 'i' for the next parameter
      return parameter;                          // Return parameter value and exit
    }
  }
}
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);       // Set internal oscillator to 32MHz (8MHz and PLL)
  set_tris_a(0x0E);                              // Configure RA1, RA2 & RA3 as inputs
  port_a_pullups(8);                             // Enable pin RA3 internal pull-up
  lcd_initialize();                              // Initialize LCD module
  lcd_cmd(LCD_CLEAR);                            // LCD Clear
  enable_interrupts(GLOBAL);                     // Enable global interrupts
  clear_interrupt(INT_RA);                       // Clear RA IOC flag bit
  enable_interrupts(INT_RA3_H2L);                // Enable RA3 interrupt (High to low)
  lcd_goto(1, 1);  lcd_out("TIME:   :  :");
  lcd_goto(1, 2);  lcd_out("DATE:   /  /20");
  while(TRUE){
    if(nec_ok == 1){                             // If a NEC remote code was received
      if(nec_code == 0x40BF00FF){                // If the remote code is for button 1
        i = 0;
        hour   = edit(7, 1, hour);
        minute = edit(10, 1, minute);
        date   = edit(7, 2, date);
        month  = edit(10, 2, month);
        year   = edit(15, 2, year);
        // Convert decimal to BCD
        minute = decimal_to_bcd(minute);
        hour   = decimal_to_bcd(hour);
        date   = decimal_to_bcd(date);
        month  = decimal_to_bcd(month);
        year   = decimal_to_bcd(year);
        // End conversion
        // Write data to DS3231 RTC
        i2c_start();                             // Start I2C protocol
        i2c_write(0xD0);                         // DS3231 address
        i2c_write(0);                            // Send register address
        i2c_write(0);                            // Reset seconds and start oscillator
        i2c_write(minute);                       // Write minute value to DS3231
        i2c_write(hour);                         // Write hour value to DS3231
        i2c_write(1);                            // Write day value (not used)
        i2c_write(date);                         // Write date value to DS3231
        i2c_write(month);                        // Write month value to DS3231
        i2c_write(year);                         // Write year value to DS3231
      }
      _reset();                                  // Call _reset function
    }
    i2c_start();                                 // Start I2C protocol
    i2c_write(0xD0);                             // DS3231 address
    i2c_write(0);                                // Send register address
    i2c_start();                                 // Restart I2C
    i2c_write(0xD1);                             // Initialize data read
    second = i2c_read(1);                        // Read seconds from register 0
    minute = i2c_read(1);                        // Read minutes from register 1
    hour   = i2c_read(1);                        // Read hour from register 2
    i2c_read(1);                                 // Read day from register 3 (not used)
    date   = i2c_read(1);                        // Read date from register 4
    month  = i2c_read(1);                        // Read month from register 5
    year   = i2c_read(0);                        // Read year from register 6
    i2c_stop();                                  // Stop I2C protocol
    DS3231_display();                            // Display time & calendar
    delay_ms(50);
  }
}
// End of code.
Small video:


Wednesday, September 6, 2017

Interfacing PIC16F84 with DS3231 RTC


This post shows how to make a real time clock and calendar using PIC16F84 and DS3231 RTC.
The DS3231 uses I2C protocol to interface with the master device which is in this example the PIC16F84A MCU. In this project software I2C is used because the PIC16F84A MCU has no hardware I2C module.
A hardware circuit for DS3231 RTC and PIC16F84A
Hardware Required:
  • PIC16F84A microcontroller
  • 1602 LCD screen
  • 8MHz crystal oscillator
  • 2 x 22pF ceramic capacitor
  • 10K ohm variable resistor
  • 3 x 10K ohm resistor
  • 2 x push button
  • 5V supply source
  • Breadboard
  • Jumper wires
DS3231 board contains the following components:
  • DS3231 RTC - datasheet
  • 2 x 4.7K ohm resistors
  • 0.1uF ceramic capacitor
  • 3V coin cell battery
Interfacing PIC16F84 with DS3231 RTC circuit:
Interfacing PIC16F84A with DS3231 real time clock-calendar circuit
In the circuit there are 2 push buttons (B1 & B2) connected to pin RA2 and pin RA3, the two push buttons are used to set the time as well as the calendar parameters (minutes, hours, date, month and year). Button B1 selects the parameter and B2 increments the selected parameter.
Interfacing PIC16F84 with DS3231 RTC C code:
The C code below was tested with CCS PIC C compiler version 5.051.
I used the function below to initialize the software I2C where pin RA0 and pin RA1 are used for SDA and SCL lines respectively (PIC16F84A is used as master device):
#use I2C(MASTER, SDA=PIN_A0, SCL=PIN_A1, FAST=100000)
The DS3231 works with BCD format only and to convert the BCD to decimal and vise versa I used the 2 functions below. Before displaying (after reading from DS3231), the data have to be converted from BCD to decimal, and before writing to the DS3231 (after editing the parameters) the data have to be converted from decimal to BCD:
int8 bcd_to_decimal(number)
int8 decimal_to_bcd(number)
Each function returns the converted value of the variable number.
void DS3231_display() : displays time and calendar data, before displaying time and calendar data are converted from BCD format to decimal format using the function bcd_to_decimal(number) .
int8 edit(x, y, parameter) : I used this function to edit time calendar parameters (minutes, hours, date, month and year). 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
After the edit of time and calendar, the data have to be converted back to BCD format using the function decimal_to_bcd(number) and written to the DS3231.
void blink() : this small function works as a delay except that it is interrupted by the buttons B1 (connected to RA2) and B2 (connected to RA3). When called and without pressing any button the total time is 10 x 25ms = 250ms. With this function we can see the blinking of the selected parameter with a frequency of 2Hz. So a delay of 250ms comes after the print of the selected parameter and after that delay a 2 spaces is printed which makes the parameter disappears from the LCD and another 250ms delay comes after the print of the 2 spaces.
The complete C code is the one below.
// Real time clock - calendar with DS3231 and PIC16F84A CCS C code.
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN     PIN_B0
#define LCD_RW_PIN     PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4      PIN_B3
#define LCD_DATA5      PIN_B4
#define LCD_DATA6      PIN_B5
#define LCD_DATA7      PIN_B6
//End LCD module connections

#include <16F84A.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(clock = 8000000)
#use fast_io(A)
#use fast_io(B)
#include <lcd.c>
#use I2C(MASTER, SDA=PIN_A0, SCL=PIN_A1, FAST=100000)


int8 i, second, minute, hour, date, month, year;
int8 bcd_to_decimal(number){                     // Convert BCD to decimal function
  return((number >> 4) * 10 + (number & 0x0F));
}
int8 decimal_to_bcd(number){                     // Convert decimal to BCD function
  return(((number / 10) << 4) + (number % 10));
}
void ds3231_display(){
  second = bcd_to_decimal(second);
  minute = bcd_to_decimal(minute);
  hour   = bcd_to_decimal(hour);
  date   = bcd_to_decimal(date);
  month  = bcd_to_decimal(month);
  year   = bcd_to_decimal(year);
  lcd_gotoxy(13, 1);   
  printf(lcd_putc,"%02u", second);               // Display seconds
  lcd_gotoxy(10, 1);   
  printf(lcd_putc,"%02u", minute);               // Display minutes
  lcd_gotoxy(7, 1);   
  printf(lcd_putc,"%02u", hour);                 // Display hours
  lcd_gotoxy(7, 2);   
  printf(lcd_putc,"%02u", date);                 // Display date
  lcd_gotoxy(10, 2);   
  printf(lcd_putc,"%02u", month);                // Display month
  lcd_gotoxy(15, 2);   
  printf(lcd_putc,"%02u", year);                 // Display year
}
void blink(){
  int8 j = 0;
  while(j < 10 && input(PIN_A2) && input(PIN_A3)){
    j++;
    delay_ms(25);
  }
}
unsigned int8 edit(x, y, parameter){
  while(!input(PIN_A2));                         // Wait until button RB0 is released
  while(TRUE){
    while(!input(PIN_A3)){                       // If button RB1 is pressed
      parameter++;
      if(i == 0 && parameter > 23)               // If hours > 23 ==> hours = 0
        parameter = 0;
      if(i == 1 && parameter > 59)               // If minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)               // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 3 && parameter > 12)               // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)               // If year > 99 ==> year = 0
        parameter = 0;
      lcd_gotoxy(x, y);
      printf(lcd_putc,"%02u", parameter);        // Display parameter
      delay_ms(200);                             // Wait 200ms
    }
    lcd_gotoxy(x, y);
    lcd_putc("  ");                              // Display two spaces
    blink();
    lcd_gotoxy(x, y);
    printf(lcd_putc,"%02u", parameter);          // Display parameter
    blink();
    if(!input(PIN_A2)){                          // If button RA2 is pressed
      i++;                                       // Increament 'i' for the next parameter
      return parameter;                          // Return parameter value and exit
    }
  }
}
void main(){
  lcd_init();                                    // Initialize LCD module
  lcd_putc('\f');                                // LCD clear
  lcd_gotoxy(1, 1);  lcd_putc("TIME:   :  :");
  lcd_gotoxy(1, 2);  lcd_putc("DATE:   /  /20");
  while(TRUE){
    if(!input(PIN_A2)){                          // If RB0 button is pressed
      i = 0;
      hour   = edit(7, 1, hour);
      minute = edit(10, 1, minute);
      date   = edit(7, 2, date);
      month  = edit(10, 2, month);
      year   = edit(15, 2, year);
      // Convert decimal to BCD
      minute = decimal_to_bcd(minute);
      hour   = decimal_to_bcd(hour);
      date   = decimal_to_bcd(date);
      month  = decimal_to_bcd(month);
      year   = decimal_to_bcd(year);
      // End conversion
      // Write data to DS3231 RTC
      i2c_start();                               // Start I2C protocol
      i2c_write(0xD0);                           // DS3231 address
      i2c_write(0);                              // Send register address
      i2c_write(0);                              // Reset sesonds and start oscillator
      i2c_write(minute);                         // Write minute value to DS3231
      i2c_write(hour);                           // Write hour value to DS3231
      i2c_write(1);                              // Write day value (not used)
      i2c_write(date);                           // Write date value to DS3231
      i2c_write(month);                          // Write month value to DS3231
      i2c_write(year);                           // Write year value to DS3231
      delay_ms(200);                             // Wait 200ms
    }
    i2c_start();                                  // Start I2C protocol
    i2c_write(0xD0);                              // DS3231 address
    i2c_write(0);                                 // Send register address
    i2c_start();                                  // Restart I2C
    i2c_write(0xD1);                              // Initialize data read
    second = i2c_read(1);                         // Read seconds from register 0
    minute = i2c_read(1);                         // Read minuts from register 1
    hour   = i2c_read(1);                         // Read hour from register 2
    i2c_read(1);                                  // Read day from register 3 (not used)
    date   = i2c_read(1);                         // Read date from register 4
    month  = i2c_read(1);                         // Read month from register 5
    year   = i2c_read(0);                         // Read year from register 6
    i2c_stop();                                   // Stop I2C protocol
    DS3231_display();                             // Diaplay time & calendar
    delay_ms(50);
  }
}
Simulation video:
The video below shows the project simulation using Proteus.



Tuesday, September 5, 2017

Real time clock and temperature monitor using PIC16F887 and DS3231


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
  • 20x4 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 RTCC with buttons, alarm functions and temperature monitor PIC16F887 circuit
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.
/* Real time clock and calendar with 2 alarms and temperature monitor using
   PIC16F887 & DS3231 CCS PIC C code.
   Read DS3231 RTC datasheet to understand the code!
   Time & date parameters can be set using two push buttons connected to RB1 & RB2.
   Alarm1 and alarm2 can be set using buttons RB3 and RB2.
   Pin RB4 becomes high when alarm occurred and button RB2 returns it to low and
   turn the occurred alarm OFF.
   DS3231 interrupt pin is connected to PIC16F887 external interrupt pin RB0.
   http://ccspicc.blogspot.com/
   electronnote@gmail.com
*/

//LCD module connections
#define LCD_RS_PIN      PIN_D0
#define LCD_RW_PIN      PIN_D1
#define LCD_ENABLE_PIN  PIN_D2
#define LCD_DATA4       PIN_D3
#define LCD_DATA5       PIN_D4
#define LCD_DATA6       PIN_D5
#define LCD_DATA7       PIN_D6
//End LCD module connections

#include <16F887.h>
#fuses NOMCLR, INTRC_IO, NOBROWNOUT, NOLVP
#use delay(clock = 8MHz)
#use fast_io(B)
#use fast_io(D)
#include <lcd.c>
#use I2C(master, I2C1, FAST = 100000)

int1 alarm1_status, alarm2_status;
char time[]     = "  :  :  ",
     calendar[] = "      /  /20  ",
     alarm1[]   = "A1:   :  :00", alarm2[]   = "A2:   :  :00",
     temperature[] = "T:   .   C";
int8  i, second, minute, hour, day, date, month, year,
      alarm1_minute, alarm1_hour, alarm2_minute, alarm2_hour,
      status_reg;
#INT_EXT                                         // External interrupt routine
void ext_isr(void){
  output_high(PIN_B4);
  clear_interrupt(INT_EXT);
}
void DS3231_read(){                              // Read time & calendar data function
  i2c_start();                                   // Start I2C protocol
  i2c_write(0xD0);                               // DS3231 address
  i2c_write(0);                                  // Send register address (seconds register)
  i2c_start();                                   // Restart I2C
  i2c_write(0xD1);                               // Initialize data read
  second = i2c_read(1);                          // Read seconds from register 0
  minute = i2c_read(1);                          // Read minutes from register 1
  hour   = i2c_read(1);                          // Read hour from register 2
  day    = i2c_read(1);                          // Read day from register 3
  date   = i2c_read(1);                          // Read date from register 4
  month  = i2c_read(1);                          // Read month from register 5
  year   = i2c_read(0);                          // Read year from register 6
  i2c_stop();                                    // Stop I2C protocol
}
void alarms_read_display(){                      // Read and display alarm1 and alarm2 data function
  int8 control_reg, temperature_lsb;
  signed int8 temperature_msb;
  i2c_start();                                   // Start I2C protocol
  i2c_write(0xD0);                               // DS3231 address
  i2c_write(0x08);                               // Send register address (alarm1 minutes register)
  i2c_start();                                   // Restart I2C
  i2c_write(0xD1);                               // Initialize data read
  alarm1_minute = i2c_read(1);                   // Read alarm1 minutes
  alarm1_hour   = i2c_read(1);                   // Read alarm1 hours
  i2c_read(1);                                   // Skip alarm1 day/date register
  alarm2_minute = i2c_read(1);                   // Read alarm2 minutes
  alarm2_hour   = i2c_read(1);                   // Read alarm2 hours
  i2c_read(1);                                   // Skip alarm2 day/date register
  control_reg = i2c_read(1);                     // Read the DS3231 control register
  status_reg  = i2c_read(1);                     // Read the DS3231 status register
  i2c_read(1);                                   // Skip aging offset register
  temperature_msb = i2c_read(1);                 // Read temperature MSB
  temperature_lsb = i2c_read(0);                 // Read temperature LSB
  i2c_stop();                                    // Stop I2C protocol
    // Convert BCD to decimal
  alarm1_minute = (alarm1_minute >> 4) * 10 + (alarm1_minute & 0x0F);
  alarm1_hour   = (alarm1_hour   >> 4) * 10 + (alarm1_hour & 0x0F);
  alarm2_minute = (alarm2_minute >> 4) * 10 + (alarm2_minute & 0x0F);
  alarm2_hour   = (alarm2_hour   >> 4) * 10 + (alarm2_hour & 0x0F);
    // End conversion
  alarm1[8]     = alarm1_minute % 10  + 48;
  alarm1[7]     = alarm1_minute / 10  + 48;
  alarm1[5]     = alarm1_hour   % 10  + 48;
  alarm1[4]     = alarm1_hour   / 10  + 48;
  alarm2[8]     = alarm2_minute % 10  + 48;
  alarm2[7]     = alarm2_minute / 10  + 48;
  alarm2[5]     = alarm2_hour   % 10  + 48;
  alarm2[4]     = alarm2_hour   / 10  + 48;
  alarm1_status = bit_test(control_reg, 0);      // Read alarm1 interrupt enable bit (A1IE) from DS3231 control register
  alarm2_status = bit_test(control_reg, 1);      // Read alarm2 interrupt enable bit (A2IE) from DS3231 control register
  if(temperature_msb < 0){
    temperature_msb = abs(temperature_msb);
    temperature[2] = '-';
  }
  else
    temperature[2] = ' ';
  temperature_lsb >>= 6;
  temperature[4] = temperature_msb % 10  + 48;
  temperature[3] = temperature_msb / 10  + 48;
  if(temperature_lsb == 0 || temperature_lsb == 2){
    temperature[7] = '0';
    if(temperature_lsb == 0) temperature[6] = '0';
    else                     temperature[6] = '5';
  }
  if(temperature_lsb == 1 || temperature_lsb == 3){
    temperature[7] = '5';
    if(temperature_lsb == 1) temperature[6] = '2';
    else                     temperature[6] = '7';
  }
  temperature[8]  = 223;                         // Degree symbol
  lcd_gotoxy(11, 1);                             // Go to column 10 row 1
  printf(lcd_putc, temperature);                 // Display temperature
  lcd_gotoxy(21, 1);                             // Go to column 1 row 3
  printf(lcd_putc, alarm1);                      // Display alarm1
  lcd_gotoxy(38, 1);                             // Go to column 18 row 3
  if(alarm1_status)  lcd_putc("ON ");            // If A1IE = 1 print 'ON'
  else               lcd_putc("OFF");            // If A1IE = 0 print 'OFF'
  lcd_gotoxy(21, 2);                             // Go to column 1 row 4
  printf(lcd_putc, alarm2);                      // Display alarm2
  lcd_gotoxy(38, 2);                             // Go to column 18 row 4
  if(alarm2_status)  lcd_putc("ON ");            // If A2IE = 1 print 'ON'
  else               lcd_putc("OFF");            // If A2IE = 0 print 'OFF'
  
}
void calendar_display(){                         // Display calendar function
  switch(day){
    case 1:  strcpy(calendar, "Sun   /  /20  "); break;
    case 2:  strcpy(calendar, "Mon   /  /20  "); break;
    case 3:  strcpy(calendar, "Tue   /  /20  "); break;
    case 4:  strcpy(calendar, "Wed   /  /20  "); break;
    case 5:  strcpy(calendar, "Thu   /  /20  "); break;
    case 6:  strcpy(calendar, "Fri   /  /20  "); break;
    case 7:  strcpy(calendar, "Sat   /  /20  "); break;
    default: strcpy(calendar, "Sat   /  /20  "); break;
  }
  calendar[13] = year  % 10 + 48;
  calendar[12] = year  / 10 + 48;
  calendar[8]  = month % 10 + 48;
  calendar[7]  = month / 10 + 48;
  calendar[5]  = date  % 10 + 48;
  calendar[4]  = date  / 10 + 48;
  lcd_gotoxy(1, 2);                              // Go to column 1 row 2
  printf(lcd_putc, calendar);                    // Display calendar
}
void DS3231_display(){
  // Convert BCD to decimal
  second = (second >> 4) * 10 + (second & 0x0F);
  minute = (minute >> 4) * 10 + (minute & 0x0F);
  hour = (hour >> 4) * 10 + (hour & 0x0F);
  date = (date >> 4) * 10 + (date & 0x0F);
  month = (month >> 4) * 10 + (month & 0x0F);
  year = (year >> 4) * 10 + (year & 0x0F);
  // End conversion
  time[7]     = second % 10  + 48;
  time[6]     = second / 10  + 48;
  time[4]     = minute % 10  + 48;
  time[3]     = minute / 10  + 48;
  time[1]     = hour   % 10  + 48;
  time[0]     = hour   / 10  + 48;
  calendar_display();                            // Call calendar display function
  lcd_gotoxy(1, 1);                              // Go to column 1 row 1
  printf(lcd_putc, time);                        // Display time
}
void blink(){
  int8 j = 0;
  while(j < 10 && (input(PIN_B1) || i >= 5) && input(PIN_B2) && (input(PIN_B3) || i < 5)){
    j++;
    delay_ms(25);
  }
}
int8 edit(parameter, x, y){
  while(!input(PIN_B1) || !input(PIN_B3));       // Wait until button RB0 is released
  while(TRUE){
    while(!input(PIN_B2)){                       // If button RB2 is pressed
      parameter++;
      if(((i == 0) || (i == 5)) && parameter > 23)    // If hours > 23 ==> hours = 0
        parameter = 0;
      if(((i == 1) || (i == 6)) && parameter > 59)    // If minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)               // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 3 && parameter > 12)               // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)               // If year > 99 ==> year = 0
        parameter = 0;
      if(i == 7 && parameter > 1)                // For alarms ON or OFF (1: alarm ON, 0: alarm OFF)
        parameter = 0;
      lcd_gotoxy(x, y);
      if(i == 7){                                // For alarms ON & OFF
        if(parameter == 1)  lcd_putc("ON ");
        else                lcd_putc("OFF");
      }
      else
        printf(lcd_putc,"%02u", parameter);      // Display parameter
      if(i >= 5){
        DS3231_read();                           // Read data from DS3231
        DS3231_display();                        // Display DS3231 time and calendar
      }
      delay_ms(200);                             // Wait 200ms
    }
    lcd_gotoxy(x, y);                            // Go to LCD x column and y row
    lcd_putc("  ");                              // Print two spaces
    if(i == 7) lcd_putc(" ");                    // Print space (for alarms ON & OFF)
    blink();                                     // Call blink function
    lcd_gotoxy(x, y);                            // Go to LCD x column and y row
    if(i == 7){                                  // For alarms ON & OFF
      if(parameter == 1)  lcd_putc("ON ");
      else                lcd_putc("OFF");
    }
    else
      printf(lcd_putc,"%02u", parameter);        // Display parameter
    blink();
    if(i >= 5){
      DS3231_read();
      DS3231_display();}
    if((!input(PIN_B1) && i < 5) || (!input(PIN_B3) && i >= 5)){
      i++;                                       // Increment 'i' for the next parameter
      return parameter;                          // Return parameter value and exit
    }
  }
}
void main(){
  setup_oscillator(OSC_8MHZ);                    // Set internal oscillator to 8MHz
  output_b(0);
  set_tris_b(0x0F);                              // Configure RB0 ~ 3 as input pins
  set_tris_d(0);                                 // Configure all PORTD pins as outputs
  port_b_pullups(0x0E);                          // Enable RB1, RB2 & RB internal pull-ups
  enable_interrupts(GLOBAL);                     // Enable global interrupts
  enable_interrupts(INT_EXT_H2L);                // Enable external interrupt with edge from high to low
  lcd_init();                                    // Initialize LCD module
  lcd_putc('\f');                                // LCD clear
  while(TRUE){
    if(!input(PIN_B1)){                          // If RB1 button is pressed
      i = 0;
      hour   = edit(hour, 1, 1);
      minute = edit(minute, 4, 1);
      while(!input(PIN_B1));                     // Wait until button RB0 released
      while(TRUE){
        while(!input(PIN_B2)){                   // If button RB2 button is pressed
          day++;                                 // Increment day
          if(day > 7) day = 1;
          calendar_display();                    // Call display calendar
          lcd_gotoxy(1, 2);                      // Go to column 1 row 2
          printf(lcd_putc, calendar);            // Display calendar
          delay_ms(200);
        }
        lcd_gotoxy(1, 2);                        // Go to column 1 row 2
        lcd_putc("   ");                         // Print 3 spaces
        blink();
        lcd_gotoxy(1, 2);                        // Go to column 1 row 2
        printf(lcd_putc, calendar);              // Print calendar
        blink();                                 // Call blink function
        if(!input(PIN_B1))                       // If button RB1 is pressed
          break;
      }
      date = edit(date, 5, 2);                   // Edit date
      month = edit(month, 8, 2);                 // Edit month
      year = edit(year, 13, 2);                  // Edit year
      // Convert decimal to BCD
      minute = ((minute / 10) << 4) + (minute % 10);
      hour = ((hour / 10) << 4) + (hour % 10);
      date = ((date / 10) << 4) + (date % 10);
      month = ((month / 10) << 4) + (month % 10);
      year = ((year / 10) << 4) + (year % 10);
      // End conversion
      // Write time & calendar data to DS3231 RTC
      i2c_start();                               // Start I2C protocol
      i2c_write(0xD0);                           // DS3231 address
      i2c_write(0);                              // Send register address (seconds address)
      i2c_write(0);                              // Reset seconds and start oscillator
      i2c_write(minute);                         // Write minute value to DS3231
      i2c_write(hour);                           // Write hour value to DS3231
      i2c_write(day);                            // Write day value
      i2c_write(date);                           // Write date value to DS3231
      i2c_write(month);                          // Write month value to DS3231
      i2c_write(year);                           // Write year value to DS3231
      i2c_stop();                                // Stop I2C
      delay_ms(200);                             // Wait 200ms
    }
    if(!input(PIN_B3)){                          // If RB3 button is pressed
      while(!input(PIN_B3));                     // Wait until button RB3 released
      i = 5;
      alarm1_hour   = edit(alarm1_hour, 25, 1);
      alarm1_minute = edit(alarm1_minute, 28, 1);
      alarm1_status = edit(alarm1_status, 38, 1);
      i = 5;
      alarm2_hour   = edit(alarm2_hour, 25, 2);
      alarm2_minute = edit(alarm2_minute, 28, 2);
      alarm2_status = edit(alarm2_status, 38, 2);
      alarm1_minute = ((alarm1_minute / 10) << 4) + (alarm1_minute % 10);
      alarm1_hour   = ((alarm1_hour / 10) << 4) + (alarm1_hour % 10);
      alarm2_minute = ((alarm2_minute / 10) << 4) + (alarm2_minute % 10);
      alarm2_hour   = ((alarm2_hour / 10) << 4) + (alarm2_hour % 10);
      // Write alarms data to DS3231
      i2c_start();                               // Start I2C
      i2c_write(0xD0);                           // DS3231 address
      i2c_write(7);                              // Send register address (alarm1 seconds)
      i2c_write(0);                              // Write 0 to alarm1 seconds
      i2c_write(alarm1_minute);                  // Write alarm1 minutes value to DS3231
      i2c_write(alarm1_hour);                    // Write alarm1 hours value to DS3231
      i2c_write(0x80);                           // Alarm1 when hours, minutes, and seconds match
      i2c_write(alarm2_minute);                  // Write alarm2 minutes value to DS3231
      i2c_write(alarm2_hour);                    // Write alarm2 hours value to DS3231
      i2c_write(0x80);                           // Alarm2 when hours and minutes match
      i2c_write(4 | alarm1_status | (alarm2_status << 1));      // Write data to DS3231 control register (enable interrupt when alarm)
      i2c_write(0);                              // Clear alarm flag bits
      i2c_stop();                                // Stop I2C
      delay_ms(200);                             // Wait 200ms
    }
    if(!input(PIN_B2) && input(PIN_B4)){         // When button B2 pressed with alarm (Reset and turn OFF the alarm)
      output_low(PIN_B4);                        // Turn OFF the alarm indicator
      i2c_start();                               // Start I2C
      i2c_write(0xD0);                           // DS3231 address
      i2c_write(0x0E);                           // Send register address (control register)
      // Write data to control register (Turn OFF the occurred alarm and keep the other as it is)
      i2c_write(4 | (!bit_test(status_reg, 0) & alarm1_status) | ((!bit_test(status_reg, 1) & alarm2_status) << 1));
      i2c_write(0);                              // Clear alarm flag bits
      i2c_stop();                                // Stop I2C
    }
    DS3231_read();                               // Read time and calendar parameters from DS3231 RTC
    alarms_read_display();                       // Read and display alarms parameters
    DS3231_display();                            // Display time & calendar
    delay_ms(50);                                // Wait 50ms
  }
}
// End of code
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.