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
Newbie level 3
Joined
Nov 23, 2023
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
63
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
 
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) ;

}
Your code seems mostly correct for interfacing an LCD with an STM32F072C8T6 microcontroller. However, there are a few points to note:

1. GPIO Configuration: You're initializing GPIO pins for LCD control and data lines in the `LCD_GPIO_Init()` function. Ensure that `GPIOB` clock is enabled before initializing GPIO pins on it. Add `RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);` before initializing GPIO pins on `GPIOB`.

2. Delay Function: The `Delay()` function seems to be a simple busy-loop delay. Depending on your system clock frequency and optimization settings, you might need to adjust the loop count to achieve the desired delay duration. Verify that your delay function provides accurate timing.

3. LCD Initialization Sequence: The initialization sequence in `LCD_Init()` seems appropriate for many standard HD44780-compatible LCDs. However, LCDs can have different initialization requirements. Ensure that the sequence matches the datasheet of your specific LCD module.

4. LCD_SendCommand() Function: In the `LCD_SendCommand()` function, you're writing the command to GPIOB pins instead of GPIOA pins. Correct this by changing `GPIO_Write(GPIOB, command);` to `GPIO_Write(GPIOA, command);`.

5. LCD_SendData() Function: Similar to `LCD_SendCommand()`, ensure that you're writing data to the correct GPIO port. Change `GPIO_Write(GPIOA, data);` to `GPIO_Write(GPIOB, data);`.

6. Timing: Ensure that your delays between operations are appropriate for your LCD module. Some LCDs require longer delays for certain commands or data writes.

7. Error Handling: Implement error handling for GPIO initialization and LCD communication functions. This could involve checking return values of initialization functions and ensuring proper error codes or messages are provided in case of failure.

After addressing these points, your code should be in good shape for interfacing with an LCD using an STM32F072C8T6 microcontroller. Remember to refer to the datasheets of your specific components for accurate pin assignments, initialization sequences, and timing requirements.
 
Just on that 'Delay()' function - I would bet dollars to doughnuts that it is not doing anything.
Even with optimisation turned off, any half decent compiler will see that nCount is only being used in the 'while' loop which is otherwise empty. Therefore the functionally equivalent statement for the while 'while' loop is "nCount=0;'. As that value is not used again, it will remove even that functionally equivalent statement.
Therefore your function is a big 'noop'. Depending on the compiler it might not even get called.
Use the 'HAL-Delay()' (or whatever) function to have a delay that REALLY works.
Susan
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top