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 CCS C 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.
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.
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).
All the grounded terminals are connected together.
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 @ 32 MHz, MCLR pin is configured as an input pin.
Interfacing PIC18F46K22 MCU with Nokia 5110 LCD C code:
The following C code is for CCS C compiler, it was tested with versions 5.051 and 5.083.
To be able to compile project C code with no error, 2 libraries are required:
The first library is a driver for the Nokia 5110 LCD (PCD8544 controller), its full name (with extension) is PCD8544.c, download link is below:
Nokia 5110 LCD library for CCS C compiler
The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for CCS C compiler
after the download of the 2 library files, add both of them to the project folder.
Hints:
The 2 library files are included in the main code as shown below:
1 2 | #include <PCD8544.c> // include PCD8544 controller driver source code (Nokia 5110 LCD driver) #include <GFX_Library.c> // include graphics library source code |
Connections of LCD pins to the microcontroller are defined as:
1 2 3 4 5 6 | // define Nokia LCD module pin connections #define LCD_RST PIN_D0 // reset pin, optional! #define LCD_CS PIN_D1 // chip select pin #define LCD_DC PIN_D2 // data/command pin #define LCD_DAT PIN_D3 // data-in pin (MOSI) #define LCD_CLK PIN_D4 // clock pin |
Software SPI is initialized using #use function as shown below:
1 | #use SPI(DO = LCD_DAT, CLK = LCD_CLK, MODE = 0, BITS = 8, STREAM = LCD) |
And the LCD is initialized as:
1 2 | // initialize the LCD LCD_begin(); |
Rest of code is described through comments.
Full 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 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 346 347 348 349 | /************************************************************************** Interfacing PIC18F46K22 microcontroller with Nokia 5110 (3310) LCD display. C Code for CCS C compiler. 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 *********************************************************************/ // define Nokia LCD module pin connections #define LCD_RST PIN_D0 // reset pin, optional! #define LCD_CS PIN_D1 // chip select pin #define LCD_DC PIN_D2 // data/command pin #define LCD_DAT PIN_D3 // data-in pin (MOSI) #define LCD_CLK PIN_D4 // clock pin #include <18F46K22.h> #fuses NOMCLR,NOLVP,NOBROWNOUT,PUT,NOXINST #use delay(internal = 32MHz) #use SPI(DO = LCD_DAT, CLK = LCD_CLK, MODE = 0, BITS = 8, STREAM = LCD) #include <PCD8544.c> // include PCD8544 controller driver source code (Nokia 5110 LCD driver) #include <GFX_Library.c> // include graphics library source code // define random max number #define RAND_MAX (display_width - 1) // include std library source code (required by rand() function) #include <stdlib.h> #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #define LOGO16_GLCD_HEIGHT 16 #define LOGO16_GLCD_WIDTH 16 rom char logo16_glcd_bmp[] = { 0b00000000, 0b11000000, 0b00000001, 0b11000000, 0b00000001, 0b11000000, 0b00000011, 0b11100000, 0b11110011, 0b11100000, 0b11111110, 0b11111000, 0b01111110, 0b11111111, 0b00110011, 0b10011111, 0b00011111, 0b11111100, 0b00001101, 0b01110000, 0b00011011, 0b10100000, 0b00111111, 0b11100000, 0b00111111, 0b11110000, 0b01111100, 0b11110000, 0b01110000, 0b01110000, 0b00000000, 0b00110000 }; void testdrawbitmap(rom uint8_t *bitmap, uint8_t w, uint8_t h) { static uint8_t icons[NUMFLAKES][3], f, x_pos, y_pos; // initialize for (f=0; f< NUMFLAKES; f++) { icons[f][XPOS] = rand(); icons[f][YPOS] = 0; icons[f][DELTAY] = (rand() % 5) + 1; } icons[0][XPOS] = 30; while (TRUE) { uint8_t f; // draw each icon for (f=0; f< NUMFLAKES; f++) { x_pos = icons[f][XPOS]; y_pos = icons[f][YPOS]; display_drawBitmapV2(x_pos, y_pos, bitmap, w, h, BLACK); } display(); delay_ms(200); // then erase it + move it for (f=0; f< NUMFLAKES; f++) { x_pos = icons[f][XPOS]; y_pos = icons[f][YPOS]; display_drawBitmapV2(x_pos, y_pos, bitmap, w, h, WHITE); // move it icons[f][YPOS] += icons[f][DELTAY]; // if its gone, reinit if (icons[f][YPOS] > display_height) { icons[f][XPOS] = rand(); icons[f][YPOS] = 0; icons[f][DELTAY] = (rand() % 5) + 1; } } } } void testdrawchar(void) { display_setTextSize(1); display_setTextColor(BLACK); display_setCursor(0, 0); for (uint8_t i=0; i < 168; i++) { if (i == '\n' || i == '\r') continue; display_print(i); } display(); } void testdrawcircle(void) { for (int16_t i=0; i<display_height; i+=2) { display_drawCircle(display_width/2, display_height/2, i, BLACK); display(); } } void testfillrect(void) { uint8_t color = 1; for (int16_t i=0; i<display_height/2; i+=3) { // alternate colors display_fillRect(i, i, display_width-i*2, display_height-i*2, color%2); display(); color++; } } void testdrawtriangle(void) { for (int16_t i=0; i<min(display_width,display_height)/2; i+=5) { display_drawTriangle(display_width/2, display_height/2-i, display_width/2-i, display_height/2+i, display_width/2+i, display_height/2+i, BLACK); display(); } } void testfilltriangle(void) { uint8_t color = BLACK; for (int16_t i=min(display_width,display_height)/2; i>0; i-=5) { display_fillTriangle(display_width/2, display_height/2-i, display_width/2-i, display_height/2+i, display_width/2+i, display_height/2+i, color); if (color == WHITE) color = BLACK; else color = WHITE; display(); } } void testdrawroundrect(void) { for (int16_t i=0; i<display_height/2-2; i+=2) { display_drawRoundRect(i, i, display_width-2*i, display_height-2*i, display_height/4, BLACK); display(); } } void testfillroundrect(void) { uint8_t color = BLACK; for (int16_t i=0; i<display_height/2-2; i+=2) { display_fillRoundRect(i, i, display_width-2*i, display_height-2*i, display_height/4, color); if (color == WHITE) color = BLACK; else color = WHITE; display(); } } void testdrawrect(void) { for (int16_t i=0; i<display_height/2; i+=2) { display_drawRect(i, i, display_width-2*i, display_height-2*i, BLACK); display(); } } void testdrawline() { int16_t i; for (i=0; i<display_width; i+=4) { display_drawLine(0, 0, i, display_height-1, BLACK); display(); } for (i=0; i<display_height; i+=4) { display_drawLine(0, 0, display_width-1, i, BLACK); display(); } delay_ms(250); display_clear(); for (i=0; i<display_width; i+=4) { display_drawLine(0, display_height-1, i, 0, BLACK); display(); } for (i=display_height-1; i>=0; i-=4) { display_drawLine(0, display_height-1, display_width-1, i, BLACK); display(); } delay_ms(250); display_clear(); for (i=display_width-1; i>=0; i-=4) { display_drawLine(display_width-1, display_height-1, i, 0, BLACK); display(); } for (i=display_height-1; i>=0; i-=4) { display_drawLine(display_width-1, display_height-1, 0, i, BLACK); display(); } delay_ms(250); display_clear(); for (i=0; i<display_height; i+=4) { display_drawLine(display_width-1, 0, 0, i, BLACK); display(); } for (i=0; i<display_width; i+=4) { display_drawLine(display_width-1, 0, i, display_height-1, BLACK); display(); } delay_ms(250); } // main function void main(void) { // initialize the LCD LCD_begin(); // init done // you can change the contrast around (0 ... 63) to //adapt the display for the best viewing! display_setContrast(50); display(); // show splashscreen delay_ms(2000); display_clear(); // clears the screen and buffer // draw a single pixel display_drawPixel(10, 10, BLACK); display(); delay_ms(2000); display_clear(); // draw many lines testdrawline(); display(); delay_ms(2000); display_clear(); // draw rectangles testdrawrect(); display(); delay_ms(2000); display_clear(); // draw multiple rectangles testfillrect(); display(); delay_ms(2000); display_clear(); // draw mulitple circles testdrawcircle(); display(); delay_ms(2000); display_clear(); // draw a circle, 10 pixel radius display_fillCircle(display_width/2, display_height/2, 10, BLACK); display(); delay_ms(2000); display_clear(); testdrawroundrect(); delay_ms(2000); display_clear(); testfillroundrect(); delay_ms(2000); display_clear(); testdrawtriangle(); delay_ms(2000); display_clear(); testfilltriangle(); delay_ms(2000); display_clear(); // draw the first ~12 characters in the font testdrawchar(); display(); delay_ms(2000); display_clear(); // text display tests display_setTextSize(1); display_setTextColor(BLACK); display_setCursor(0, 0); display_print("Hello, world!\r\n"); display_setTextColor(WHITE, BLACK); // 'inverted' text printf(display_print, "%f\r\n", 3.141592); display_setTextSize(2); display_setTextColor(BLACK); printf(display_print, "0x%LX\r\n", 0xDEADBEEF); display(); delay_ms(2000); // rotation example display_clear(); display_setRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further. display_setTextSize(1); display_setTextColor(BLACK); display_setCursor(0, 0); display_print("Rotation\r\n"); display_setTextSize(2); display_print("Example!\r\n"); display(); delay_ms(2000); // revert back to no rotation display_setRotation(0); // miniature bitmap display display_clear(); display_drawBitmapV2(30, 16, logo16_glcd_bmp, 16, 16, BLACK); display(); // invert the display display_invert(true); delay_ms(1000); display_invert(false); delay_ms(1000); // draw a bitmap icon and 'animate' movement testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT); } // end of code. |
My breadboard circuit result is shown by the following video:
Proteus simulation:
This example works with Proteus simulation software since it contains the Nokia 5110 (3310) LCD library. The video below shows the simulation result:
Proteus simulation file download link is below, use version 8.6 or higher to open it:
PIC18F46K22 and Nokia 5110 Proteus simulation
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
#use SPI(DO = LCD_DAT, CLK = LCD_CLK, MODE = 0, BITS = 8, STREAM = LCD)
I need some explanation on:
#use SPI(DO = LCD_DAT, CLK = LCD_CLK, MODE = 0, BITS = 8, STREAM = LCD)
What does the DO stands for? IS this a port?
Thanks. Very good. I tend to operate both off 3.3V saves on components.