cancel
Showing results for 
Search instead for 
Did you mean: 

Why can't get adress of 2x16 LCD in Cube IDE?

SMusc.1
Associate III

Hi every one. I am using STM32-F401RE, 2x16 LCD with I2C integrate, i connect SCL-SDA-Vcc and GND each other (board and LCD), i tried to make scann program to know adress of LCD in this way:

#include "main.h"

#include "stdio.h"

I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart1;

uint8_t Buffer[25] = {0};

uint8_t Space[] = " - ";

uint8_t StartMSG[] = "Starting I2C Scanning: \r\n";

uint8_t EndMSG[] = "Done! \r\n\r\n";

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_USART1_UART_Init(void);

int main(void)

{

  uint8_t i = 0, ret;

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_I2C1_Init();

  MX_USART1_UART_Init();

  HAL_Delay(1000);

  /*-[ I2C Bus Scanning ]-*/

  HAL_UART_Transmit(&huart1, StartMSG, sizeof(StartMSG), 10000);

  for(i=1; i<128; i++)

  {

    ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 3, 5);

    if (ret != HAL_OK) /* No ACK Received At That Address */

    {

      HAL_UART_Transmit(&huart1, Space, sizeof(Space), 10000);

    }

    else if(ret == HAL_OK)

    {

      sprintf(Buffer, "0x%X", i);

      HAL_UART_Transmit(&huart1, Buffer, sizeof(Buffer), 10000);

    }

  }

  HAL_UART_Transmit(&huart1, EndMSG, sizeof(EndMSG), 10000);

  /*--[ Scanning Done ]--*/

  while (1)

  {

  }

}

but no adress i saw. If i use LCD with Arduino Mega board i am able to see the 0x27 adress of LCD. I used also all the pins avaiable on F401RE board for SCL and SDA comunication but never i see the adress of LCD.

Have some one idea how i can get it(adress of LCD)?

10 REPLIES 10
SMusc.1
Associate III

good evening, i am not able to see any letter with my system: F401RE Nucleo board and 2x16 LCD with I2C integrate but, i am able to see some things ( some letter and example) if connect LCD with arduino. Can some one show me small program to be able also to see one word in this system built: F401RE and LCD? Maybe i have to change brand and buy some other board from other brand/vendor.

>>2x16 LCD 

That's a bit generic, what specific make/model are we talking about here? Got a link to the part you purchased?

Arduino Mega, that's a 5V board, sure you have the thing powered correctly? Pull-ups?

It's ALL the other code where the magic really happens, configures the pins, peripherals, clocks, etc.

No detail of anything in the code provided, Cube hides it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SMusc.1
Associate III

Hi, I bought the F401RE Nucleo board on Amazon, I used it for several project and no issue to here. When i tried to connect thos boaard to 2x16 LCD with I2C drive integrated to LCD, i wasn't able to send any letter from board to LCD. The lcd work because i used it on arduino and i see on lcd some letter like : hello word. I create a progect in CUBE IDE, left the standard configuration, i wrote this code in main:

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C1_Init();

 /* USER CODE BEGIN 2 */

 lcd_init ();

 lcd_send_string ("HELLO WORLD");

 HAL_Delay(1000);

 lcd_put_cur(1, 0);

 lcd_send_string("from CTECH");

 HAL_Delay(2000);

 lcd_clear ();

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

I add two library, this:

First libray:

/** Put this in the src folder **/

#include "i2c-lcd.h"

extern I2C_HandleTypeDef hi2c1; // change your handler here accordingly

#define SLAVE_ADDRESS_LCD 0x4E // change this according to ur setup

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_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=0

data_t[1] = data_u|0x09; //en=0, rs=0

data_t[2] = data_l|0x0D; //en=1, rs=0

data_t[3] = data_l|0x09; //en=0, rs=0

HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);

}

void lcd_clear (void)

{

lcd_send_cmd (0x80);

for (int i=0; i<70; i++)

{

lcd_send_data (' ');

}

}

void lcd_put_cur(int row, int col)

{

  switch (row)

  {

    case 0:

      col |= 0x80;

      break;

    case 1:

      col |= 0xC0;

      break;

  }

  lcd_send_cmd (col);

}

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); //Display 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++);

}

Second library:

#include "stm32f4xx_hal.h"

void lcd_init (void);  // initialize lcd

void lcd_send_cmd (char cmd); // send command to the lcd

void lcd_send_data (char data); // send data to the lcd

void lcd_send_string (char *str); // send string to the lcd

void lcd_put_cur(int row, int col); // put cursor at the entered position row (0 or 1), col (0-15);

void lcd_clear (void);

but i see no letter on LCD.

I supply the LCD with 5 V of the board. I didn't found any tutorial on web using this F401RE Nucleo with LCD but only other kind of STM32 board. Maybe this board have some issue in this kind of application?

I tried to use all possible SDA/SCA pins canghing the IC2, IC3 but nothing working. Attached some pictures of my project. I used arduino mega and there working.

If some one can help me i can send the entire project.

SMusc.1
Associate III
 

The question was with regard to the make/model of "16x2" display.

Guessing it's using an I2C IO Expander to drive as a 4-bit controller.

Any of the I2C commands error?

Show any of the pin initialization code?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Do you check the return code from I2C functions? Are you sure that the LCD responds?

Here you can find helping hands to get started.

Likely the one here

https://controllerstech.com/i2c-lcd-in-stm32/

Illustrate your wiring. Prior post only has a single picture, not pictures

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi, no error , nothing. Here i post the LCD model. I saw 10 times the video you send me: https://controllerstech.com/i2c-lcd-in-stm32/ because i used the information on that video to create and to add library and code.

I attach three picture (model of LCD that is the same used on the video) and configuration end wiring.

Here all the programm where can see the pin inizialization:

#include "main.h"

#include "i2c-lcd.h"

I2C_HandleTypeDef hi2c1;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

int row=0;

int col=0;

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_I2C1_Init();

 lcd_init ();

 lcd_send_string ("HELLO WORLD");

 HAL_Delay(1000);

 lcd_put_cur(1, 0);

 lcd_send_string("from CTECH");

 HAL_Delay(2000);

 lcd_clear ();

 while (1)

 {

 }

}

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 __HAL_RCC_PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLM = 16;

 RCC_OscInitStruct.PLL.PLLN = 336;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

 RCC_OscInitStruct.PLL.PLLQ = 7;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

 {

  Error_Handler();

 }

}

static void MX_I2C1_Init(void)

{

 hi2c1.Instance = I2C1;

 hi2c1.Init.ClockSpeed = 100000;

 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

 hi2c1.Init.OwnAddress1 = 0;

 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 hi2c1.Init.OwnAddress2 = 0;

 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 if (HAL_I2C_Init(&hi2c1) != HAL_OK)

 {

  Error_Handler();

 }

}

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

 GPIO_InitStruct.Pin = B1_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = LD2_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

}

void Error_Handler(void)

{

 __disable_irq();

 while (1)

 {

 }

}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)

{

}

#endif /* USE_FULL_ASSERT */

SMusc.1
Associate III

no video related the retur code you talk