Interface LCD 16×2 with STM32 without I2C
As the topic states, today in this tutorial we are going to interface LCD 16×2 with STM32, and this time we are not going to use the I2C to do so. If you are looking for LCD 16×2 with I2C, goto controllerstech.com/i2c-lcd-in-stm32/
Basically we are going to use the parallel connection between STM32, and the LCD itself. LCD 16×2 can be connected in the parallel mode either using 4 data pins (LCD 4 bit MODE) or using all 8 data pins (8 bit MODE). In this tutorial, we are going to use the 4 bit MODE to connect LCD with our microcontroller.
As you can see in the picture above, the higher 4 pins of the LCD ( DB4-DB7) are connected to the microcontroller pins (PA4-PA7).
- RS is connected to PA1
- RW is connected to PA2
- EN is connected to PA3
You can connect the pins anywhere you want. It doesn’t have to be in order or in a single port. We will define these pins later in our code.
Here we need to do 2 things. First we have set all the pins, that we want to use, as output. It is shown in the picture below.
Second, we have to setup timer to create delay in microseconds. If you are using HCLK less than 100 MHz, you do not need this. I am setting it up just in case. If you don’t know how to use timer to create delay in microseconds, visit https://controllerstech.com/create-1-microsecond-delay-stm32/
Now the Cubemx setup is done, let’s take a look at the functions that we are going to use
First of all we need to define the Pins and Ports that we are going to use for the LCD. As i mentioned in the beginning, you can use any port and pins for the connection. Just make sure you define it in this LCD1602.c file, as shown below
Next is the Timer definition, to give us the delay in microseconds. You might not need to do this for Lower HCLK. Just replace htim1 with the timer handler that you are using.
I have created a function to write the data/command to the respective pins of the LCD. This function takes 2 parameters, first one is the data that we are going to write, and second one is the RS, which can be set as 0 in case of command, and 1 if we want to write data.
- All we have to do is, take the useful data, which is 4 bit long, and write the first bit to the DB4, second bit to DB5, third to DB6, and fourth to DB7.
- Then we need to perform the enable pin strobe, to update this data to the respective pins. Which means that first pull the pin HIGH, and than pull it LOW.
- If you are using a controller with HCLK more than 100 MHz, than you need to un-comment that delay line, as shown in the code above.
- This is because We have to wait for the enable pin to settle. If the clock is very high, increase the delay.
The following is the function to send the command to the LCD
- LCD is connected in the 4 bit mode, where only 4 data pins of the LCD are being used.
- The command that we send should also be 4 bit wide. Therefore we have to first send the higher nibble, and than lower nibble. RS pin must be 0, for the command operation.
Below is the function to send data to the LCD
It is similar to the command function, except RS pin is HIGH to indicate the data operation.
The above function puts the cursor at the desired row and column. The row can be either 0 or 1, and the column can vary from 0 to 15.
Lcd_init function initializes the LCD in the 4 bit mode
The above code is with reference to the pattern given for the initialization in the datasheet of the device.
The following is the code inside the main function. First I have initialized the LCD, and than printing the strings.