This post shows how to build a real time clock/calendar with temperature monitor using PIC16F877A microcontroller, DS3231 RTCC 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 DS3231 is more accurate than the DS1307 due to its built-in temperature sensor. It also (the DS3231) keeps the time running if the main power source is down (with the help of a coin cell battery). It also uses I2C interface to communicate with the master device which is in this project PIC16F877A 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:
The following topics may contain some useful information about the current project.
Interfacing PIC16F877A with SSD1306 OLED display
Real time clock with PIC16F877A, SSD1306 OLED and DS1307
Real time clock and calendar using DS3231 and PIC16F877A
The SSD1306 OLED used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode or I2C mode) such as soldering, placing jumpers …
Hardware Required:
- PIC16F877A microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DS3231 board — DS3231 RTC datasheet
- 8 MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- 10k ohm resistor
- 2 x push button
- 3V coin cell battery
- 5V power source
- Breadboard
- Jumper wires
The Circuit:
The following image shows project circuit diagram.
(All grounded terminals are connected together)
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 PORTB pins are enabled in the code.
PIC16F877A microcontroller, DS3231 board and SSD1306 display board are supplied with 5V source between their VDD (or VCC) pin and GND pin.
In this project the PIC16F877A microcontroller runs with crystal oscillator of 8MHz.
The Code:
The C code below is for CCS C compiler, it was tested with version 5.051.
To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, it’s name is SSD1306.C, its download link is below:
SSD1306 OLED display driver
after the download, add the driver file to the project folder or CCS C compiler drivers 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.
CCS C code:
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 | /************************************************************************************** Real time clock/calendar with chip temperature monitor using PIC16F877A microcontroller, SSD1306 OLED display (128x64 Pixel) and DS3231 RTC chip. C Code for CCS C compiler Crystal oscillator used @ 8MHz This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // SSD1306 OLED reset pin definition #define SSD1306_RST PIN_D4 #define button1 PIN_B0 // Button B1 is connected to RB0 pin #define button2 PIN_B1 // Button B2 is connected to RB1 pin #include <16F877A.h> #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock = 8MHz) #use fast_io(B) #use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // initialize I2C // include SSD1306 OLED driver source code #include <SSD1306.c> // Variables declaration char Time[] = " : : "; char Date[] = " / /20 "; char temperature[] = "000.00"; signed int8 temperature_msb; int8 i, second, minute, hour, w_day, day, month, year, temperature_lsb; char degree[] = {0, 7, 5, 7, 0}; // degree symbol custom char // Function for display day of the week void display_day() { SSD1306_GotoXY(7, 1); switch(w_day){ case 1: SSD1306_PutC(" SUNDAY "); break; case 2: SSD1306_PutC(" MONDAY "); break; case 3: SSD1306_PutC(" TUESDAY "); break; case 4: SSD1306_PutC("WEDNESDAY"); break; case 5: SSD1306_PutC("THURSDAY "); break; case 6: SSD1306_PutC(" FRIDAY "); break; default: SSD1306_PutC("SATURDAY "); } } 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); day = (day >> 4) * 10 + (day & 0x0F); month = (month >> 4) * 10 + (month & 0x0F); year = (year >> 4) * 10 + (year & 0x0F); // End conversion 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'; 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'; 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'; } SSD1306_GotoXY(6, 3); printf(SSD1306_PutC, Date); // Print date SSD1306_GotoXY(7, 5); printf(SSD1306_PutC, Time,); // Print time SSD1306_GotoXY(7, 8); printf(SSD1306_PutC, temperature); // Print temperature SSD1306_PutCustomC(degree); // degree symbol ( ° ) SSD1306_PutC('C'); } void blink_parameter() { int8 j = 0; while(j < 10 && input(button1) && input(button2)) { j++; delay_ms(25); } } int8 edit(int8 x_pos, int8 y_pos, int8 parameter) { delay_ms(50); while(!input(button1)); // Wait for button B1 release while(true){ while(!input(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); printf(SSD1306_PutC, "%02u", parameter); delay_ms(200); // Wait 200ms } SSD1306_GotoXY(x_pos, y_pos); SSD1306_PutC(" "); blink_parameter(); SSD1306_GotoXY(x_pos, y_pos); printf(SSD1306_PutC, "%02u", parameter); blink_parameter(); if(!input(button1)){ // If button B1 is pressed i++; // Increament 'i' for the next parameter return parameter; // Return parameter value and exit } } } // main function void main(void) { port_b_pullups(TRUE); // Enable PORTB internal weak pull-ups delay_ms(1000); // 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); SSD1306_PutC("CHIP TEMPERATURE:"); while (TRUE) { if(!input(button1)) { // If button B1 is pressed i = 0; delay_ms(50); while(!input(button1)); // Wait for button B1 release while(TRUE) { while(!input(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_PutC(" "); blink_parameter(); // Call blink_parameter function display_day(); // Call display_day function blink_parameter(); // Call blink_parameter function if(!input(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 = ((minute / 10) << 4) + (minute % 10); hour = ((hour / 10) << 4) + (hour % 10); day = ((day / 10) << 4) + (day % 10); month = ((month / 10) << 4) + (month % 10); year = ((year / 10) << 4) + (year % 10); // End conversion // Write data to DS3231 RTC i2c_start(SSD1306_STREAM); // Start I2C i2c_write(SSD1306_STREAM, 0xD0); // DS3231 address i2c_write(SSD1306_STREAM, 0); // Send register address i2c_write(SSD1306_STREAM, 0); // Reset sesonds and start oscillator i2c_write(SSD1306_STREAM, minute); // Write minute value to DS3231 i2c_write(SSD1306_STREAM, hour); // Write hour value to DS3231 i2c_write(SSD1306_STREAM, w_day); // Write day of week value to DS3231 i2c_write(SSD1306_STREAM, day); // Write day value to DS3231 i2c_write(SSD1306_STREAM, month); // Write month value to DS3231 i2c_write(SSD1306_STREAM, year); // Write year value to DS3231 i2c_stop(SSD1306_STREAM); // Stop I2C delay_ms(200); // Wait 200ms } // Read current time and date i2c_start(SSD1306_STREAM); // Start I2C i2c_write(SSD1306_STREAM, 0xD0); // DS3231 address i2c_write(SSD1306_STREAM, 0); // Send register address i2c_start(SSD1306_STREAM); // Restart I2C i2c_write(SSD1306_STREAM, 0xD1); // Initialize data read second = i2c_read(SSD1306_STREAM, 1); // Read seconds from register 0 minute = i2c_read(SSD1306_STREAM, 1); // Read minuts from register 1 hour = i2c_read(SSD1306_STREAM, 1); // Read hour from register 2 w_day = i2c_read(SSD1306_STREAM, 1); // Read day of week from register 3 day = i2c_read(SSD1306_STREAM, 1); // Read day from register 4 month = i2c_read(SSD1306_STREAM, 1); // Read month from register 5 year = i2c_read(SSD1306_STREAM, 0); // Read year from register 6 i2c_stop(SSD1306_STREAM); // Stop I2C // Read temperature i2c_start(SSD1306_STREAM); // Start I2C i2c_write(SSD1306_STREAM, 0xD0); // DS3231 address i2c_write(SSD1306_STREAM, 0x11); // Send register address (temperature MSB register) i2c_start(SSD1306_STREAM); // Restart I2C i2c_write(SSD1306_STREAM, 0xD1); // Initialize data read temperature_msb = i2c_read(SSD1306_STREAM, 1); // Read temperature MSB temperature_lsb = i2c_read(SSD1306_STREAM, 0); // Read temperature LSB i2c_stop(SSD1306_STREAM); // Stop I2C display_day(); // Display day of the week DS3231_display(); // Diaplay time & Date delay_ms(50); // Wait 50ms } } // End of code. |
PIC16F877A + SSD1306 OLED + DS3231 Project video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
I’m confused about what the SSD_1306_STREAM is. Yhanks