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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | /* Real time clock and calendar with 2 alarms and temperature monitor using PIC16F887 & DS3231 mikroC PRO for PIC C code. The PIC16F887 runs with internal oscillator @ 8MHz. Read DS3231 RTC datasheet to understand the code! Time & date 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, button RB2 returns it to low and turn the occurred alarm OFF. DS3231 interrupt pin is connected to PIC16F887 external interrupt pin RB0. */ // LCD module connections sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD2_bit; sbit LCD_D5 at RD3_bit; sbit LCD_D6 at RD4_bit; sbit LCD_D7 at RD5_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD2_bit; sbit LCD_D5_Direction at TRISD3_bit; sbit LCD_D6_Direction at TRISD4_bit; sbit LCD_D7_Direction at TRISD5_bit; // End LCD module connections bit alarm1_status, alarm2_status; char time[] = " : : ", calendar[] = " / /20 ", alarm1[] = "A1: : :00", alarm2[] = "A2: : :00", temperature[] = "T: . C"; char i, second, minute, hour, day, date, month, year, alarm1_minute, alarm1_hour, alarm2_minute, alarm2_hour, status_reg; void Interrupt() { if (INTF_bit){ PORTB.F4 = 1; INTF_bit = 0; } } void DS3231_read(){ // Read time & calendar data function I2C1_Start(); // Start I2C protocol I2C1_Wr(0xD0); // DS3231 address I2C1_Wr(0); // Send register address (seconds register) I2C1_Repeated_Start(); // Restart I2C I2C1_Wr(0xD1); // Initialize data read second = I2C1_Rd(1); // Read seconds from register 0 minute = I2C1_Rd(1); // Read minutes from register 1 hour = I2C1_Rd(1); // Read hour from register 2 day = I2C1_Rd(1); // Read day from register 3 date = I2C1_Rd(1); // Read date from register 4 month = I2C1_Rd(1); // Read month from register 5 year = I2C1_Rd(0); // Read year from register 6 I2C1_Stop(); // Stop I2C protocol } void alarms_read_display(){ // Read and display alarm1 and alarm2 data function char control_reg, temperature_lsb; short temperature_msb; I2C1_Start(); // Start I2C protocol I2C1_Wr(0xD0); // DS3231 address I2C1_Wr(0x08); // Send register address (alarm1 minutes register) i2c_restart(); // Restart I2C I2C1_Wr(0xD1); // Initialize data read alarm1_minute = I2C1_Rd(1); // Read alarm1 minutes alarm1_hour = I2C1_Rd(1); // Read alarm1 hours I2C1_Rd(1); // Skip alarm1 day/date register alarm2_minute = I2C1_Rd(1); // Read alarm2 minutes alarm2_hour = I2C1_Rd(1); // Read alarm2 hours I2C1_Rd(1); // Skip alarm2 day/date register control_reg = I2C1_Rd(1); // Read the DS3231 control register status_reg = I2C1_Rd(1); // Read the DS3231 status register I2C1_Rd(1); // Skip aging offset register temperature_msb = I2C1_Rd(1); // Read temperature MSB temperature_lsb = I2C1_Rd(0); // Read temperature LSB I2C1_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 = control_reg; // Read alarm1 interrupt enable bit (A1IE) from DS3231 control register alarm2_status = 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_Out(1, 11, temperature); // Display temperature Lcd_Out(3, 1, alarm1); // Display alarm1 if(alarm1_status) Lcd_Out(3, 18, "ON "); // If A1IE = 1 print 'ON' else Lcd_Out(3, 18, "OFF"); // If A1IE = 0 print 'OFF' Lcd_Out(4, 1, alarm2); // Display alarm2 if(alarm2_status) Lcd_Out(4, 18, "ON "); // If A2IE = 1 print 'ON' else Lcd_Out(4, 18, "OFF"); // If A2IE = 0 print 'OFF' } void day_display(){ // Day display function switch(day){ case 1: Lcd_Out(2, 1, "Sun"); break; case 2: Lcd_Out(2, 1, "Mon"); break; case 3: Lcd_Out(2, 1, "Tue"); break; case 4: Lcd_Out(2, 1, "Wed"); break; case 5: Lcd_Out(2, 1, "Thu"); break; case 6: Lcd_Out(2, 1, "Fri"); break; default: Lcd_Out(2, 1, "Sat"); break; } } 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[9] = year % 10 + 48; calendar[8] = year / 10 + 48; calendar[4] = month % 10 + 48; calendar[3] = month / 10 + 48; calendar[1] = date % 10 + 48; calendar[0] = date / 10 + 48; Lcd_Out(1, 1, time); // Display time day_display(); // Display day Lcd_Out(2, 5, calendar); // Display calendar } void blink(){ char j = 0; while(j < 10 && (PORTB.F1 || i >= 5) && PORTB.F2 && (PORTB.F3 || i < 5)){ j++; delay_ms(25); } } char edit(char parameter, char x, char y){ while(!PORTB.F1 || !PORTB.F3); // Wait for button RB0 is release while(1){ while(!PORTB.F2){ // 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; if(i == 7){ // For alarms ON & OFF if(parameter == 1) Lcd_Out(y, x, "ON "); else Lcd_Out(y, x, "OFF"); } else{ Lcd_Chr(y, x, parameter / 10 + 48); Lcd_Chr(y, x + 1, parameter % 10 + 48); } if(i >= 5){ DS3231_read(); // Read data from DS3231 DS3231_display(); // Display DS3231 time and calendar } delay_ms(200); // Wait 200ms } Lcd_Out(y, x, " "); // Print two spaces if(i == 7) Lcd_Out(y, x + 2, " "); // Print space (for alarms ON & OFF) blink(); // Call blink function if(i == 7){ // For alarms ON & OFF if(parameter == 1) Lcd_Out(y, x, "ON "); else Lcd_Out(y, x, "OFF"); } else{ Lcd_Chr(y, x, parameter / 10 + 48); Lcd_Chr(y, x + 1, parameter % 10 + 48); } blink(); if(i >= 5){ DS3231_read(); DS3231_display();} if((!PORTB.F1 && i < 5) || (!PORTB.F3 && i >= 5)){ i++; // Increment 'i' for the next parameter return parameter; // Return parameter value and exit } } } void main() { OSCCON = 0X70; // Set internal oscillator to 8MHz ANSELH = 0; // Configure all PORTB pins as digital PORTB = 0; // PORTB initial state TRISB = 0x0F; // Configure RB0 ~ 3 as input pins OPTION_REG.F7 = 0; // Enable PORTB internal pull-ups WPUB = 0x0F; // Enable RB0 ~ 3 internal pull-ups Lcd_Init(); // Initialize LCD module Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off Lcd_Cmd(_LCD_CLEAR); // clear LCD INTF_bit = 0; // Clear external interrupt flag bit GIE_bit = 1; // Enable global interrupts INTEDG_bit = 0; // Set external Interrupt on falling edge INTE_bit = 1; // Enable external interrupt I2C1_Init(100000); // Initialize I2C communication while(1) { if(!PORTB.F1){ // If RB1 button is pressed i = 0; hour = edit(hour, 1, 1); // Edit hours minute = edit(minute, 4, 1); // Edit minutes while(!PORTB.F1); // Wait for button RB0 release while(1){ while(!PORTB.F2){ // If button RB2 button is pressed day++; // Increment day if(day > 7) day = 1; day_display(); delay_ms(200); } Lcd_Out(2, 1, " "); // Print 3 spaces blink(); day_display(); blink(); // Call blink function if(!PORTB.F1) // 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 I2C1_Start(); // Start I2C protocol I2C1_Wr(0xD0); // DS3231 address I2C1_Wr(0); // Send register address (seconds address) I2C1_Wr(0); // Reset seconds and start oscillator I2C1_Wr(minute); // Write minute value to DS3231 I2C1_Wr(hour); // Write hour value to DS3231 I2C1_Wr(day); // Write day value I2C1_Wr(date); // Write date value to DS3231 I2C1_Wr(month); // Write month value to DS3231 I2C1_Wr(year); // Write year value to DS3231 I2C1_Stop(); // Stop I2C delay_ms(200); // Wait 200ms } if(!PORTB.F3){ // If RB3 button is pressed while(!PORTB.F3); // Wait until button RB3 released i = 5; alarm1_hour = edit(alarm1_hour, 5, 3); alarm1_minute = edit(alarm1_minute, 8, 3); alarm1_status = edit(alarm1_status, 18, 3); i = 5; alarm2_hour = edit(alarm2_hour, 5, 4); alarm2_minute = edit(alarm2_minute, 8, 4); alarm2_status = edit(alarm2_status, 18, 4); 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 I2C1_Start(); // Start I2C I2C1_Wr(0xD0); // DS3231 address I2C1_Wr(7); // Send register address (alarm1 seconds) I2C1_Wr(0); // Write 0 to alarm1 seconds I2C1_Wr(alarm1_minute); // Write alarm1 minutes value to DS3231 I2C1_Wr(alarm1_hour); // Write alarm1 hours value to DS3231 I2C1_Wr(0x80); // Alarm1 when hours, minutes, and seconds match I2C1_Wr(alarm2_minute); // Write alarm2 minutes value to DS3231 I2C1_Wr(alarm2_hour); // Write alarm2 hours value to DS3231 I2C1_Wr(0x80); // Alarm2 when hours and minutes match I2C1_Wr(4 | alarm1_status | (alarm2_status << 1)); // Write data to DS3231 control register (enable interrupt when alarm) I2C1_Wr(0); // Clear alarm flag bits I2C1_Stop(); // Stop I2C delay_ms(200); // Wait 200ms } if(!PORTB.F2 && PORTB.F4){ // When button B2 pressed with alarm (Reset and turn OFF the alarm) PORTB.F4 = 0; // Turn OFF the alarm indicator I2C1_Start(); // Start I2C I2C1_Wr(0xD0); // DS3231 address I2C1_Wr(0x0E); // Send register address (control register) // Write data to control register (Turn OFF the occurred alarm and keep the other as it is) I2C1_Wr(4 | (!(status_reg & 1) & alarm1_status) | ((!((status_reg >> 1) & 1) & alarm2_status) << 1)); I2C1_Wr(0); // Clear alarm flag bits I2C1_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 |
The video below shows how the project works in hardware circuit (in this video the microcontroller used is PIC16F877A):
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.