Table of Contents
Use Stm32cubemx to configure I2C.
LCD MCU
Vdd 3V
Gnd Gnd
SCL PB6
SDA PB9
1. Pin out setting
2. I2C configuration
3. Code generation and edition code
In Src/main.c add code:
#include “stm32f4xx_hal.h”
#define LCD_ADD 0x3E<<1
/* Private function prototypes ———————————————–*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
uint8_t cmd[1];
cmd[0]=0b00111000;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b00111001;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b00010100;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b01111101;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b01010110;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b01101100;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b00001100;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
cmd[0]=0b00000001;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, cmd,1,0×100);
HAL_Delay(10);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
int i=0;
int j=0;
while (1)
{
char buffer[40]=”Hello, Welcome to www.ngolongtech.net”;
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x00, 1, 0b00000010,1,0×100);
for(i=j;i<=strlen(buffer);i++){
cmd[0]=buffer[i];
HAL_I2C_Mem_Write(&hi2c1, LCD_ADD, 0x41, 1, cmd,1,0×100);
HAL_Delay(1);
}
j++;
if(j==(sizeof(buffer)-10)){
j=0;
}
HAL_Delay(500);
}
}
——————————————————————————