In this tutorial we are going to interface LCD 20×4 Display with STM32 using I2C. I am using STM32F103C8 microcontroller and I2C device is PCF8574 with the slave address of 0x4E.
You can use the same code for any other LCD display Type (i.e 16×2, 16X4 etc), except the DDRAM addresses. You can google the addresses for your LCD Type.
Working with these LCD displays is pretty simple. All we need to do is the following :-
- Initialize the display
- Write the address of the DDRAM
- Write the character to be dispalyed
According to the datasheet, To initialize the 20×4 LCD we need to send some sequence of commands to the display. They are as follows:-
First of all we need to wait for more than 40ms
HAL_Delay(50); // wait for >40ms
Send the function set instruction with the command (0x30), 3 times and delay between each command varies as shown below. This entire process is than followed by another function set command (0x20)
Than Send the function set command (0x28) to set the display in 4 bit mode (DL=0), 2 line display (N=1) and 5×8 fonts (F=0)
Write to the Display On/OFF Control and turn the display off (D=0), Cursor off (C=0) and Blink OFF (B=0)
Clear the display by writing command
Set the entry mode of your choice. I am setting Cursor move as increment (I/D=1) and display shift as OFF (S=0)
Turn the display ON (D=1) along withe choice for cursor and blinking. I am setting both to 0 (C=0, B=0)
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
Addresses for the DDRAM of the LCD 20×4 is shown below.
While setting an address we need to OR (|) it with the 0x80. For eg if I want to set the cursor to 3rd row, I have to set the address as (0x80|0x40).
If you want to use a 16×4 LCD than the addresses for the DDRAM are shown below. Use the same code with just the change in address.
We will cover CGRAM and custom characters in the upcoming tutorials.
- LPC2148 UART Send and Receive April 5, 2020
- FreeRTOS Tutorial #5 ->Using Queue April 2, 2020
- Low Power Modes in STM32 March 30, 2020
- FreeRTOS TUTORIAL #4 -> Using Counting Semaphore February 18, 2020
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020