STM32 Black Pill | Using HAL Programming to Use I2C LCD

by Anjandeep Singh in Circuits > Microcontrollers

175 Views, 1 Favorites, 0 Comments

STM32 Black Pill | Using HAL Programming to Use I2C LCD

image.png

In this tutorial, we'll use an STM32 Black Pill to program an I²C LCD on STM32CubeIDE using HAL programming.

Supplies

Hardware:

  • STM32 Black Pill
  • 16*2 I²C LCD
  • Laptop or desktop
  • USB-C cable
  • Few jumper wires

Software:

  • STM32CubeIDE
  • STM32CubeProgrammer

Project Creation

  • Create a new STM32 project from File > New > STM32 Project
  • Select the appropriate board.

Pinout Selection

stm32cubeide_2oqlDMo3qS.png
stm32cubeide_sNOmQuqYZt.png
  • Go to Connectivity > I2C1 and enable it. This will set SDA to pin 43 and SCL to pin 42.\
  • Go to System Core > RCC > HSE > Crystal/Ceramic Resonator.

Clock Configuration

stm32cubeide_w3wwYCs2MP.png
  • Configure your board's clock.
  • Save using Ctrl + S.

Writing the Program

stm32cubeide_Bj97Rztq9V.png
  • Go to the project file in project explorer, then Core > Src > main.c.
  • Inside the function int main(void) write the following program:
  HD44780_Init(2);
HD44780_Clear();
HD44780_SetCursor(0,0);
HD44780_PrintStr("Hi from Anjan");
HD44780_SetCursor(0,1);
HD44780_PrintStr("Singh");
HAL_Delay(2000);

Note: The code may vary according to your micrcontroller.

  • Save the file and debug.

STM32CubeProgrammer

javaw_MODih5pSe7.png
javaw_xBd5Pxp5LM.png
javaw_wg277ARknJ.png
javaw_A1Hot49OX3.png
  • We will bootload STM using USB, this tutorial does not use ST-Link.
  • Next, press on "Open file" and select the .elf file from the project folder.
  • Press on "Download".


  • Go to "Erasing and Programming" from the menu in the left.
  • Go to file path and select the .elf file and press "Start automatic mode".
  • Once compiled, disconnect and reconnect the STM board.

Result

IMG_4577.jpg

You should see the preferred result.


Thank you.