Let’s continue the SSD1306 OLED display projects and now I’m going to build a simple real time clock with PIC18F4550 microcontroller and DS1307 real time clock chip, time and date are displayed on the SSD1306 display.
With the help of a 3V coin cell battery, the DS1307 RTC chip keeps the time running even if the main power supply is off. Time and date are set with two push buttons.
The SSD1306 OLED used in this post 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 …
To simplifies project circuit, the SSD1306 display and the DS1307 RTC chip are connected to the same I2C bus, always the PIC18F4550 microcontroller talks with one device only because their I2C slave addresses are different.
Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC18F4550 with SSD1306 OLED
Real time clock with PIC18F4550 and DS1307 RTC – CCS C
Hardware Required:
- PIC18F4550 microcontroller —> datasheet
- SSD1306 OLED display (128×64 Pixel)
- DS1307 RTC —> datasheet
- 32.768KHz crystal oscillator
- 2 x 10K ohm resistor
- 2 x push button
- 3V coin cell battery
- 5V power source
- Breadboard
- Jumper wires
- PIC microcontroller programmer (PICkit 3, PICkit 4…)
PIC18F4550 Real time clock with DS1307 and SSD1306 display 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 RB4 (#37) and B2 is connected to pin RB3 (#36). Internal pull-ups for PORTB pins are enabled in the code.
In this project the PIC18F4550 runs with its internal oscillator @ 8MHz and MCLR pin function is disabled.
PIC18F4550 Real time clock with DS1307 and SSD1306 display C 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 DS1307 RTC and the SSD1306 OLED display share the same I2C bus, DS1307 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 | /****************************************************************************! PIC18F4550 microcontroller with SSD1306 OLED (128x64 Pixel) and DS1307 RTC This code works also with DS3231 & DS3232 C Code for CCS C compiler Internal oscillator used @ 8MHz https://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 = 100000, stream = SSD1306_STREAM) // Initialize I2C // Include SSD1306 OLED driver source code #include <SSD1306OLED.c> // Variables declaration char Time[] = " : : "; char Date[] = " / /20 "; int8 i, second, minute, hour, w_day, day, month, year; // 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 DS1307_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; SSD1306_DrawText(4, 18, Date, 2); // Print date SSD1306_DrawText(16, 42, Time, 2); // Print time } 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 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; 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_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, 18, day); // Edit date month = edit(40, 18, month); // Edit month year = edit(100, 18, year); // Edit year hour = edit(16, 42, hour); // Edit hours minute = edit(52, 42, 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 DS1307 RTC i2c_start(SSD1306_STREAM); // Start I2C i2c_write(SSD1306_STREAM, 0xD0); // DS1307 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 DS1307 i2c_write(SSD1306_STREAM, hour); // Write hour value to DS1307 i2c_write(SSD1306_STREAM, w_day); // Write day of week value to DS1307 i2c_write(SSD1306_STREAM, day); // Write day value to DS1307 i2c_write(SSD1306_STREAM, month); // Write month value to DS1307 i2c_write(SSD1306_STREAM, year); // Write year value to DS1307 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); // DS1307 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 display_day(); // Display day of the week DS1307_display(); // Diaplay time & Date SSD1306_Display(); delay_ms(50); // Wait 50ms } } // End of code. |
Project simulation:
The video below shows the simulation of the project with Proteus ISIS. Note that simulation circuit is not the same as actual hardware circuit, project hardware circuit is shown above (don’t try to build your hardware circuit as shown in the simulation video, otherwise your circuit may not work or you’ll damage your components!).
Proteus simulation file download (for version 8.6 or higher):
PIC18F4550 + SSD1306 OLED + DS1307
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.