Setting up LCD to Print String

Hello All,

I truly tried for hours to figure out why my LCD (ST7066U) will not turn on the cursor, let alone print a string. It did work once, but after running another program, it would not turn back on. The code I researched for help was found on this subreddit (https://www.reddit.com/r/stm32f4/comments/cb33gl/printing_an_adc_value_on_an_lcd_screen/). All I did was change my GPIO ports:

​

PB0, PB1, and PB2 = RS, r/W, and E, respectively;

PC8, PC9, PC10, and PC11 = DB4, DB5, DB6, and DB7 respectively.

​

void numberToGpio (char number)// change a 4 bit number into gpio outputs

{

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, 0); //reset pins

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, 0);

if(number>=8)

​

{

​

`HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, 1);`

​

`number=number-8;`

​

}

​

if(number>=4)

​

`{`

​

\`HAL\_GPIO\_WritePin(GPIOC, GPIO\_PIN\_9, 1);\`

​

\`number=number-4;\`

​

`}`

​

if(number>=2)

​

`{`

​

\`HAL\_GPIO\_WritePin(GPIOC, GPIO\_PIN\_10, 1);\`

​

\`number=number-2;\`

​

`}`

​

if(number>=1)

​

`{`

​

\`HAL\_GPIO\_WritePin(GPIOC, GPIO\_PIN\_11, 1);\`

​

`}`

}

​

void send4bitCommand (char number) // send a 4 bit command

{

`HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 0); //rs=0 ==> cmd`

​

`HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 0); //R/W=O ==> write`

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); //En=1

​

HAL_Delay(5);

​

numberToGpio (number);

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 0); //En=0

​

HAL_Delay(5);

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, 0); //reset pins

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, 0);

}

​

void sendCommand (char number)

{

char lower=number & 0x0F;

​

char higher=(number>>4) & 0x0F;

​

send4bitCommand(higher);

​

send4bitCommand(lower);

}

​

void sendData (char number)

{

char lower=number & 0x0F;

​

char higher=(number>>4) & 0x0F;

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1); //rs=1 ==> data

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 0); //R/W=O ==> write

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); //En=1

​

HAL_Delay(5);

​

numberToGpio (higher);

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 0); //En=0

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, 0); //reset pins

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, 0);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1); //rs=1 ==> data

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 1); //R/W=O ==> write

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); //En=1

​

HAL_Delay(5);

​

numberToGpio (lower);

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 0); //En=0

​

HAL_Delay(5);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, 0); //reset pins

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, 0);

​

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, 0);

}

​

void initLCD()

{

//init sequence

​

HAL_Delay(100);

​

send4bitCommand(0x3);

​

HAL_Delay(5);

​

send4bitCommand(0x3);// no need to add delay, since i already added 1sec delay in the sendCommadn function

​

send4bitCommand(0x3);

​

send4bitCommand(0x2); //function set 4 bit

​

sendCommand(0x28);//function set 4 bit 2 lines, 5\*8

​

sendCommand(0x08);//forced to put 0x08 for the initialization ==> display off

​

sendCommand(0x01);//clear

​

HAL_Delay(3);

​

sendCommand(0x06);//entry mode set incremente et shift cursor

​

//display control function to put the display on

​

sendCommand(0x0F);//the "real" cursor or display shift: Display, cursor and blink on

HAL_Delay(50);

}

​

int main(void)

{

`HAL_Init();`

​

`MX_GPIO_Init();`

​

`SystemClock_Config();`

​

`initLCD();`

​

`for(;;);`

}

Any advice would greatly be appreciated! This is NOT for school, just learning to play around with the HAL library. I also prefer not to use CubeMX (all of this was manual.) Again, thanks and Happy Holidays!

Posted Dec 25, 2019, 3:32 AM UTC

02
GlassConclusion•

If you haven't figured it out, put it on github and send me a PM.

1
willynilly271OP•

Ok, will do. And thank you for your help!

1