// STM32 Nucleo-L031K6 HAL Blink + printf() example
// Simulation: https://wokwi.com/projects/367244067477216257

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32l0xx_hal.h>









I2C_HandleTypeDef hi2c1;

static uint8_t data[5]; // Increased the size to accommodate the control byte

void send_command(uint8_t command) {
  printf("helloa\n");
  data[0] = 0x00;      // Control byte
  printf("hellob\n");
  data[1] = command;   // Command byte
  printf("helloc\n");
  if (HAL_I2C_Master_Transmit(&hi2c1, 0x27 << 1, data, 2, HAL_MAX_DELAY) != HAL_OK) {
    printf("I2C Transmit Error\n");
}
  HAL_I2C_Master_Transmit(&hi2c1, 0x27 << 1, data, 2, HAL_MAX_DELAY);
  printf("hellod\n");
}

void send_data(uint8_t data) {
  uint8_t buffer[2];   // Temporary buffer for control and data bytes
  buffer[0] = 0x40;    // Control byte
  buffer[1] = data;    // Data byte
  HAL_I2C_Master_Transmit(&hi2c1, 0x27 << 1, buffer, 2, HAL_MAX_DELAY);
}

void HAL_MspInit(void) {
  GPIO_InitTypeDef gpio_init;

  // Enable I2C1 clock
  __HAL_RCC_I2C1_CLK_ENABLE();

  // Configure SCL and SDA pins
  gpio_init.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  gpio_init.Mode = GPIO_MODE_AF_OD;
  gpio_init.Pull = GPIO_PULLUP;
  gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
  gpio_init.Alternate = GPIO_AF4_I2C1;
  HAL_GPIO_Init(GPIOB, &gpio_init);
}

void HAL_MspDeInit(void) {
  // ... other de-initialization tasks
}

int main(void) {
  printf("hello\n");

  HAL_Init();
  HAL_MspInit();

  // Initialize I2C1
  hi2c1.Instance = I2C1;
  HAL_I2C_Init(&hi2c1);
  printf("hello1\n");
  // Initialize LCD
  send_command(0x38); // Set 8-bit mode, 2 lines
  printf("hello2\n");
  send_command(0x0F); // Turn on display
  printf("hello3\n");
  send_command(0x01); // Clear display
  printf("hello4\n");
  send_command(0x06); // Set cursor move direction
printf("hello5\n");
  // Print "Hello" on the first line
  send_command(0x80); // Set cursor position to 0,0
  
  send_data('H');
  send_data('e');
  send_data('l');
  send_data('l');
  send_data('o');
printf("hello6\n");
  while (1) {
    // Add your application logic here
  }
}




Loading
st-nucleo-l031k6