This tutorial shows how to make a simple real time clock & calendar (RTCC) using PIC18F46K22 microcontroller and DS3231 RTCC board.
In this project time and date are printed on Nokia 5110 (3310) monochrome LCD (84×48 pixel resolution) and they may be set with two push buttons connected to the PIC18F46K22 MCU.
The compiler used in this project is MikroElektronika mikroC PRO for PIC.
To see how to interface PIC18F46K22 MCU with Nokia 5110 LCD using mikroC compiler, visit this post:
Interfacing PIC MCU with Nokia 5110 LCD | mikroC Projects
The DS3231 RTC has built-in two alarm functions and temperature sensor with resolution of 0.25 °C and accuracy of ±3 °C. Chip temperature also will be printed on the Nokia 5110 LCD.
Hardware Required:
- PIC18F46K22 microcontroller —-> datasheet
- Nokia 5110 LCD screen
- DS3231 board —-> DS3231 datasheet
- AMS1117 3V3 voltage regulator
- 10 uF capacitor
- 100 nF ceramic capacitor
- 5 x 3.3k ohm resistor
- 5 x 2.2k ohm resistor
- 2 x push button
- 3V coin cell battery
- 5V power source
- Breadboard
- Jumper wires
PIC18F46K22 with Nokia 5110 LCD and DS3231 circuit:
The following image shows project hardware circuit schematic diagram.
The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). SDA1 and SCL1 pins of the PIC18F46K22 MCU are respectively connected to SDA and SCL pins of the DS3231 RTC board.
All the grounded terminals are connected together.
The Nokia 5110 LCD module which is shown in the circuit diagram has 8 pins (from left to right): RST (reset), CE (chip enable), DC (or D/C: data/command), Din (data in), Clk (clock), VCC (3.3V), BL (back light) and Gnd (ground).
The Nokia 5110 LCD works with 3.3V only (power supply and control lines). The LCD module is supplied with 3.3V which comes from the AMS1117 3V3 voltage regulator, this regulator steps down the 5V into 3.3V (supplies the LCD controller PCD8544 with regulated 3V3).
All PIC18F46K22 microcontroller output pins are 5V, connecting a 5V pin to the Nokia 5110 LCD may damage its controller circuit!
To connect the PIC18F46K22 to the LCD module, I used voltage divider for each line which means there are 5 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.
So, the Nokia 5110 LCD pins are connected to PIC18F46K22 MCU as follows (each one through voltage divider):
RST (reset) pin is connected to pin RD0 (#19),
CE (chip enable) pin is connected to pin RD1 (#20),
DC (data/command) pin is connected to pin RD2 (#21),
DIN (data in) pin is connected to pin RD3 (#22),
CLK (clock) pin is connected to pin RD4 (#27),
VCC and BL are connected to AMS1117 3V3 regulator output pin and GND is connected to circuit ground (0V).
In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.
PIC18F46K22 with Nokia 5110 LCD and DS3231 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 project C code, a driver for the Nokia 5110 LCD is required, its full name (with extension) is NOKIA5110.C, download link is the one below:
Nokia 5110 LCD library for mikroC compiler
Also, another library for the DS3231 RTC is required, its full name (with extension) is: DS3231.C, download link is the one below:
DS3231 mikroC library
and for more details about it, visit this topic:
DS3231 RTC Library for mikroC Compiler | mikroC Projects
after the download of the 2 library files, add both of them to mikroC project folder.
The connection of the LCD pins with the microcontroller are defined in the C code as shown below:
1 2 3 4 5 6 7 8 | // Nokia 5110 LCD module connections // use software SPI #define LCD_RST RD0_bit // reset pin, optional! #define LCD_CS RD1_bit // chip select pin, optional! #define LCD_DC RD2_bit // data/command pin #define LCD_DAT RD3_bit // data-in pin (MOSI) #define LCD_CLK RD4_bit // clock pin // end LCD module connections |
The drivers of the Nokia 5110 LCD and the DS3231 RTC are included in the code using the 2 lines below:
1 2 3 4 | // include Nokia 5110 LCD driver source file #include <NOKIA5110.c> // include DS3231 driver source file #include <DS3231.c> |
Functions used in the code:
char debounce (): this function is for button B1 debounce, returns 1 if button is debounced.
void dow_print(): displays day of the week (Monday, Tuesday …) on the LCD.
void rtc_print(): displays time, date and day of the week on the LCD. This function calls the previous one for displaying the day of the week.
void wait(): this function is just a delay of 500 ms except that it can be interrupted by the two push buttons (B1 and B2). In this function Timer0 module is used to count the 500 milliseconds, it is used as 16-bit timer with prescaler = 32 (==> 1 tick every 8 microseconds ==> 62500 x 8 = 500000 us = 500 ms).
char edit(char parameter): this function is for setting the real time clock, returns the edited parameter.
Rest of code is described through comments.
Full mikroC 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 | /************************************************************************************** Interfacing PIC18F46K22 microcontroller with Nokia 5110 LCD (84x48 Pixel) and DS3231 real time clock chip. C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 16MHz Configuration words: CONFIG1H = 0x0028 CONFIG2L = 0x0018 CONFIG2H = 0x003C CONFIG3H = 0x0037 CONFIG4L = 0x0081 CONFIG5L = 0x000F CONFIG5H = 0x00C0 CONFIG6L = 0x000F CONFIG6H = 0x00E0 CONFIG7L = 0x000F CONFIG7H = 0x0040 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // Nokia 5110 LCD module connections // use software SPI #define LCD_RST RD0_bit // reset pin, optional! #define LCD_CS RD1_bit // chip select pin, optional! #define LCD_DC RD2_bit // data/command pin #define LCD_DAT RD3_bit // data-in pin (MOSI) #define LCD_CLK RD4_bit // clock pin // end LCD module connections // 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 Nokia 5110 LCD driver source file #include <NOKIA5110.c> // include DS3231 driver source file #include <DS3231.c> RTC_Time *mytime; // DS3231 library variable // a small function for button1 (B1) debounce char debounce() { char m, count = 0; for(m = 0; m < 5; m++) { if ( !button1 ) count++; delay_ms(10); } if(count > 2) return 1; else return 0; } // interrupted delay void wait() { unsigned int TMR0_value = 0; TMR0L = TMR0H = 0; // rest Timer0 low and high registers while( (TMR0_value < 62500L) && button1 && button2 ) TMR0_value = (TMR0H << 8) | TMR0L; } char edit(char parameter) { static char i = 0; static const char x_pos[5] = {12, 30, 60, 18, 36}; char buffer[5]; while(debounce()); // call debounce function (wait for B1 to be released) if (i < 3) LCD_GotoXY(x_pos[i], 8); else LCD_GotoXY(x_pos[i], 24); sprinti(buffer, "%02u\b\b", (int)parameter); // '\b': move back by 1 position while(1) { while( !button2 ) // while button2 is pressed { parameter++; if(i == 0 && parameter > 31) // if day > 31 ==> day = 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; sprinti(buffer, "%02u\b\b", (int)parameter); LCD_Print(buffer); LCD_Display(); delay_ms(200); } LCD_Print(" \b\b"); LCD_Display(); wait(); LCD_Print(buffer); LCD_Display(); wait(); if(!button1) // if button B1 is pressed { i = (i + 1) % 5; // increment 'i' for the next parameter return parameter; // return parameter value and exit } } } void dow_print() { char dow_matrix[7][10] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}; static const char x_pos[7] = {24, 24, 21, 15, 18, 24, 18}; if( mytime->dow < 1 || mytime->dow > 7) mytime->dow = 1; LCD_FillRect(15, 0, 54, 8, WHITE); LCD_GotoXY( x_pos[mytime->dow - 1], 0 ); LCD_Print( dow_matrix[mytime->dow - 1] ); // now update the displayed screen LCD_Display(); } void rtc_print() { char buffer[11]; // print date sprinti(buffer, "%02u-%02u-20%02u", (int)mytime->day, (int)mytime->month, (int)mytime->year); LCD_GotoXY(12, 8); LCD_Print(buffer); // print time sprinti(buffer, "%02u:%02u:%02u", (int)mytime->hours, (int)mytime->minutes, (int)mytime->seconds); LCD_GotoXY(18, 24); LCD_Print(buffer); // print day of the week dow_print(); } // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELB = 0; // configure all PORTB pins as digital ANSELC = 0; // configure all PORTC pins as digital ANSELD = 0; // configure all PORTD pins as digital TRISD = 0; // configure all PORTD pins as outputs T0CON = 0x04; // configure Timer0 module (16-bit timer & prescaler = 32) // enable RB0 and RB1 internal pull ups RBPU_bit = 0; // clear RBPU bit (INTCON2.7) WPUB = 0x03; // WPUB register = 0b00000011 I2C1_Init(100000); // initialize I2C communication with clock frequency of 100kHz LCD_Begin(); // initialize the LCD LCD_Clear(); // clear the display buffer LCD_SetContrast(60); // set LCD contrast LCD_TextWrap(false); // disable text wrap LCD_TextSize(1); // set text size to 1 LCD_GotoXY(28, 16); // move cursor to position (28, 16) pixel LCD_Print("TIME:"); LCD_GotoXY(13, 32); // move cursor to position (13, 32) pixel LCD_Print("CHIP TEMP:"); LCD_Display(); // update the screen while(1) { // read current time and date from the RTC chip mytime = RTC_Get(); // read chip temperature { int chip_temp = Get_Temperature(); char buffer[9]; if (chip_temp < 0) // if temperature is negative sprinti(buffer, "-%02u.%02u C", abs(chip_temp) / 100, abs(chip_temp) % 100); else sprinti(buffer, " %02u.%02u C", chip_temp / 100, chip_temp % 100); LCD_GotoXY(15, 40); LCD_Print(buffer); LCD_DrawRect(53, 40, 3, 3, BLACK); // print degree symbol ( ° ) } // print time & date rtc_print(); if( !button1 ) // if button B1 is pressed if( debounce() ) // call debounce function (make sure if B1 is pressed) { while( debounce() ); // call debounce function (wait for button B1 to be released) TMR0ON_bit = 1; // enable Timer0 module while(1) { while( !button2 ) // while button B2 button is pressed { mytime->dow++; if(mytime->dow > SATURDAY) mytime->dow = SUNDAY; dow_print(); // print week day delay_ms(500); // wait half a second } LCD_FillRect(15, 0, 54, 8, WHITE); LCD_Display(); wait(); // call wait() function dow_print(); // print week day wait(); // call wait() function if( !button1 ) // if button B1 is pressed break; } mytime->day = edit(mytime->day); // edit day (day of month) mytime->month = edit(mytime->month); // edit month mytime->year = edit(mytime->year); // edit year mytime->hours = edit(mytime->hours); // edit hours mytime->minutes = edit(mytime->minutes); // edit minutes mytime->seconds = 0; // reset seconds TMR0ON_bit = 0; // disable Timer0 module while(debounce()); // call debounce function (wait for button B1 to be released) // write data to the RTC chip RTC_Set(mytime); } // end 'if( debounce() )' delay_ms(100); // wait 100 milliseconds } } // end of code. |
The following picture shows my protoboard hardware circuit:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.