Interface LCD 16×2 via I2C with STM32

Today I am going to interface LCD to STM32 using an I2C device (PCF8574). PCF8574 can be used as a port extender, to which LCD will be connected. If you haven’t read my previous post about I2C go check that out HERE.

pcf8574
pcf8574

As you can see above PCF8574 has 4 input pins GND, VCC, SDA, SCL and 16 output pins. We will connect our LCD to these 16 pins.

What is the ADDRESS for PCF8574

The higher nibble of PCF8574 address is 0100 and this is fixed. But lower nibble can be modified according to our convenience.  The question you must be thinking is  why we need to modify lower nibble? 

Well you generally don’t but as I mentioned in my previous article that we can connect up to 128 devices on the same I2C line and let’s say we want to connect two different LCDs on the same I2C line, than we can’t use two PCF8574 with same addresses and we need to modify one of them.

So how do we modify the address?

  • The address of the PCF8574 is 0 1 0 0 A2 A1 A0 R/W. To change the address we are provided with A0, A1 and A2 pins.
  • By default these three pins are high so the address by default is 01001110 which is 0x4E.  
  • To change the address of this device, you have to connect any/all of these three pins to ground, which is provided just above them.
  • So let’s say you connected A0 to ground, new address will be 01001100 which is 0x4C.
  • In this manner, we can connect up to 8 LCDs to the same line.
  • I want to point to one more thing here, the last bit of the address is kept 0 intentionally, because this bit is responsible for read(1)/ write(0) operation.


HOW TO Connect LCD to PCF8574

As shown in the figure above, first pin of the device is Vss which is pin 1 of LCD. So all you have to do is connect first pins of the LCD to Vss above and rest will connect accordingly.   Starting with Vss as first pin, connection is as follows:-

pcf connection to lcd
connection LCD





Some Insight into the Code

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

The default slave address defined in the i2c-lcd.c is 0x4E. This is default for the PCF8574.


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);
}
  • The above function will send the command to the device to which our LCD is connected.
  • As we are using 4 bit LCD mode, we have to send command in two parts.
  • First we send the upper nibble, and than the lower one. Both parts are sent along enable pin 1 and than with enable pin 0.
  • When the data_t is OR(|) with 0x0C, which implies that P2 (En) pin is high and P0 (RS), P1(R/W) are low for the command and write operation.
  • In the second case data is sent along with 0x08 only. This is to ensure that the back light is on and En, RS, R/W are all low.

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=1
	data_t[1] = data_u|0x09;  //en=0, rs=1
	data_t[2] = data_l|0x0D;  //en=1, rs=1
	data_t[3] = data_l|0x09;  //en=0, rs=1
	HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}
  • The above function sends the data to the device to which our LCD is connected.
  • Similarly like command, We have to send data in two parts, first upper and than lower half.
  • Both parts are sent along enable pin 1 and than with enable pin 0. Data is OR(|) with 0x0D which implies that P2 (En) and P0 (RS) pin are high , P1(R/W) is low and back light is on, for the data and write operation.
  • In second case data is OR(|) with 0x09 to make only RS pin high and for the back light, and all others low.  

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)
}

As according to the datasheet of the LCD 16×2, in order to initialize the LCD, we have to use some sequence of commands. The code is commented properly, so that you can understand it better


void lcd_send_string (char *str)
{
	while (*str) lcd_send_data (*str++);
}

Above function can be Used to send the string to the LCD.




RESULT

Result_lcd

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

