STM32CubeIDE operation LCD1602

 

to add on:

 Note: All commands, data reading and writing require a pulse from "E".

STM32CubeIDE first needs to initialize the chip, as shown in the figure:

 The code in the book is very comprehensive but not conducive to understanding, simplify it, the general idea.

 1. Determine whether to write data or write instructions through the RS pin.

2. The read-write control terminal (RW) is set to write mode.

3. Send data or instructions to the data line. (D0~D7)

4. Give E a high pulse to send data to the LCD controller.


    WriteData(0x38);

/* Prepare D0~D7  data, set 16*2 display, 5*7 dot matrix, 8-bit data interface.

 */


    HAL_GPIO_WritePin(GPIOA, RS_Pin, GPIO_PIN_RESET); 
    HAL_GPIO_WritePin(GPIOA, RW_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA, E_Pin, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);
    HAL_Delay(2);


    WriteData(0x01);

/* Prepare D0~D7  data, clear the display, and clear the data pointer.

 */


    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);


    WriteData(0x06);

/* Prepare D0~D7  data, add 1 to the address pointer after writing a character

 */
    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);
    HAL_Delay(2);


    WriteData(0x0c);

/* Prepare D0~D7  data, set the display to on, and not to display the cursor. Code table see above picture*/
    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);
    HAL_Delay(2);


    WriteData(0x80);

/*定位*/
    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);
    HAL_Delay(2);


    WriteData('W');

/*写入字符“W”*/
    HAL_GPIO_WritePin(GPIOA, RS_Pin, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOA, RW_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA, E_Pin, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOA,RS_Pin,GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOA,RW_Pin,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_SET);
    __NOP();
    HAL_GPIO_WritePin(GPIOA,E_Pin,GPIO_PIN_RESET);
    HAL_Delay(2);

Got it :) 

 The WriteData function is rewritten with qt, and you will know why you have to do it this way:)

        unsigned char dat=0x38;
        unsigned short Set_Pins=0;
        unsigned short Rst_Pins=0;
        if(dat & 0x01)Set_Pins|=0x0001;
        else          Rst_Pins|=0x0001;
        if(dat & 0x02)Set_Pins|=0x0002;
        else          Rst_Pins|=0x0002;
        if(dat & 0x04)Set_Pins|=0x0004;
        else          Rst_Pins|=0x0004;
        if(dat & 0x08)Set_Pins|=0x0008;
        else          Rst_Pins|=0x0008;
        if(dat & 0x10)Set_Pins|=0x0010;
        else          Rst_Pins|=0x0010;
        if(dat & 0x20)Set_Pins|=0x0020;
        else          Rst_Pins|=0x0020;
        if(dat & 0x40)Set_Pins|=0x0040;
        else          Rst_Pins|=0x0040;
        if(dat & 0x80)Set_Pins|=0x0080;
        else          Rst_Pins|=0x0080;
        qDebug()<<QString::number(Set_Pins,2)<<"|"<<QString::number(Rst_Pins,2);

Guess you like

Origin blog.csdn.net/wangz76/article/details/127672387