This post shows how to build a real time clock/calendar and temperature monitor using PIC18F4550 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.
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 PIC18F4550 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 OLED address is 0x7A.
In the circuit there are two push buttons for setting time and date of the real time clock.
Related Projects:
Interfacing PIC18F4550 with SSD1306 OLED
PIC18F4550 Real time clock with DS1307 and SSD1306 display
Real time clock & calendar with PIC18F4550 and DS3231 – CCS C
Hardware Required:
- PIC18F4550 microcontroller
- DS3231 board — DS3231 RTC datasheet
- SSD1306 OLED (I2C mode)
- 2 x push button
- 3V coin cell battery
- Power source with 5VDC
- Breadboard
- Jumper wires
The Circuit:
Project circuit diagram is shown by the following image.
(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 RB4 (#37) and B2 is connected to pin RB3 (#36). Internal pull-ups for PORTB pins are enabled in the code.
PIC18F4550 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 PIC18F4550 runs with its internal oscillator @ 8MHz and MCLR pin function is disabled.
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 SSD1306OLED.C, for more information about this driver, visit the following post:
SSD1306 OLED Library for CCS C compiler
It can be downloaded also from the link below:
SSD1306 OLED Library download
after the download, add the driver file to 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 | /****************************************************************************! PIC18F4550 Real time clock and temperature monitor with DS3231 and SSD1306 OLED C Code for CCS C compiler Internal oscillator used @ 8MHz http://simple-circuit.com/ *******************************************************************************/ // SSD1306 OLED reset pin definition #define SSD1306_RST PIN_B2 #define button1 PIN_B4 // Button B1 is connected to RB4 pin #define button2 PIN_B3 // Button B2 is connected to RB3 pin #include <18F4550.h> #device PASS_STRINGS = IN_RAM #fuses NOMCLR, INTRC_IO, 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 <SSD1306OLED.c> // Variables declaration char Time[] = " : : "; char Date[] = " / /20 "; char temperature[] = " 00.00"; signed int8 temperature_msb; int8 i, second, minute, hour, w_day, day, month, year, temperature_lsb; // Function for display day of the week void display_day() { switch(w_day){ case 1: SSD1306_Drawtext(40, 0, " SUNDAY ", 1); break; case 2: SSD1306_Drawtext(40, 0, " MONDAY ", 1); break; case 3: SSD1306_Drawtext(40, 0, " TUESDAY ", 1); break; case 4: SSD1306_Drawtext(40, 0, "WEDNESDAY", 1); break; case 5: SSD1306_Drawtext(40, 0, "THURSDAY ", 1); break; case 6: SSD1306_Drawtext(40, 0, " FRIDAY ", 1); break; default: SSD1306_Drawtext(40, 0, "SATURDAY ", 1); } } 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 + 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; Date[9] = year % 10 + 48; Date[8] = year / 10 + 48; Date[4] = month % 10 + 48; Date[3] = month / 10 + 48; Date[1] = day % 10 + 48; Date[0] = day / 10 + 48; if(temperature_msb < 0) { temperature_msb = abs(temperature_msb); temperature[0] = '-'; } else temperature[0] = ' '; temperature_lsb >>= 6; temperature[2] = temperature_msb % 10 + 48; temperature[1] = temperature_msb / 10 + 48; 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_DrawText(4, 14, Date, 2); // Print date SSD1306_DrawText(16, 35, Time, 2); // Print time SSD1306_DrawText(80, 56, temperature, 1); // Print temperature } 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) { char text[3]; sprintf(text,"%02u", 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 day > 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; sprintf(text,"%02u", parameter); SSD1306_Drawtext(x_pos, y_pos, text, 2); SSD1306_Display(); delay_ms(200); // Wait 200ms } SSD1306_DrawText(x_pos, y_pos, " ", 2); SSD1306_Display(); blink_parameter(); SSD1306_DrawText(x_pos, y_pos, text, 2); SSD1306_Display(); 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) { setup_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz 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_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); // Clear the buffer SSD1306_ClearDisplay(); SSD1306_DrawText(0, 56, "TEMPERATURE ="); SSD1306_DrawRect(117, 56, 3, 3); // Put degree symbol ( ° ) SSD1306_DrawText(122, 56, "C"); SSD1306_Display(); 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 SSD1306_Display(); delay_ms(200); // Wait 200 ms } SSD1306_DrawText(40, 0, " ", 1); SSD1306_Display(); blink_parameter(); // Call blink_parameter function display_day(); // Call display_day function SSD1306_Display(); blink_parameter(); // Call blink_parameter function if(!input(button1)) // If button B1 is pressed break; } day = edit(4, 14, day); // Edit date month = edit(40, 14, month); // Edit month year = edit(100, 14, year); // Edit year hour = edit(16, 35, hour); // Edit hours minute = edit(52, 35, 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 SSD1306_Display(); delay_ms(50); // Wait 50ms } } // End of code. |
Project video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.