121 Comments. Leave new

  • doesn’t write anything in the second line… (

    Reply
  • doesn’t write anything in the second line… (

    Reply
  • i add “i2c-lcd.h” and “i2c-lcd.c” into my project,1602 LCD can not print number(0,1,2) with ” lcd_send_data(1)”.howeverm LCD can print “string”.

    Reply
    • displays can only print ascii characters.
      You need to convert numbers into the relevant ascii character.
      sprintf (buffer, “%d”, num);
      lcd_send_string (buffer);

      Reply
  • void lcd_clear (void)
    {
    lcd_send_cmd(0x01);
       delay_ms(2);
    }

    Reply
  • //Add function
    void lcd_gotoxy(unsigned char x, unsigned char y){ 
      unsigned char xy; 
      if(y==0){xy=0x80;}    
      if(y==1){xy=0xC0;}    
      if(y==2){xy=0x94;}    
      if(y==3){xy=0xD4;}
      xy=xy+x;
      lcd_send_cmd (xy);
    }

    Reply
  • Hi, I want to suggest the following code for PC8574 with 9 pins (blue board)
    //Rs–>P0, RW–>P1, E–>P2, D4–>P4, D5–>P5, D6–>P6, D7–>P7
    void lcd_send_cmd(char cmd)
    {
    uint8_t cmd_t[4];

    cmd_t[0]=(cmd&0xf0)|(0x04); //cmd_u ,E=1, RS=0
    cmd_t[1]=(cmd&0xf0); //E=0, RS=0
    cmd_t[2]=((cmd<<4)&0xf0)|(0x04); //cmd_l E=1, RS=0
    cmd_t[3]=((cmd<<4)&0xf0); //E=0, RS=0
    HAL_I2C_Master_Transmit(&hi2c1, SLAVE_ADDRESS_LCD, (uint8_t *) cmd_t, 4, 200);
    }
    void lcd_send_data(char data)
    {
    uint8_t data_t[4];
    data_t[0]=(data&0xf0)|(0x05); //data_u , E=1, RS=1
    data_t[1]=(data&0xf0)|(0x01); //E=0, RS=1
    data_t[2]=((data<<4)&0xf0)|(0x05); //data_l, E=1, RS=1
    data_t[3]=((data<<4)&0xf0)|(0x01); //E=0, RS=1
    HAL_I2C_Master_Transmit(&hi2c1, SLAVE_ADDRESS_LCD, (uint8_t *) data_t, 4, 200);
    }

    thank you so much!!

    Reply
  • Savino Giovanni
    October 9, 2022 9:58 PM

    hi, i follow every steps but on my lcd i see only the ligth but no word. Can you help me plese?

    Reply
  • it’s work very fine. Can you help me to print the value of the potentiometer linked on ADC please?

    Reply
  • Hi I imported the project but it will not run it keeps saying “this launch configuration requires the selected build configuration to use the MCU ARM GCC toolchain”
    how do I fix this?

    Reply
  • Arvin Ghahremani
    January 30, 2022 12:01 PM

    Hi everyone. I have a problem when my program does software reset, my LCD shows noisy characters after running LCD_init(). I have to reset it again to work properly. How can I fix it? It’s imprortant when I use IWDG

    Reply
  • When I try to display numbers above 9, it prints it corresponding ASCII characters.How will I print a value stored in a variable which is incremented or varying.

    Thanks for the tutorial.It works fine

    Reply
    • I also wanted to print ADC values which have float and integer values.

      Reply
      • you should use sprintf to convert the numbers to characters.

        Reply
        • Do you have an example code for that function with the above-attached header files and source files ? Please send it or mention it if you have.

          Reply
          • let’s say you want to convert 1234 to characters.
            unsigned int num = 1234;
            char buffer[4];
            sprintf (buffer, “%u”, num);

  • does it work in mode 8bit?

    Reply
  • Juan Figueroa
    July 13, 2021 8:44 PM

    its works, but i am having problems with the brightness and it is not the potentiometer, where should i change it?

    Reply
  • kishor sherolla
    May 1, 2021 6:42 PM

    sir i want to toggle curser set position on lcd

    Reply
  • You are telling so many topics, you are wasting your time. We are watching you until the end, but you do not share any files. How are we going to experiment ourselves? Every download link contains ads. it still does not download. Why follow as long as you don’t provide any support here?

    There are not even header and source files suitable for your program. Or you don’t even have a general program. If we get it wrong, at least somewhere, we can download your program and try it out.

    But there is no file.

    Reply
    • Sorry man. I found it . Please change the still for download button:))

      I am so sorry . My fault. I did not see it.

      Reply
  • where can download the files? I can`t see the link

    Reply
  • ok

    Reply
  • How can set cursor in another position?

    Reply
  • Another way to clear the screen is:
    lcd_send_cmd(1);

    Cheers

    Reply
  • Excellent. Worked first time. Thanks a lot.

    Reply
  • well done, thanks

    Reply
  • Leonardo Ferreira
    May 21, 2020 6:56 PM

    I had to use Direct manipulation on the I2C registers on my Nucleo STM32F767 to make this thing works -_-

    Reply
  • There are slightly different I2C parts with different I2C address 0x3F for -AT and 0x27 for -T
    Refer to spec and modify you code for part you have.

    Reply
  • The PCF8574AT has I2C address as shown in this code starting with 0x3F.

    The PCF8574T has different I2C address starting with 0x27.

    Refer to part spec.
    You may need to modify I2C address for the part you have.

    Reply
  • hi
    how can send custom character with your library?
    (is it even possible)

    Reply
    • hi again
      i managed to create custom character using your library like below:
      /* code
      const char UserFont[8][8] =
      {
      { 0x11,0x0A,0x04,0x1B,0x11,0x11,0x11,0x0E },
      { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
      { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
      { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
      { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
      { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
      { 0x0E,0x1F,0x11,0x11,0x13,0x17,0x1F,0x1F },
      { 0x1F,0x11,0x11,0x11,0x11,0x11,0x11,0x1F }
      };

      lcd_send_cmd(0x40); // Set CGRAM address counter to 0
      char const *p1;
      p1 = &UserFont[0][0];
      for (int i = 0; i < sizeof(UserFont); i++, p1++){
      lcd_send_data(*p1);
      }
      lcd_send_cmd(0x80);
      */
      now sending character 0x00 to 0x07 displays 8 predefined custom character

      Reply
  • If there is anyone that can help me i send him my code. The circuit that i mounted is the same of the youtube tutorial.

    Reply
  • Hello, i have problem with this project. Can anyone help me? I found the i2c addres with i2c scanner did with keil, not arduino: it’s 0x3F. The code is the same that i have downloaded from this site; maybe change some setting into frequency or clock because i use an stm32f767zi. Please help me.

    Reply
  • Hi, is it possible to avoid using HAL_DELAY() in void lcd_init (void)? What else can I use in the init function instead of HAL_DEALY() ?

    Reply
  • Error building…

    ../Core/Src/i2c-lcd.c:20: undefined reference to `hi2c1′

    Reply
  • Hi Everyone, I am using with stm32f072 board and have an lcd with PCF8574 converter. The project is building successfully but for some reason I do not get anything on my lcd. What could be the reason? This is the first time I am trying to send something to my lcd.

    Reply
    • Try adjusting the potentiometer for the contrast

      Reply
      • I have done that. I am not sure the slave address though. How can I make sure 0x4E is an appropriate one?

        Reply
        • if you are using pcf8574 (not any other version of it) than the address is 0x4E. Read the datasheet for the address related querry

          Reply
          • That is interesting but I have different manual – by Philips Semiconductors. I do not have that address reference in there, but for addressing I have a slave address: S 0 1 0 0 A2 A1 A0 0 A

          • that is how the addressing is. 0100 (4) and A2 A1 A0 pins are high by default. which makes the default address as 0x4E.

          • Ok, I do not know how it came but it is working now 🙂 Thank you.

          • Do I need to change / modify libs for sending ADC temperature results to my lcd ?

          • Hi, I am struggling to get it working. For some reason, am not successful. Can you share your code (vrtata@gmail.com) and wiring of the setup? It will be a great help.

  • Anh Tu Nguyen
    January 18, 2020 4:40 PM

    For binary 01001100 , I think it should be 76 (decimal) and it means 0x4C instead of 0x4B

    Reply
  • Hello!
    Redid the project in STM32CUBEIDE does not work. Blue pill board.

    Reply
  • Hello, Thank you for this code.I use your code, when I initialize LCD my LCD’s back-light is goes off,why this happens?could you help me?

    Reply
    • The backlight was the issue but it’s fixed now. I don’t know what is happening in your case. Check the send command function in i2clcd.c file and confirm if it is same as below
      0x0c , 0x08, 0x0c, 0x08
      And data function should be 0x0d, 0x09, 0x0d, 0x09.

      Reply
  • I’m using your code but all the i2c bus is sending are ‘Setup Write to [&] + NAK’ packets over and over and over again. I’m using salae’s logic program and an analyzer to get the data. The board is a blue pill with some chinese knockoff STM32f103c8 but it’s working as normal so far except for the code. Any ideas??

    Reply
    • What are u using to connect lcd to the I2C ?
      If it is pcf8574, are u using the address 0x4E ?

      Reply
      • It’s an I2C backpack module for the 16×2 lcd screen. The chip is a PFC8574AT and using the arduino and some handy I2C scanner code I found that the address is 0x27 which I also placed in the code instead of your provided address.

        Reply
        • The address for the PCF8574 is not 0x27. Don’t use the arduino one. Look at the datasheet and use the proper address. I think it’s 0x4E

          Reply
  • It works now, my chip is PCF8574AT, the address is 7E, thanks,
    I changed on backlight side, replace the transistor with 470 ohm resistor, so it’s on always…cheers…

    Reply
  • it’s not working yet with STM32F107VCT6, will it work with 3.3V supply and bus ? thanks

    Reply
    • What error are u facing ? Are you using pcf8574 ? Is the slave address correct ? Do the debugging and find out the error type. Only than i can help you.
      And keep the vcc at 5v for the lcd
      Sck and sda are connected to the microcontroller pins so they will be at 3.3v always.
      Also try using pull up at sck and sda and see if it helps.

      Reply
  • Anonymous123
    May 22, 2019 1:23 AM

    i’m having problems using it, i did an exact copy of the code, activated the I2C on cubemx and the display isn’t doing anything at all besides turning on and i already adjusted brightness, i tried to measure RW pin with multimeter and i got a 5V signal everytime i did it, so i think it’s the RW but i don’t know how i change it

    Reply
  • it doesn’t work in my 16×4 lcd, can you help me to use 16×4 lcd?

    Reply
  • Hello, can I use your i2c-lcd library for STM32F072B-discovery? Is it ok when I replace in i2c-lcd.h #include “stm32f4xx_hal.h” for #include “stm32f0xx_hal.h”. Thank you.

    Reply
    • yeah sure you can use it

      Reply
      • I don’t know where i do mistake. The address of display I have default 0x4E.

        /* Includes ——————————————————————*/
        #include “main.h”
        #include “i2c-lcd.h”
        /* Private variables ———————————————————*/
        I2C_HandleTypeDef hi2c1;

        /* Private function prototypes ———————————————–*/
        void SystemClock_Config(void);
        static void MX_GPIO_Init(void);
        static void MX_I2C1_Init(void);

        /* Private user code ———————————————————*/

        int main(void)
        {

        /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
        HAL_Init();

        /* Configure the system clock */
        SystemClock_Config();

        /* Initialize all configured peripherals */
        MX_GPIO_Init();
        MX_I2C1_Init();
        /* Infinite loop */
        while (1)
        {

        lcd_init ();
        lcd_send_string (“HELLO WORLD”);
        HAL_Delay(100);

        }
        }

        /**
        * @brief System Clock Configuration
        * @retval None
        */
        void SystemClock_Config(void)
        {
        RCC_OscInitTypeDef RCC_OscInitStruct = {0};
        RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
        RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

        /** Initializes the CPU, AHB and APB busses clocks
        */
        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.PLLMUL = RCC_PLL_MUL6;
        RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
        if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
        {
        Error_Handler();
        }
        /** Initializes the CPU, AHB and APB busses clocks
        */
        RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
        |RCC_CLOCKTYPE_PCLK1;
        RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
        RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
        RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

        if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
        {
        Error_Handler();
        }
        PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
        PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
        {
        Error_Handler();
        }
        }

        /**
        * @brief I2C1 Initialization Function
        * @param None
        * @retval None
        */
        static void MX_I2C1_Init(void)
        {

        hi2c1.Instance = I2C1;
        hi2c1.Init.Timing = 0x00101D2D;
        hi2c1.Init.OwnAddress1 = 0;
        hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
        hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
        hi2c1.Init.OwnAddress2 = 0;
        hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
        hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
        hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
        if (HAL_I2C_Init(&hi2c1) != HAL_OK)
        {
        Error_Handler();
        }
        /** Configure Analogue filter
        */
        if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_DISABLE) != HAL_OK)
        {
        Error_Handler();
        }
        /** Configure Digital filter
        */
        if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
        {
        Error_Handler();
        }
        /* USER CODE BEGIN I2C1_Init 2 */

        /* USER CODE END I2C1_Init 2 */

        }

        /**
        * @brief GPIO Initialization Function
        * @param None
        * @retval None
        */
        static void MX_GPIO_Init(void)
        {

        /* GPIO Ports Clock Enable */
        __HAL_RCC_GPIOB_CLK_ENABLE();

        }

        Reply
  • Jose Ronaldo
    March 16, 2019 4:14 AM

    I use STM32F103C8 + PCF8574A + Keil-5, does not work.
    The program scans and finds the address 0x3F.
    Works well with STM32F103C8 + PCF8574A + Arduino IDE using address 0x3F.
    Works well STM32F103C8 + SSD1306 + Keil-5.
    Does anyone have an idea what the problem is?
    Regards

    Reply
    • use the address 0x4E

      Reply
      • i try it use 0x4E and others address but can’t work too,

        Reply
        • exactly me too. did you have any solution for your problem? all of my problem is similar you. and i dont know why. if you have any suggestion please tell me.
          best regards

          Reply
      • exactly me too. did you have any solution for your problem? i have problem like him. and i dont know why. if you have any suggestion please tell me. i tested lots of addresses but i did not get any answer.
        best regards

        Reply
        • 1. If you are using pcf8574, the default address is 0x4E, so don’t test anything else.
          2. I have updated the code here now it works with CUBEIDE by default. If u want to use keil, you have to create project and than unclude those 2 files.
          Everything is working properly. I have rechecked it.

          Reply
          • I downloaded your code and tried executing but its not working for me. My setup is STM32F103C8 + PCF8574T + STM32CubeIDE. Any idea what could be the problem?

          • Make sure the contrast is set properly. Most of the times, it is working but you can’t see because of contrast.
            You can try using slave address 0x7E

          • It was not working for me. What I realized is the execution is going into infinite loop at HAL_Delay(50). Any inputs on why this could be? Preemption Priority for System tick timer is set to 0.

          • Check your system setup. Looks like some problem with the clock setup. Before attempting this, try blinking the LED.

          • It was the clock issue. Now I do not have issues with HAL_Delay. The program runs but am unable to get any output on either the display or LogicAnalyzer. I have a feeling I am doing something stupid in my wiring. Your exact code does not produce any results. Need help with verifying my wiring. Is there a diagram I can refer to. Now am obsessed with making this work.

          • Working for me. Was a faulty bluepill

          • RONALDO MARTINS
            November 11, 2020 4:37 AM

            What did you do to work

          • I do not know why. But for some reason the code doesn’t work from I2C1. I changed the bluepill chip. There is no problem with the hardware. It works fine from I2C2.

  • Thanks for your code it works even with a 20×4 display with some changes.
    I have a question: Why does your code work without the required waitings between the init commands?
    The datasheet says that you have to wait more tha 4.1 ms between the first instructions.
    Thanks

    Reply
  • hi how to write code for scrolling display for 20*4 now i am able to write but it is improper please help me in the issuse

    Reply
  • I can’t thank you enough for this lib!
    Such a great and working lib and so simple!!!

    Feel free to contact me for colaboration, I’d love to!

    JS

    Reply
  • Thomas Christensen
    January 31, 2018 1:57 AM

    Great tutorials/examples (not only this one). Thanks for sharing.
    Can you give an estimate of the refresh rate when writing all 2×16 characters. Is it 1 sec. to write all characters or 0.1 sec.?
    Do you consider such a display setup an acceptable debugging option?

    Reply
  • I’ve got one question.
    How to use it it other commands, for example command to clear screen ?
    Can I find anywhere list of hex code for functions ?

    Reply
    • To get the commands just google lcd 16×2 commands.
      To use it, there is a function available i.e. lcd_send_cmd (cmd);
      for eg to clear screen, type lcd_send_cmd (0x01);

      Reply
  • I wrote to you in YT but its proper place . When you initialize LCD you must send 4-bit mode at first with delay
    void lcd_init (void)
    {
    uint8_t i=0;
    HAL_Delay(100);
    for(i=0;i<3;i++)//sending 3 times: select 4-bit mode
    {
    lcd_send_cmd(0x03);
    HAL_Delay(45);
    }
    lcd_send_cmd (0x02);
    HAL_Delay(100);
    lcd_send_cmd (0x28);
    HAL_Delay(1);
    lcd_send_cmd (0x0c);
    HAL_Delay(1);
    lcd_send_cmd (0x80);
    HAL_Delay(1);
    }

    and some additional function to set proper location on screen :
    void lcd_print_x_y(uint8_t line , uint8_t row, char *str )
    {
    if (line == 0 ){
    uint8_t line_x_y = 0x80 + row ;
    lcd_send_cmd(line_x_y);
    while (*str) lcd_send_data (*str++);
    }else if (line == 1 ){
    uint8_t line_x_y = 0x80 |( 0x40 + row) ;
    lcd_send_cmd(line_x_y);
    while (*str) lcd_send_data (*str++);
    }
    }

    Reply
    • Nice work
      Could you send me the library that you have used ( i2c-lcd )

      Reply
    • Would you know how to insert special characters in CGRAM in this I2C communication to the LCD?

      Reply
    • Thank you very much PawelDNB (even after 4 years)!

      Using the old initalization I faced the problem that from time to time
      the display was showing crap after power up.
      With your initalization routine the error disappeared!

      Reply
  • Nice.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×