This simple project shows how to build a real time clock with temperature monitor using PIC16F887 microcontroller, DS3231 RTCC (or DS3232) and SSD1306 OLED display. Time and date are set with two push buttons and they are displayed on the SSD1306 OLED screen (128×64 Pixel).
The DS3231 has a buit-in temperature sensor which means we can use it for measuring the chip temperature.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.
The DS3231 is more accurate than the DS1307 due to its built-in temperature sensor. It also (the DS3231) keeps time running if the main power source is down (with the help of a 3V coin cell battery). It also uses I2C interface to communicate with the master device which is in this project PIC16F887 microcontroller. This means the DS3231 and the SSD1306 OLED screen share the same I2C bus. Even they share the same bus but at any time the microcontroller ‘speaks’ with 1 device only depending on the address sent. The DS3231 RTC address is 0xD0 (the same as the DS1307 address) and the SSD1306 address is 0x7A.
In the circuit there are two push buttons for setting time and date of the real time clock.
Related Projects:
Interfacing PIC microcontroller with SSD1306 OLED | mikroC Projects
PIC16F887 Interfacing with SSD1306 and DS1307 | mikroC Projects
Hardware Required:
- PIC16F887 microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DS3231 board — DS3231 RTC datasheet
- 2 x push button
- 3V coin cell battery
- 5V power source
- Breadboard
- Jumper wires
Interfacing PIC16F887 with DS3231 RTC and SSD1306 OLED circuit:
The following image shows project circuit diagram.
(All grounded terminals are connected together)
The PIC16F887 microcontroller has one hardware I2C module (MSSP module) with SDA on pin RC4 (#23) and SCL on pin RC3 (#18). The SDA pin of the MCU is connected to the SDA pin of the display (& DS3231 board) and the SCL pin of the MCU is connected to the SCL pin of the display (& DS3231 board).
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.
The SSD1306 OLED display DC pin is connected to VDD which means the I2C slave address of the display is 0x7A.
PIC16F887 microcontroller, DS3231 board and SSD1306 display board are supplied with 5V source between their VDD (or VCC) and GND pins.
The two push buttons B1 and B2 are for setting time and date. Button B1 is connected to pin RB0 (#33) and B2 is connected to pin RB1 (#34). Internal pull-ups for RB0 and RB1 pins are enabled in the code.
In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.
The C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
To be able to compile the C code below with no error, a driver (& library) for the SSD1306 OLED display is required, it’s full name is SSD1306.C, download link is below:
SSD1306 OLED display driver for mikroC compiler
after the download, add the driver file (SSD1306.C) to project folder.
The DS3231 RTC and the SSD1306 OLED display share the same I2C bus, DS3231 I2C address is 0xD0 and SSD1306 display I2C address is 0x7A (default address).
Functions used in the code:
The RTC chip (DS1307 or DS3231) works with BCD format only, to convert BCD to decimal and vise versa I used the 2 functions below. Before displaying (after reading from RTC IC), the data have to be converted from BCD to decimal, and before writing to the RTC IC (after editing the parameters) the data have to be converted from decimal to BCD:
char bcd_to_decimal(char number);
char decimal_to_bcd(char number);
Each function returns the converted value of the variable number.
void RTC_display() : displays time, date and chip temperature, before printing time and date on the screen, they are converted from BCD format to decimal format using the function bcd_to_decimal(char number) .
void display_day(): this function is for displaying day of the week (Saturday, Sunday …).
char edit(char x, char y, char parameter) : I used this function to edit time and date parameters (minutes, hours, date, month and year). I used a variable named i to distinguish between the parameters:
i = 0, 1, 2 : date, month and year respectively
i = 3, 4: hours and minutes respectively
After the edit of time and date, the data have to be converted back to BCD format using the function decimal_to_bcd(char number) and written to the RTC chip.
void blink() : this small function works as a delay except that it can be interrupted by buttons B1 (connected to RB0) and B2 (connected to RB1). When called and without pressing any button the total time is 100 x 5ms = 500ms. With this function we can see the blinking of the selected parameter with a frequency of 1 Hz. So a delay of 500ms comes after the print of the selected parameter and after that delay, 2 spaces are printed which make the parameter disappears from the screen and another 500ms delay comes after the print of the 2 spaces.
Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700
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 323 324 325 | /************************************************************************************** Real time clock with chip temperature monitor using PIC16F887 microcontroller, SSD1306 OLED display (128x64 Pixel) and DS3231 (DS3232) RTC chip. C Code for mikroC PRO for PIC compiler Internal oscillator used @ 8MHz Configuration words: CONFIG1 = 0x2CD4 CONFIG2 = 0x0700 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // SSD1306 OLED reset pin definition (if available) #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit // button definitions #define button1 RB0_bit // button B1 is connected to RB0 pin #define button2 RB1_bit // button B2 is connected to RB1 pin // include SSD1306 OLED driver source code #include <SSD1306.c> // variable declarations char Time[] = " : : "; char Date[] = " / /20 "; char Temperature[] = "000.00"; signed short temperature_msb; unsigned short i, second, minute, hour, w_day, day, month, year, temperature_lsb; char degree[] = {0, 7, 5, 7, 0}; // degree symbol custom char // a small function for button1 (B1) debounce char debounce () { char i, count = 0; for(i = 0; i < 5; i++) { if (button1 == 0) count++; delay_ms(10); } if(count > 2) return 1; else return 0; } // convert BCD to decimal function char bcd_to_decimal(char number) { return ((number >> 4) * 10 + (number & 0x0F)); } // convert decimal to BCD function char decimal_to_bcd(char number) { return (((number / 10) << 4) + (number % 10)); } // function for displaying day of the week void display_day() { SSD1306_GotoXY(7, 1); switch(w_day) { case 1: SSD1306_Print(" SUNDAY "); break; case 2: SSD1306_Print(" MONDAY "); break; case 3: SSD1306_Print(" TUESDAY "); break; case 4: SSD1306_Print("WEDNESDAY"); break; case 5: SSD1306_Print("THURSDAY "); break; case 6: SSD1306_Print(" FRIDAY "); break; default: SSD1306_Print("SATURDAY "); } } // function for displaying time, date and chip temperature void RTC_Display() { // convert BCD to decimal second = bcd_to_decimal(second); minute = bcd_to_decimal(minute); hour = bcd_to_decimal(hour); day = bcd_to_decimal(day); month = bcd_to_decimal(month); year = bcd_to_decimal(year); // end conversion // update time array Time[7] = second % 10 + '0'; Time[6] = second / 10 + '0'; Time[4] = minute % 10 + '0'; Time[3] = minute / 10 + '0'; Time[1] = hour % 10 + '0'; Time[0] = hour / 10 + '0'; // update date array Date[9] = year % 10 + '0'; Date[8] = year / 10 + '0'; Date[4] = month % 10 + '0'; Date[3] = month / 10 + '0'; Date[1] = day % 10 + '0'; Date[0] = day / 10 + '0'; // update temperature array if(temperature_msb < 0) { temperature_msb = abs(temperature_msb); Temperature[0] = '-'; } else Temperature[0] = ' '; temperature_lsb >>= 6; Temperature[2] = temperature_msb % 10 + '0'; Temperature[1] = temperature_msb / 10 + '0'; if(temperature_lsb == 0 || temperature_lsb == 2) { Temperature[5] = '0'; if(temperature_lsb == 0) Temperature[4] = '0'; else Temperature[4] = '5'; } if(temperature_lsb == 1 || temperature_lsb == 3) { Temperature[5] = '5'; if(temperature_lsb == 1) Temperature[4] = '2'; else Temperature[4] = '7'; } // print date SSD1306_GotoXY(6, 3); SSD1306_Print(Date); // print time SSD1306_GotoXY(7, 5); SSD1306_Print(Time); // print temperature SSD1306_GotoXY(7, 8); SSD1306_Print(Temperature); // print temperature SSD1306_PutCustomC(degree); // put degree symbol ( ° ) SSD1306_PutC('C'); } // make 'editing' parameter blinks function void blink() { char j = 0; while(j < 100 && button1 && button2) { j++; delay_ms(5); } } // edit time and date function char edit(char x_pos, char y_pos, char parameter) { while(debounce()); // call debounce function (wait for B1 to be released) while(1) { while(!button2) // if button B2 is pressed { parameter++; if(i == 0 && parameter > 31) // if date > 31 ==> date = 1 parameter = 1; if(i == 1 && parameter > 12) // if month > 12 ==> month = 1 parameter = 1; if(i == 2 && parameter > 99) // if year > 99 ==> year = 0 parameter = 0; if(i == 3 && parameter > 23) // if hours > 23 ==> hours = 0 parameter = 0; if(i == 4 && parameter > 59) // if minutes > 59 ==> minutes = 0 parameter = 0; SSD1306_GotoXY(x_pos, y_pos); SSD1306_PutC(parameter / 10 + '0'); SSD1306_PutC(parameter % 10 + '0'); delay_ms(200); // wait 200ms } SSD1306_GotoXY(x_pos, y_pos); SSD1306_Print(" "); blink(); SSD1306_GotoXY(x_pos, y_pos); SSD1306_PutC(parameter / 10 + '0'); SSD1306_PutC(parameter % 10 + '0'); blink(); if(!button1) // if button B1 is pressed if(debounce()) // call debounce function (make sure if B1 is pressed) { i++; // increment 'i' for the next parameter return parameter; // return parameter value and exit } } } // main function void main() { OSCCON = 0x70; // set internal oscillator to 8MHz ANSELH = 0; // configure all PORTB pins as digital // enable RB0 and RB1 internal pull ups NOT_RBPU_bit = 0; // clear RBPU bit (OPTION_REG.7) WPUB = 0x03; // WPUB register = 0b00000011 delay_ms(1000); // wait a second I2C1_Init(400000); // initialize I2C communication with clock frequency of 400KHz // initialize the SSD1306 OLED with an I2C addr = 0x7A (default address) SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); // clear the whole display SSD1306_ClearDisplay(); SSD1306_GotoXY(3, 7); // move cursor to column 3, row 7 SSD1306_Print("CHIP TEMPERATURE:"); while(1) { if(!button1) // if button B1 is pressed if(debounce()) // call debounce function (make sure if B1 is pressed) { i = 0; while(debounce()); // call debounce function (wait for button B1 to be released) while(1) { while(!button2) // while button B2 pressed { w_day++; // increment w_day if(w_day > 7) w_day = 1; display_day(); // call display_day function delay_ms(200); // wait 200 ms } SSD1306_GotoXY(7, 1); SSD1306_Print(" "); blink(); // call blink function display_day(); // call display_day function blink(); // call blink function if(!button1) // if button B1 is pressed break; } day = edit(6, 3, day); // edit date month = edit(9, 3, month); // edit month year = edit(14, 3, year); // edit year hour = edit(7, 5, hour); // edit hours minute = edit(10, 5, minute); // edit minutes // convert decimal to BCD minute = decimal_to_bcd(minute); hour = decimal_to_bcd(hour); day = decimal_to_bcd(day); month = decimal_to_bcd(month); year = decimal_to_bcd(year); // end conversion while(debounce()); // call debounce function (wait for button B1 to be released) // write data to RTC chip I2C1_Start(); // start I2C I2C1_Wr(0xD0); // RTC chip address I2C1_Wr(0); // send register address I2C1_Wr(0); // reset seconds and start oscillator I2C1_Wr(minute); // write minute value to RTC chip I2C1_Wr(hour); // write hour value to RTC chip I2C1_Wr(w_day); // write day value (not used) I2C1_Wr(day); // write date value to RTC chip I2C1_Wr(month); // write month value to RTC chip I2C1_Wr(year); // write year value to RTC chip I2C1_Stop(); // stop I2C delay_ms(200); // wait 200ms } // read current time and date from the RTC chip I2C1_Start(); // start I2C I2C1_Wr(0xD0); // RTC chip address I2C1_Wr(0); // send register address 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 w_day = I2C1_Rd(1); // read day from register 3 (not used) day = 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 // read chip temperature I2C1_Start(); // start I2C I2C1_Wr(0xD0); // RTC chip address (temperature MSB register) I2C1_Wr(0x11); // send register address I2C1_Repeated_Start(); // restart I2C I2C1_Wr(0xD1); // initialize data read temperature_msb = I2C1_Rd(1); // read temperature MSB temperature_lsb = I2C1_Rd(0); // read temperature LSB I2C1_Stop(); // stop I2C // print time, date and chip temperature display_day(); // print day of the week RTC_Display(); // print time, date and chip temperature delay_ms(50); // wait 50 ms } } // end of code. |
Hardware circuit result of this project should be similar to what’s shown in the following video where PIC16F877A microcontroller is used:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.