cancel
Showing results for 
Search instead for 
Did you mean: 

LCD interfacing Programming Doubt. Can anybody catch the mistakes?

SPand.25
Associate II

All connections are done on port D.

This is my program code.

I am using stm32f407vg disc board.

/*

 * lcd_programming.c

 *

 * Created on: 20-Aug-2020

 *   Author: SOBHIT PANDA

 */

#include "stm32f407xx.h"

#include "stm32f407xx_gpio_driver.h"

void LCD4bits_Cmd(unsigned char command);

void LCD4bits_Data(unsigned char data);

void delayUs(uint8_t n);

void delayMs(uint8_t n);

void LCD4Bits_Init (void);

void LCD_Line1 (unsigned char position);

void LCD_Line2 (unsigned char position);

void LCD_Nibble (unsigned char j);

void LCD_Clear (void);

#define disp_clear 0x01

#define disp_control 0x0C

#define func_set 0x28

int main()

{

LCD4Bits_Init();

LCD_Line1(0);

LCD4bits_Data("MICROCONTROLLER");

LCD_Line2(0);

LCD4bits_Data("KING");

while(1);

}

void LCD4Bits_Init (void)

{

GPIO_PeriClockControl(GPIOD,ENABLE);

GPIO_Handle_t LCD[8];

for (uint8_t i = 0 ; i < 8 ; i++)

{

LCD[i].pGPIOx = GPIOD ;

LCD[i].GPIO_PinConfig.GPIO_PinNumber = i;

LCD[i].GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_OUT ;

LCD[i].GPIO_PinConfig.GPIO_PinSpeed = GPIO_SPEED_LOW ;

LCD[i].GPIO_PinConfig.GPIO_PinOPType = GPIO_OP_TYPE_PP ;

LCD[i].GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_NO_PUPD ;

GPIO_Init(&LCD[i]);

}

GPIO_Handle_t RS = LCD[0];

GPIO_Handle_t EN = LCD[1];

GPIO_WriteToOutputPin(RS.pGPIOx , RS.GPIO_PinConfig.GPIO_PinNumber , DISABLE);

GPIO_WriteToOutputPin(EN.pGPIOx , EN.GPIO_PinConfig.GPIO_PinNumber , DISABLE);

delayMs(250);

LCD_Nibble(func_set);

delayMs(1);

LCD4bits_Cmd(func_set);

delayMs(1);

LCD4bits_Cmd(func_set);

delayMs(1);

LCD4bits_Cmd(func_set);

LCD4bits_Cmd(disp_control);

LCD_Clear();

}

void LCD_Nibble (unsigned char j)     // 4 bit mode function for lcd

{

GPIO_WriteToOutputPort(GPIOD, 0xF0 | j);

GPIO_WriteToOutputPin(GPIOD , 1 , ENABLE);

delayUs(50);

GPIO_WriteToOutputPin(GPIOD , 1 , DISABLE);

}

void LCD4bits_Cmd(unsigned char command)

{

uint8_t temp;

GPIO_WriteToOutputPin(GPIOD , 0 , DISABLE);

temp=command;

LCD_Nibble (temp);

temp = temp<<4 ;

LCD_Nibble (temp);

}

void LCD4bits_Data(unsigned char data)

{

uint8_t temp;

GPIO_WriteToOutputPin(GPIOD , 0 , ENABLE);

temp=data;

LCD_Nibble (temp);

temp=temp<<4;

LCD_Nibble (temp);

}

void LCD_Line1 (unsigned char position)  //lcd line 1 position function

{  LCD4bits_Cmd(0x80 + position); }

void LCD_Line2 (unsigned char position)  //lcd line 2 position function

{  LCD4bits_Cmd(0xC0 + position); }

void LCD_Clear (void)

{

LCD4bits_Cmd(disp_clear);

delayMs(50);

}

void delayUs(uint8_t n)

{

for (int i = 0; i<n ; i++)

{

for (int j = 0; j<3 ; j++)

{

;

}

}

}

void delayMs(uint8_t n)

{

for (int i = 0; i<n ; i++)

{

for (int j = 0; j<3180 ; j++)

{

;

}

}

}

3 REPLIES 3
TDK
Guru

What is the issue?

delayMs is pretty bad. The compiler could optimize it out entirely.

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

Its not working :loudly_crying_face:​. Screen is blank

Piranha
Chief II

Such delays are unreliable, but at least for now add volatile qualifiers to counter variables.

Think about what this does:

GPIO_WriteToOutputPort(GPIOD, 0xF0 | j);