Stm32f4 – I2C LCD module -Use Stm32cubemx to configure I2C

Stm32f4 – I2C LCD module . Use Stm32cubemx to configure I2C. STM32CubeMX is a graphical tool that allows a very easy configuration of STM32 microcontrollers and microprocessors, as well as the generation of the corresponding initialization C code for the Arm® Cortex®-M core or a partial Linux® Device Tree for Arm® Cortex®-A core),
 
 

Use Stm32cubemx to configure I2C.

Connect
LCD               MCU
Vdd                3V
Gnd                Gnd
SCL                 PB6
SDA                 PB9

1. Pin out setting

RCC-> High Speed clock -> Crystal ceramic resonator
Up system clock to 216 Mhz
I2C1 -> I2C ->I2C

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);
  }

}
 ——————————————————————————

Sponsored Links: