AnsweredAssumed Answered

STM32F4xx and character LCD not talking using I2C - HAL library

Question asked by Ch.Pradeep on Jan 31, 2017

I am trying to make communication between STM32F411RCT6 and character LCD  NHD-C0220BIZ-FS(RGB)-FBW-3VM using I2C protocol.

STM32 is the master and the LCD is slave device. I am using stm32cubemx and HAL libraries for this.

 

Following is my code so far:

 

#define LCD_ADDRESS  0x78

uint8_t commands[10] = {0x00,0x38, 0x39, 0x14,0x78,0x5e,0x6d,0x0c,0x01,0x06 };
uint8_t data[4] = { 0x40, 0x61, 0x62,0x63 }; // characters a,b,c to be displayed starting from RAM address 0x40.

HAL_StatusTypeDef halstatus = 0;

 

void Init_LCD()

{

halstatus = HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t) (LCD_ADDRESS),2,100);
printf("halstatus1 = %d\r\n",halstatus);

halstatus = HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)LCD_ADDRESS, commands, 10, 100);
printf("halstatus2 = %d\r\n",halstatus);

}

void Show_LCD()

{

halstatus = HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)LCD_ADDRESS, data, 4, 100);
printf("halstatus3 = %d\r\n",halstatus);

}

 

int main(void)
{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();

 

Init_LCD();
Show_LCD();

while (1)
{ }
}

I am getting output as:

halstatus1 = 0;

halstatus2 = 1;

halstatus3 = 1;

I was expecting characters a,b,c to be displayed on the LCD screen, which is not happening.

 

The HAL_I2C_Master_Transmit() function fails with HAL_ERROR, it is sending 10 bytes first time and 4 bytes another time.

When I try to send just 2 command bytes using HAL_I2C_Master_Transmit() instead of 10 bytes, halstatus2 is zero or HAL_OK, but it does not configure my LCD display.

 

Following is the C code of character LCD given in the data sheet:

/* Initializes the LCD panel. */
void init_LCD(void) {
I2C_Start();

I2C_out( 0x78 ); // Slave address of the LCD panel.
I2C_out( 0x00 ); // Control byte: all following bytes are commands.
I2C_out( 0x38 ); // 8-bit bus, 2-line display, normal instruction mode.
delay( 10 );

I2C_out( 0x39 ); // 8-bit bus, 2-line display, extension instruction mode.
delay( 10 );

I2C_out( 0x14 ); // Bias set to 1/5.
I2C_out( 0x78 ); // Contrast set.
I2C_out( 0x5E ); // Icon display on, booster on, contrast set.
I2C_out( 0x6D ); // Follower circuit on, amplifier=1?
I2C_out( 0x0C ); // Display on, cursor off.
I2C_out( 0x01 ); // Clear display.
I2C_out( 0x06 ); // Entry mode set to cursor-moves-right.
delay( 10 );

I2C_Stop();
}

 

/* Writes a 10-char string to the RAM of the LCD. */
void show( unsigned char *text ) {
int n;

I2C_Start();

I2C_out( 0x78 ); // Slave address of panel.
I2C_out( 0x40 ); // Control byte: data bytes follow, data is RAM data.

for( n = 0; n < 10; n++ ) {
I2C_out( *text );
text++;
}

I2C_Stop();
}

 

void main(void) {
int i;

init_LCD();

show("HelloWorld");

while(1)
{}
}

Please update me why HAL_I2C_Master_Transmit() is failing with HAL_ERROR two times.

Am I doing wrong somewhere .. ?

 

Thanks,

Outcomes