cancel
Showing results for 
Search instead for 
Did you mean: 

I2C commands to LCD

SScot.3
Associate II

I am learning this new 32-bit from my 8-bit world as well as new IDE on top of re-learning C. So be gentle....

I am trying to use the CubeIDE to talk to an I2C LCD. I am following a tutorial but the LCD does not respond at all.

Some of the code below:

/* LCD Commands */

#define LCD_CLEARDISPLAY  0x01

#define LCD_RETURNHOME   0x02

#define LCD_ENTRYMODESET  0x04

#define LCD_DISPLAYCONTROL 0x08

#define LCD_CURSORSHIFT   0x10

#define LCD_FUNCTIONSET   0x20

#define LCD_SETCGRAMADDR  0x40

#define LCD_SETDDRAMADDR  0x80

/* Commands bitfields */

//1) Entry mode Bitfields

#define LCD_ENTRY_SH   0x01

#define LCD_ENTRY_ID   0x02

//2) Display control

#define LCD_DISPLAY_B   0x01

#define LCD_DISPLAY_C   0x02

#define LCD_DISPLAY_D   0x04

//3) Shift control

#define LCD_SHIFT_RL   0x04

#define LCD_SHIFT_SC   0x08

//4) Function set control

#define LCD_FUNCTION_F  0x04

#define LCD_FUNCTION_N  0x08

#define LCD_FUNCTION_DL  0x10

/* I2C Control bits */

#define LCD_RS    (1 << 0)

#define LCD_RW    (1 << 1)

#define LCD_EN    (1 << 2)

#define LCD_BK_LIGHT (1 << 3)

/* Library variables */

static I2C_HandleTypeDef* lcd16x2_i2cHandle;

static uint8_t LCD_I2C_SLAVE_ADDRESS=0;

#define LCD_I2C_SLAVE_ADDRESS_0 0x4E

#define LCD_I2C_SLAVE_ADDRESS_1 0x7E

/* Private functions */

static void lcd16x2_i2c_sendCommand(uint8_t command)

{

 const uint8_t command_0_3 = (0xF0 & (command<<4));

 const uint8_t command_4_7 = (0xF0 & command);

 uint8_t i2cData[4] =

 {

   command_4_7 | LCD_EN | LCD_BK_LIGHT,

   command_4_7 | LCD_BK_LIGHT,

   command_0_3 | LCD_EN | LCD_BK_LIGHT,

   command_0_3 | LCD_BK_LIGHT,

 };

 HAL_I2C_Master_Transmit(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS, i2cData, 4, 200);

}

static void lcd16x2_i2c_sendData(uint8_t data)

{

 const uint8_t data_0_3 = (0xF0 & (data<<4));

 const uint8_t data_4_7 = (0xF0 & data);

 uint8_t i2cData[4] =

 {

   data_4_7 | LCD_EN | LCD_BK_LIGHT | LCD_RS,

   data_4_7 | LCD_BK_LIGHT | LCD_RS,

   data_0_3 | LCD_EN | LCD_BK_LIGHT | LCD_RS,

   data_0_3 | LCD_BK_LIGHT | LCD_RS,

 };

 HAL_I2C_Master_Transmit(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS, i2cData, 4, 200);

}

I have a logic analyzer set to display I2C on the SCL and SDA pins and the results after a reset are:

write to 0x1F nak

read to 0x1F ack

read to 0x1F ack

read to 0x1F ack data:0x3F

read to 0x1E ack data: 0x6D

read to 0x79 ack

read to 0x36 nak

read to 0x36 nak

I have checked with a different tutorial for using the I2C LCD and the defines seem to match.

There are no jumpers on the A0 A1 or A2 lines which suggest it is at address 0x4E for write and 0x4F for read.

I suspect this may be simple but I have been looking for sometime and it seems to evade me.

Any suggestions would be greatly appreciated.

Regards to All.

17 REPLIES 17
TDK
Guru

> the LCD does not respond at all

> read to 0x1F ack

Is the LCD ACKing the requests or not? In which case it's responding. Perhaps I am misinterpreting.

You don't set the value of LCD_I2C_SLAVE_ADDRESS to 0x4E anywhere in your posted code. Do you do that somewhere?

What is the return value from HAL_I2C_Master_Transmit?

You don't set lcd16x2_i2cHandle anywhere in the posted code either. Since everything needs to be correct in order to work, it's hard to debug with only half the story.

If you feel a post has answered your question, please click "Accept as Solution".
SScot.3
Associate II

Thanks TDK.

I am looking for how to send 3 files (2 includes and the main.c) files to this post and cannot seem to get it. I have used the paperclip but not seeing 'how' to attach files.

SScot.3
Associate II

Ahh, just figured it out!

Attached are the files I am using. I am using the 1st I2C device and when hitting reset the SDA and SDC lines 'wiggle' so I believe I am wiring the correct port on the NUCLEO-H723ZG board which also gives you which proc I am using.

Thank you for looking into this.

Regards.

SScot.3
Associate II

Main File

SScot.3
Associate II

And the other include file

ZIP multiple files

Can only attach single file to responses. Top post can have multple, but don't need to have a dozen main.c files.​

​Consider posting a single file inline, without dependencies.

You explicit set the address to zero.​

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

I see that but I thought the next 2 lines would correct the 'cleared' variable.

At any hand I tried:

static uint8_t LCD_I2C_SLAVE_ADDRESS=0x4E;

and it did not print to the screen either.

TDK
Guru

You didn't answer about the HAL return values. Does HAL_I2C_IsDeviceReady return HAL_OK or not? That would be step 1. If that doesn't work, check wiring and slave address. If it does work, then your commands are probably incorrect.

I don't see anything immediately incorrect with the code, but I'm also not going to cross reference with the LCD datasheet to verify all commands are correct.

If you feel a post has answered your question, please click "Accept as Solution".

TDK, I realize that I am a noob at this, but I cannot find the ' HAL_I2C_IsDeviceReady' in any of my 3 files.