Hardware Requirements
:
- STM32 black pill board
- 16x2 LCD
- Jumper wires
- Breadboard
Software Requirements:
- STM32CubeIDE
- STM32CubeMX
Step 1: Set up STM32CubeIDE
Install STM32CubeIDE as Step 1
STM32CubeIDE can be downloaded and installed on a PC.
Launch STM32CubeIDE after installation, then start a new project.
From the "Quickstart" menu, choose "New STM32 Project".
Select your preferred toolchain and "STM32F103C8Tx" as the board type.
Name your project, then choose the directory in which to save it.
Step 2: Configure the STM32 using STM32CubeMX
Configure the STM32 using STM32CubeMX in step two.
Select the same board when starting STM32CubeMX as you did when building the project in the previous step.
Set up the pins as follows in the "Pinout & Configuration" tab:
Using PA0 as the button's input
PB6 as output (for the LCD's RS pin)
PB7 as output (for the LCD's EN pin)
PB8 as output (for the LCD's D4 pin)
PB9 as output (for the LCD's D5 pin)
PB10 as output (for LCD D6 pin), PB11 as output (for LCD D7 pin).
Set the clock source and frequency as needed in the "Clock Configuration" page.
To exit STM32CubeMX, click "Generate Code" and then click "X."
Step 3: Write the code
Write The Following Code In main.c File -
#define SLAVE_ADDRESS_LCD 0x4E
#include "stdlib.h"
write this code after I2C_HandleTypeDef hi2c1;
char str[40];
void lcd_send_data (char data)
{
char data_u, data_l;
uint8_t data_t[4];
data_u = (data&0xf0);
data_l = ((data<<4)&0xf0);
data_t[0] = data_u|0x0D; //en=1, rs=1
data_t[1] = data_u|0x09; //en=0, rs=1
data_t[2] = data_l|0x0D; //en=1, rs=1
data_t[3] = data_l|0x09; //en=0, rs=1
HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}
void lcd_send_cmd (char cmd)
{
char data_u, data_l;
uint8_t data_t[4];
data_u = (cmd&0xf0);
data_l = ((cmd<<4)&0xf0);
data_t[0] = data_u|0x0C; //en=1, rs=0
data_t[1] = data_u|0x08; //en=0, rs=0
data_t[2] = data_l|0x0C; //en=1, rs=0
data_t[3] = data_l|0x08; //en=0, rs=0
HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}
void lcd_init (void)
{
// 4 bit initialisation
HAL_Delay(50); // wait for >40ms
lcd_send_cmd (0x30);
HAL_Delay(5); // wait for >4.1ms
lcd_send_cmd (0x30);
HAL_Delay(1); // wait for >100us
lcd_send_cmd (0x30);
HAL_Delay(10);
lcd_send_cmd (0x20); // 4bit mode
HAL_Delay(10);
// dislay initialisation
lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
HAL_Delay(1);
lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0 ---> display off
HAL_Delay(1);
lcd_send_cmd (0x01); // clear display
HAL_Delay(1);
HAL_Delay(1);
lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
HAL_Delay(1);
lcd_send_cmd (0x0C); //Di splay on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
}
void lcd_send_string (char *str)
{
while (*str) lcd_send_data (*str++);
}
void setCursor(int a, int b)
{
switch (b)
{
case 0:
b |= 0x80;
break;
case 1:
b |= 0xC0;
break;
}
lcd_send_cmd (b);
lcd_send_cmd (a);
}
Write this code in while (1) Loop :
lcd_init();
setCursor(0,0);
lcd_send_string("CHITKARA");
HAL_Delay(1000);
setCursor(0,1);
lcd_send_string("UNIVERSITY");
HAL_Delay(1000);
The STM cube IDE automatically generates the SystemClock_Config() and MX_GPIO_Init() methods, which are used to configure the clock and GPIO pins, respectively. The LCD is initialised by the lcd_init() method, and the display is cleared by the lcd_clear() function. Finally, the "Chitkara University" text is displayed on the LCD by the lcd_puts() method.
Step 6: Uploading the code
Uploading the code is step six. It's time to upload the code to the board now that it has been written. Utilise a USB cable to connect the STM32 black pill board to your PC. To build and upload the code to the board, select "Run" in the STM cube IDE.
Testing the LCD in Step 7
The LCD display ought to show the text "Chitkara University" once the code has been uploaded. If it doesn't, look for issues in your connections and code.
Congratulations! Your display of "Chitkara University" was successful.
Comments