This post shows how to build a real time clock using PIC16F877A microcontroller and DS1307 RTC chip where time and date are displayed on an OLED display with SSD1306 controller (128×64 Pixel). With the help of a 3V coin cell battery the DS1307 keeps the time running even if the main power source is cut. The compiler used in this project is CCS PIC C, hardware circuit diagram and Proteus simulation are below.
Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC16F877A with SSD1306 OLED display
Real time clock using PIC16F877A and DS1307 RTC – CCS C
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 …
To simplify project circuit, the SSD1306 display and the DS1307 RTC chip share the same I2C bus, always the PIC16F877A microcontroller talks with one device only because their I2C slave addresses are different.
Hardware Required:
- PIC16F877A microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DS1307 RTC — datasheet
- 32.768KHz crystal oscillator
- 8 MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- 3 x 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.
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 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 | /************************************************************************************** Real time clock/calendar using PIC16F877A microcontroller, SSD1306 OLED display (128x64 Pixel) and DS1307 RTC chip. C Code for CCS C compiler Crystal oscillator used @ 8MHz 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 = 100000, stream = SSD1306_STREAM) // initialize I2C // include SSD1306 OLED driver source code #include <SSD1306.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() { 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 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 + '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'; SSD1306_GotoXY(6, 3); printf(SSD1306_PutC, Date); // Print date SSD1306_GotoXY(7, 8); printf(SSD1306_PutC, Time,); // 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) { 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(9, 6); SSD1306_PutC("TIME:"); 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, 8, hour); // Edit hours minute = edit(10, 8, 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 delay_ms(50); // Wait 50ms } } // End of code. |
PIC16F877A + SSD1306 OLED + DS1307 RTC Proteus 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.
Proteus simulation file download (for version 8.6 and higher):
PIC16F877A + SSD1306 OLED + DS1307
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Dear Sir,
Please make the different type of IR2110 applications with Pic16F84A and CCS C..
Thanks,