This is the first “official” project I am doing with my PIC prototyping board. This project is about interfacing and using a simple Graphic LCD. So to follow this project you need a PIC to have SPI hardware, in order to communicate with the device. The GLCD (Nokia 5110) I am using is well known in the community and it is a cheap device. I bought it off ebay for about 5 Euros.
This screen has 8 pins:
- 2 pins for the SPI communication
- 2 pins for power and ground
- 1 pin to power the backlight LEDs
- 1 pin for Chip Enable (active low)
- 1 pin for Reset (active low)
- 1 pin to select command or data operation
5V PIC but 3.3V device!
In general, the GLCD works in the 3.3Volt environment. If you are operating your PIC on the 3.3V level you then connect the pins straight away. However, if you are like me with a 5V PIC, then you will need some extra components. First of all you need to make sure that you are supplying 3.3V of power. For this project I used a 3.3V regulator I had already on my bread board. Another trick I often use, are 2-3 diodes connected in series. Since diodes have forward voltage drop, you can use that to drop the 5V supply down and match 3.3V as close as possible without violating the limits of the target device (or you will burn it!). I don’t recommend you use this method for your final board, but for a quick prototyping testing it should be ok.
Another problem is the logic levels of the communication. You have 5 pins in total which are connected from the PIC to the GLCD. I followed the advice I read here and just connected a 10kOhms resistor between each communication line. And it worked just fine!
Operating the 5110 GLCD
As we already said, we will use the SPI portocol to send data and commands to the device. So the first thing you need to do is to setup the SPI settings in your program. Since I am using the Microchip’s C18 free compiler, I used the following command:
OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
On page 15, the datasheet specifies the procedure we need to follow in order to turn on the GLCD. Before we see how to do that, let me just first write the function I use to send the data, LCD5110_send function:
void LCD5110_send(unsigned char data, unsigned char dc) { DC_5110 = dc; // Set the appropriate status for command=0 or data=1 putcSPI(data); }
Using this command, you can send a byte (unsigned char) to the device but you need define whether this byte is a data byte or command. The DC_5110 you see there is the pin I defined earlier on which represents the command/data line. I also defined the rest of the pins and you can see this in the full source code of the program. So now that we are able to send stuff to the screen, lets initialize it!
Initializing 5110 GLCD
This is the code I am using. I got this from here, and convert it to my own needs:
LCD5110_send(0x20 + 0x01, 0); // Extended instructions enabled LCD5110_send(0x80 + 0x40, 0); // Set contrast 0 - 127 LCD5110_send(0x04 + 0x02, 0); // Temperature control LCD5110_send(0x10 + 0x03, 0); // Set bias system LCD5110_send(0x20 + 0x00, 0); // Return to basic instruction set, power on, set horizontal addressing LCD5110_send(0x08 + 0x04, 0); // Display control set to normal mode
These commands set your screen ready to send some text on! There are some other stuff you need to do before sending the above, so make sure you take a look at the full source code (at the end of this post).
Sending characters to the screen
Remember this is a GLCD not a text LCD, so we define the state of each individual pixel. Each character is made out of 40 pixels (5 columns x 8 rows). In order to display some text we need to use a font array which contains the correct sequence of pixels to display symbols, letters and numbers. I found a great font collection on the net which works great. This array is usually stored in the ROM area. The function I am using for sending a character to the screen is the following:
void LCD5110_sendchar(unsigned char character) { unsigned char column = 0; character = character - 0x20; // 0x20 is the first element of the array for (column = 0; column < 5; column++) // Pass through each column { LCD5110_send(font[(int) character * 5 + column], 1); } LCD5110_send(0x00, 1); // Send a small space }
Notice that we are sending data since the second parameter of the LCD5110_send function is 1.
Selecting where to display your character
So now you know how to display something on the screen, but where will it appear? In order to define the position of the next character you have to send the following commands to the GLCD
LCD5110_send(0x40 + 0, 0); // set Y address LCD5110_send(0x80 + 0, 0); // set X address
Address for the X ranges from 0 to 84 while for Y ranges from 0 to 6. So its like you have six horizontal lines where you can start writing at any point in that line. Since we are using horizontal addressing, sequential characters are displayed next to each other horizontally. Maybe you can understand this better if you read the datasheet on page 9. So to determine the Y address you have to send the 0×40 (notice the 0 parameter on the LCD5110_send function) command, plus the vertical location you want. For X, you send 0×80 command plus the horizontal location you want. For example to write Hello World! on the second line of the display starting from the beginning of the line we should write the following before sending the characters
LCD5110_send(0x40 + 1, 0); // set Y address LCD5110_send(0x80 + 0, 0); // set X address
Sending strings to the display
I am using the following function to send a string to the display
void LCD5110_sendstring(rom unsigned char *str) { while (*str) { LCD5110_sendchar(*str); str++; } }
This function iterates through each character of the string and then sends it using the LCD5110_sendchar function. Its simple.
Resources
The main.c file I used with C18 compiler. This is the full source code
References: Interfacing Nokia 3510i and 5110 LCD with PIC Microcontroller
Related Posts:
Incoming search terms:
- interface pic18f con lcd (13)
- spi hardware interface (7)
- pcbgcode (6)
- spi pic (4)
- 5110 pic (4)
- pic32 spi (3)
- spi interface (3)
- pic SPI lcd code (3)
- nokia 5110 interface (3)
- pic spi (3)

Great site! I was wondering how I could modify the code to print variables that I have initialized ? Please let me know as soon as you can
Thanks !
Hi Sarah,
did you try using the
LCD5110_sendstringfunction? You can pass your string there and it will print it. Try it and let me know.So are you saying for …
LCD5110_sendstring(“Hello World”);
I would write:
float P=5;
LCD5110_sendstring(P);
and then the LCD should show 5 ?
I don’t have my LCD with me right now but I will in a bit. Let me know if this is the right approach.
Seeing as how P is a float instead of an int, would I need to modify this part of your code:
void LCD5110_sendchar(unsigned char character)
{
unsigned char column = 0;
character = character – 0×20; // 0×20 is the first element of the array
for (column = 0; column < 5; column++) // Pass through each column
{
LCD5110_send(font[(int) character * 5 + column], 1);
}
LCD5110_send(0×00, 1); // Send a small space
}
Thanks for your help!
Hello Sarah,
I see what you are saying. The function wont work like that because its meant to output strings not numbers. You will have to format your data as a string before using that function, something like
printf.Hope that helps!
So I wrote
int P;
P = 5;
printf(“%d\n”,P);
LCD5110_send(0×40 + 0, 0);
LCD5110_send(0×80, 0);
LCD5110_sendstring(P);
The build succeeded but LCD outputs nothing. Any tips ? Thanks!
Sarah
printf sends its output to standard output — console etc in the unix world.
You need a version of printf that does the same formatting but places the result into a string variable rather than sending the result to standard output.
I believe the standard library function sprintf does what you need.