Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

is my code correct or not for lcd interfacing with stmf072c8t6 mc ??

smeraj580

Newbie
Joined
Nov 23, 2023
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
57
C:
#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"

void LCD_GPIO_Init(void);
void LCD_Init(void);
void LCD_SendCommand(uint8_t);
void Delay(uint32_t);

void LCD_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3| GPIO_Pin_4
            | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void LCD_Init(void)
{
        Delay(20000);


           LCD_SendCommand(0x38);

           LCD_SendCommand(0x0C);

           LCD_SendCommand(0x01);

           LCD_SendCommand(0x06);
}



void LCD_SendCommand(uint8_t command)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_6);
    GPIO_ResetBits(GPIOA, GPIO_Pin_7);

    GPIO_Write(GPIOB,command);

    GPIO_SetBits(GPIOA, GPIO_Pin_8);
    Delay(50);
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);

    Delay(2000);
}


void LCD_SendData(uint8_t data)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_6);
    GPIO_ResetBits(GPIOA, GPIO_Pin_7);

    GPIO_Write(GPIOA,data);

    GPIO_SetBits(GPIOA, GPIO_Pin_8);
    Delay(50);
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);

    Delay(200);
}

void LCD_DisplayString(const char* str)
{
    while (*str)
    {
        LCD_SendData(*str++);
    }
}

void Delay(uint32_t nCount)
{
    while (nCount--){
    }
}

int main(void) {
    LCD_GPIO_Init();
    LCD_Init();

    LCD_DisplayString("Hello, STM32!");

    while (1) ;

}
 
It would help if you told us a bit more about the LCD that you are using - they are not all the same. Therefore the following are 'generic' comments.
I would not truest your 'Delay' function to do anything - any half-decent compiler will see that nothing really changes and convert the whole thing to a 'nop'. Worse, if it works during the initial debugging, it won't as soon as you try to do a 'release' compile. There are HAL and other delays functions that will always work.
Personally I would not put a delay at the end of the 'LCD_SendData' and 'LCD_SendCommand' functions. During the LCD initialisation the delays are critical, but for general use after that they really depend on the operation requested. Therefore put the delays where they are really needed.
Susan
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top