This PIC MCU project shows how to interface Microchip PIC18F46K22 microcontroller with Nokia 5110 (Nokia 3310) graphical LCD screen.
The name Nokia 5110 (3310) comes from Nokia 5110 (or Nokia 3310) mobile phone. The Nokia 5110 LCD has a controller named PCD8544. This LCD 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 compiler used in this project is mikroElektronika mikroC PRO for PIC.
SPI: Serial Peripheral Interface.
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 converts 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.6.0.
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 mikroC compiler
The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for mikroC compiler
after the download of the 2 library files, add both of them to the project folder.
The default connection setting of the Nokia 5110 LCD (PCD8544) library is hardware SPI1 module (SPI1_Init(); must be called before initiating the LCD). Instead of hardware SPI1 module, software SPI or hardware SPI2 module can be used.
If LCD data pin (LCD_DAT) and clock pin (LCD_CLK) are defined then the library will automatically use software SPI.
example (respectively, LCD_DAT and LCD_CLK are connected to RD3 and RD4):
#define LCD_DAT RD3_bit
#define LCD_CLK RD4_bit
If the line below is defined then the library will use hardware SPI2 module:
#define LCD_HARD_SPI2
and SPI2_Init(); function must be called before initiating the LCD.
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 with the microcontroller are defined as (software SPI is used):
1 2 3 4 5 6 7 8 9 10 11 12 | // define Nokia LCD module pin connections // RST is optional! #define LCD_RST RD0_bit #define LCD_CS RD1_bit #define LCD_DC RD2_bit #define LCD_DAT RD3_bit #define LCD_CLK RD4_bit #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 |
And the LCD is initialized as:
1 2 | // initialize the LCD LCD_begin(); |
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 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | /**************************************************************************** Interfacing PIC18F46K22 microcontroller with Nokia 5110 (3310) LCD display. Graphics test example. 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 *****************************************************************************/ // define Nokia LCD module pin connections // RST is optional! #define LCD_RST RD0_bit #define LCD_CS RD1_bit #define LCD_DC RD2_bit #define LCD_DAT RD3_bit #define LCD_CLK RD4_bit #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 #include "PCD8544.c" // include PCD8544 controller driver source code (Nokia 5110 LCD driver) #include "GFX_Library.c" // include graphics library source code #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #define LOGO16_GLCD_HEIGHT 16 #define LOGO16_GLCD_WIDTH 16 const 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(const 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 (1) { 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) { char i; display_setTextSize(1); display_setTextColor(BLACK, WHITE); display_setCursor(0, 0); for (i=0; i < 168; i++) { if (i == '\n' || i == '\r') continue; display_putc(i); } display(); } void testdrawcircle(void) { char i; for (i=0; i<display_height; i+=2) { display_drawCircle(display_width/2, display_height/2, i, BLACK); display(); } } void testfillrect(void) { char i, color = 1; for (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) { char i; for (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) { short i, color = BLACK; for (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) { char i; for (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) { char i, color = BLACK; for (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) { char i; for (i=0; i<display_height/2; i+=2) { display_drawRect(i, i, display_width-2*i, display_height-2*i, BLACK); display(); } } void testdrawline() { short 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() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELD = 0; // configure all PORTD pins as digital // 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, WHITE); display_setCursor(0, 0); display_puts("Hello, world!\r\n"); display_setTextColor(WHITE, BLACK); // 'inverted' text display_printf("%4.2f\r\n", 3.141592); display_setTextSize(2); display_setTextColor(BLACK, WHITE); display_printf("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, WHITE); display_setCursor(0, 0); display_puts("Rotation\r\n"); display_setTextSize(2); display_puts("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.
Hello! bellow are the error messages that my compiller is returning for interface of PIC16F877A and NOKIA LCD330/5110.
299 324 Undeclared identifier ‘SSD1306_RST_DIR’ in expression nokia5110.c
308 324 Undeclared identifier ‘SSD1306_CS_DIR’ in expression nokia5110.c
410 324 Undeclared identifier ‘abs’ in expression nokia5110.c
410 324 Undeclared identifier ‘abs’ in expression nokia5110.c
410 317 Operator ” is not applicable to these operands ” nokia5110.c
420 324 Undeclared identifier ‘abs’ in expression nokia5110.c
I am still developing my software. below is the beginning of it:
// Nokia 5110 LCD module connections
// use software SPI
#define LCD_RST RB0_bit // reset pin, optional!
#define LCD_CS RB1_bit // chip select pin, optional!
#define LCD_DC RB2_bit // data/command pin
#define LCD_DAT RB3_bit // data-in pin (MOSI)
#define LCD_CLK RB4_bit // clock pin
// end LCD module connections
#define LCD_RST_DIR TRISB0_bit
#define LCD_CS_DIR TRISB1_bit
#define LCD_DC_DIR TRISB2_bit
#define LCD_DAT_DIR TRISB3_bit
#define LCD_CLK_DIR TRISB4_bit
#define LCD_HARD_SPI2
#include “NOKIA5110.c” // include PCD8544 controller driver source code (Nokia 5110 LCD driver)