This PIC MCU project shows how to interface PIC18F46K22 microcontroller with Nokia 5110 (Nokia 3310) graphical LCD screen. In this project I’m going to use mikroC PRO for PIC compiler.
The name Nokia 5110 (3310) comes from Nokia 5110 (or Nokia 3310) mobile phone. The Nokia 5110 LCD has a controller named PCD8544, it is similar to the Nokia 5110 mobile phone LCD, it uses SPI interface protocol with maximum clock frequency of 4MHz, it requires 5 control pins (at most), it’s low cost and easy to use.
The resolution of this LCD is 84 x 48 which means it has 4032 pixels. This module works with 3.3V only and it doesn’t support 5V (it’s not 5V tolerant), this means interfacing it with 5V microcontroller such as PIC18F46K22 MCU may require voltage level shifter.
The Nokia 5110 LCD module is shown below:
This module 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 pins which may be connected to the microcontroller are: RST, CE, DC, Din and Clk.
Nokia 5110 driver for mikroC compiler:
To simplify the interfacing C code, I wrote a simple driver (library) for the Nokia 5110 LCD (PCD8544 controller).
This small driver and graphics library allows us to print texts, draw lines, circles and many other function (listed below). This library is just a .C file, its full name (with extension): NOKIA5110.C. It may be installed by adding it to mikroC project folder.
The Nokia 5110 LCD library can be downloaded from the following URL:
Nokia 5110 LCD library for mikroC compiler
The connection of the LCD to the microcontroller is defined in the main code before including the library source file.
Example:
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 |
In this example all the 5 pins of the LCD module are defined.
The LCD_RST (reset) pin connection is optional, if it’s not used then it must be connected to VCC (+3.3V) because it’s active low.
The LCD_CS (chip select, or chip enable) pin connection is also optional, it has to be connected to GND if it’s not used (active low).
If the LCD clock and data pins (LCD_CLK and LCD_DAT) are defined, the library will automatically use software SPI. If they are not defined it will use hardware SPI1 module, and if the following line is defined the library will use hardware SPI2 module:
1 | #define LCD_HARD_SPI2 |
If hardware SPI module (SPI1 or SPI2) is used, it must be initialized before the initialization of the LCD (using SPIx_Init or SPIx_Init_Advanced where x 1 or 2).
The direction pins of the LCD may be defined as shown below:
1 2 3 4 5 | #define LCD_RST_DIR TRISD0_bit #define LCD_CS_DIR TRISD1_bit #define LCD_DC_DIR TRISD2_bit #define LCD_DAT_DIR TRISD3_bit #define LCD_CLK_DIR TRISD4_bit |
Nokia 5110 Library Functions:
This is a list of library functions.
LCD_Begin(): this function initializes the Nokia 5110 LCD.
LCD_SetContrast(con): sets the contrast of the display where: 0 <= con <= 63 .
LCD_Display(): prints data buffer on the display, must be called after any draw action.
LCD_Clear(): clears the whole LCD.
LCD_Fill(): fills the whole LCD (writes all LCD pixels).
LCD_SetRotation(rot): sets rotation setting for the display, rot may be in the range [0, 3].
LCD_GetRotation(): returns rotation setting of the display (value from 0 to 3).
LCD_GetWidth(): returns the width of the display, accounting for the current rotation.
LCD_GetHeight(): returns the height of the display, accounting for the current rotation.
LCD_TextSize(t_size): sets text size.
LCD_TextColor(t_color, t_bg): sets text color where t_color is text color and t_bg is background color. Both variables are of type bool (may be 1 or 0). Black color is defined as 1 (true) and white is defined as 0 (false).
LCD_GotoXY(x, y): moves cursor to position (x, y), x and y are in pixel.
LCD_TextWrap(w): sets text wrap, if w = 1 (true) text wrap is enabled (default), if w = 0 (false) text wrap is disabled.
LCD_PutC(c): prints a single character c on the LCD.
LCD_PutCustomC(*c): prints a custom character c on the display with 5×7 dimension (same as built-in font dimension), this character must be stored in ROM space.
LCD_Print(*s): prints a string s (text) on the LCD.
LCD_Invert(inv): inverts the display, inv may be 1 or 0.
LCD_DrawPixel(x, y, color): draws one pixel on the screen, x and y are the coordinates in pixel.
LCD_DrawLine(x0, y0, x1, y1, color): draws a line from (x0, y0) to (x1, y1).
LCD_DrawHLine(x, y, w, color): draws a horizontal line from (x, y) with width of w.
LCD_DrawVLine(x, y, h, color); draws a vertical line from (x, y) with height of h.
LCD_DrawRect(x, y, w, h, color): draws a rectangle from (x, y) with width w and height h.
LCD_FillRect(x, y, w, h, color): fills a rectangle from (x, y) with width w and height h.
LCD_DrawRoundRect(x, y, w, h, r, color): draws round rectangle from (x, y) with width w, height h and radius r.
LCD_FillRoundRect(x, y, w, h, r, color): fills round rectangle from (x, y) with width w, height h & radius r.
LCD_DrawTriangle(x0, y0, x1, y1, x2, y2, color): draws a triangle with points (x0, y0), (x1, y1) and (x2, y2).
LCD_FillTriangle(x0, y0, x1, y1, x2, y2, color): fills a triangle with points (x0, y0), (x1, y1) and (x2, y2).
LCD_DrawCircle(x0, y0, r, color): draws a circle from (x0, y0) with radius r.
LCD_FillCircle(x0, y0, r, color): fills a circle from (x0, y0) with radius r.
LCD_DrawCircleHelper(x0, y0, r, cornername, color): draws a circle helper from (x0, y0) with radius r and cornername.
LCD_FillCircleHelper(x0, y0, r, cornername, delta, color): fills a circle helper from (x0, y0) with radius r and cornername.
LCD_ROMBMP(x, y, *bitmap, w, h, color): draws a bitmap located in ROM.
LCD_RAMBMP(x, y, *bitmap, w, h, color): draws a bitmap located in RAM.
The variable color is of type bool (may be 1 or 0). Black color is defined as 1 (true) and white is defined as 0 (false).
Interfacing PIC18F46K22 MCU with NOKIA 5110 LCD:
This is a simple example that shows how to use Nokia 5110 LCD library for mikroC compiler.
Hardware Required:
- PIC18F46K22 microcontroller —-> datasheet
- Nokia 5110 (3310) LCD module
- AMS1117 3V3 voltage regulator
- 10 uF capacitor
- 100 nF ceramic capacitor
- 5 x 3.3k ohm resistor
- 5 x 2.2k ohm resistor
- 5V source
- Breadboard
- Jumper wires
Interfacing PIC18F46K22 MCU with Nokia 5110 LCD circuit:
The following image shows example circuit schematic diagram.
All the grounded terminals are connected together.
The Nokia 5110 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 directly 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. That 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.
Interfacing PIC18F46K22 MCU with Nokia 5110 LCD C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
This example is from Adafruit Nokia 5110 driver for Arduino (pcdtest.ino) with some modifications.
To be able to compile example C code, a driver for Nokia 5110 LCD is required, download link is above. After you download the driver file which named NOKIA5110.c, add it to the project folder.
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | /************************************************************************************** Interfacing PIC18F46K22 Nokia 5110 LCD (84x48 pixel resolution). 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/ ***************************************************************************************/ /********************************************************************* This is an example sketch for our Monochrome Nokia 5110 LCD Displays Pick one up today in the adafruit shop! ------> http://www.adafruit.com/products/338 These displays use SPI to communicate, 4 or 5 pins are required to interface Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, check license.txt for more information All text above, and the splash screen must be included in any redistribution *********************************************************************/ // 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 // include Nokia 5110 LCD driver source file #include <NOKIA5110.c> #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #define LOGO16_GLCD_HEIGHT 16 #define LOGO16_GLCD_WIDTH 16 // declare bmp image located in PIC ROM const uint8_t logo16_glcd_bmp[] = { 48, 112, 240, 240, 96, 96, 248, 158, 255, 127, 120, 224, 224, 192, 192, 192, 0, 96, 120, 125, 63, 59, 29, 31, 61, 123, 255, 243, 1, 1, 0, 0 }; void testdrawchar(void) { uint8_t i; LCD_TextSize(1); // set text size to 1 LCD_TextColor(BLACK, WHITE); // set text color to black with white background LCD_GotoXY(0, 0); // move cursor to position (0, 0) pixel (upper left) for (i = '!'; i < 117; i++) LCD_PutC(i); LCD_Display(); } void testdrawcircle(void) { int16_t i; for (i = 0; i < LCD_GetHeight(); i+=2) { LCD_DrawCircle(LCD_GetWidth()/2, LCD_GetHeight()/2, i, BLACK); LCD_Display(); } } void testfillrect(void) { uint8_t i, color = 1; for (i = 0; i < LCD_GetHeight()/2; i+=3) { // alternate colors LCD_FillRect(i, i, LCD_GetWidth()-i*2, LCD_GetHeight()-i*2, color%2); LCD_Display(); color++; } } void testdrawtriangle(void) { int16_t i; for (i = 0; i < min(LCD_GetWidth(), LCD_GetHeight())/2; i += 5) { LCD_DrawTriangle(LCD_GetWidth()/2, LCD_GetHeight()/2-i, LCD_GetWidth()/2-i, LCD_GetHeight()/2+i, LCD_GetWidth()/2+i, LCD_GetHeight()/2+i, BLACK); LCD_Display(); } } void testfilltriangle(void) { bool color = BLACK; int8_t i; for (i = min(LCD_GetWidth(), LCD_GetHeight())/2; i > 0; i -= 5) { LCD_FillTriangle(LCD_GetWidth()/2, LCD_GetHeight()/2-i, LCD_GetWidth()/2-i, LCD_GetHeight()/2+i, LCD_GetWidth()/2+i, LCD_GetHeight()/2+i, color); if (color == WHITE) color = BLACK; else color = WHITE; LCD_Display(); } } void testdrawroundrect(void) { uint8_t i; for (i = 0; i < LCD_GetHeight()/2 - 2; i += 2) { LCD_DrawRoundRect(i, i, LCD_GetWidth()-2*i, LCD_GetHeight()-2*i, LCD_GetHeight()/4, BLACK); LCD_Display(); } } void testfillroundrect(void) { bool color = BLACK; uint8_t i; for (i = 0; i < LCD_GetHeight()/2-2; i += 2) { LCD_FillRoundRect(i, i, LCD_GetWidth()-2*i, LCD_GetHeight()-2*i, LCD_GetHeight()/4, color); if (color == WHITE) color = BLACK; else color = WHITE; LCD_Display(); } } void testdrawrect(void) { uint8_t i; for (i = 0; i < LCD_GetHeight()/2; i += 2) { LCD_DrawRect(i, i, LCD_GetWidth()-2*i, LCD_GetHeight()-2*i, BLACK); LCD_Display(); } } void testdrawline() { int16_t i; for (i = 0; i < LCD_GetWidth(); i += 4) { LCD_DrawLine(0, 0, i, LCD_GetHeight()-1, BLACK); LCD_Display(); } for (i = 0; i < LCD_GetHeight(); i += 4) { LCD_DrawLine(0, 0, LCD_GetWidth()-1, i, BLACK); LCD_Display(); } delay_ms(250); LCD_Clear(); for (i = 0; i < LCD_GetWidth(); i += 4) { LCD_DrawLine(0, LCD_GetHeight()-1, i, 0, BLACK); LCD_Display(); } for (i = LCD_GetHeight() - 1; i >= 0; i -= 4) { LCD_DrawLine(0, LCD_GetHeight()-1, LCD_GetWidth()-1, i, BLACK); LCD_Display(); } delay_ms(250); LCD_Clear(); for (i = LCD_GetWidth() - 1; i >= 0; i -= 4) { LCD_DrawLine(LCD_GetWidth()-1, LCD_GetHeight()-1, i, 0, BLACK); LCD_Display(); } for (i = LCD_GetHeight()-1; i >= 0; i -= 4) { LCD_DrawLine(LCD_GetWidth()-1, LCD_GetHeight()-1, 0, i, BLACK); LCD_Display(); } delay_ms(250); LCD_Clear(); for (i = 0; i < LCD_GetHeight(); i += 4) { LCD_DrawLine(LCD_GetWidth()-1, 0, 0, i, BLACK); LCD_Display(); } for (i = 0; i < LCD_GetWidth(); i+=4) { LCD_DrawLine(LCD_GetWidth()-1, 0, i, LCD_GetHeight()-1, BLACK); LCD_Display(); } delay_ms(250); } void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { uint8_t icons[NUMFLAKES][3], f; // initialize for (f = 0; f < NUMFLAKES; f++) { icons[f][XPOS] = rand() % LCDWIDTH; icons[f][YPOS] = 0; icons[f][DELTAY] = (rand() % 5) + 1; } while (1) { // draw each icon for (f = 0; f < NUMFLAKES; f++) { LCD_ROMBMP(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); } LCD_Display(); delay_ms(200); // then erase it + move it for (f = 0; f< NUMFLAKES; f++) { LCD_ROMBMP(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); // move it icons[f][YPOS] += icons[f][DELTAY]; // if its gone, reinit if (icons[f][YPOS] > LCDHEIGHT) { icons[f][XPOS] = rand() % LCDWIDTH; icons[f][YPOS] = 0; icons[f][DELTAY] = (rand() % 5) + 1; } } } } // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELD = 0; // configure all PORTD pins as digital TRISD = 0; // configure all PORTD pins as outputs LCD_Begin(); // initialize the LCD LCD_SetContrast(50); // set LCD contrast LCD_Display(); // show splashscreen delay_ms(2000); LCD_Clear(); // clear the display buffer // draw a single pixel LCD_DrawPixel(10, 10, BLACK); LCD_Display(); // update the screen delay_ms(2000); LCD_Clear(); // clear the display buffer // draw many lines testdrawline(); LCD_Display(); // update the screen delay_ms(2000); LCD_Clear(); // clear the display buffer // draw rectangles testdrawrect(); LCD_Display(); delay_ms(2000); LCD_Clear(); // clear the display buffer // draw multiple rectangles testfillrect(); LCD_Display(); delay_ms(2000); LCD_Clear(); // clear the display buffer // draw mulitple circles testdrawcircle(); LCD_Display(); delay_ms(2000); LCD_Clear(); // clear the display buffer // draw a circle, 10 pixel radius LCD_FillCircle(LCD_GetWidth()/2, LCD_GetHeight()/2, 10, BLACK); LCD_Display(); delay_ms(2000); LCD_Clear(); // clear the display buffer testdrawroundrect(); delay_ms(2000); LCD_Clear(); // clear the display buffer testfillroundrect(); delay_ms(2000); LCD_Clear(); // clear the display buffer testdrawtriangle(); delay_ms(2000); LCD_Clear(); // clear the display buffer testfilltriangle(); delay_ms(2000); LCD_Clear(); // clear the display buffer // draw some characters in the font testdrawchar(); LCD_Display(); delay_ms(2000); LCD_Clear(); // clear the display buffer // text display tests LCD_TextSize(1); // set text size to 1 LCD_TextColor(BLACK, WHITE); // set text color to black with white background LCD_GotoXY(0, 0); // move cursor to position (0, 0) pixel (upper left) LCD_Print("Hello, world!\r\n"); LCD_TextColor(WHITE, BLACK); // 'inverted' text (text color is white with black background) { char txt[9]; FloatToStr(3.141592, txt); // convert float number to string LCD_Print(txt); LCD_Print("\r\n"); // start a new line LCD_TextSize(2); // set text size to 2 LCD_TextColor(BLACK, WHITE); // set text color to black with white background LCD_Print("0x"); LongIntToHex(0xDEADBEEF, txt); LCD_Print(txt); LCD_Display(); delay_ms(2000); } // rotation example LCD_Clear(); // clear the buffer LCD_SetRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further. LCD_TextSize(1); // set text size to 1 LCD_TextWrap(false); // disable text wrap LCD_GotoXY(0, 0); // move cursor to position (0, 0) pixel (upper left) LCD_Print("Rotation\r\n"); LCD_TextSize(2); // text size = 2 LCD_TextWrap(true); // enable text wrap LCD_Print("Example!\r\n"); LCD_Display(); delay_ms(2000); // revert back to no rotation LCD_SetRotation(0); // miniature bitmap display LCD_Clear(); LCD_ROMBMP(30, 16, logo16_glcd_bmp, 16, 16, 1); LCD_Display(); // invert the display LCD_Invert(true); // invert the display delay_ms(1000); LCD_Invert(false); // turn off display invert delay_ms(1000); // draw a bitmap icon and 'animate' movement testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT); } // end of code. |
Testing example hardware circuit gave me a result near to what’s shown in this video where Arduino UNO board is used instead of the PIC18F46K22 microcontroller:
Proteus simulation:
We can simulate this project using Proteus software. Simulation result should be near to the one shwn in this video:
Proteus simulation file download link is below, use version 8.6 or higher to open it:
PIC18F46K22 and Nokia 5110 Proteus simulation
PIC Projects where Nokia 5110 LCD is used:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.