0

我正在尝试使用 CoIDE 学习 C++ 中的嵌入式开发。我在分线板上有一个 STM32F0 芯片。我已经完成了一些 LED 教程等。我被困在这段代码上,该代码应该向 LCD 写入一些简单的文本字符串。我正在关注的教程是在eeherald 教程中,我已将代码改编为我的 STM32F0 芯片。我想我已经接近了,但是 LCD 只是停留在初始化模式,所有单元都亮起。屏幕永远不会清除以写入文本。这是我的代码...任何能帮助我指出正确方向的帮助都将不胜感激!

我“认为”问题可能出在数据方向代码的 initGPIO() 中,但我不确定……我尝试了很多不同的东西,但没有运气。

enter code here
#include "stm32f0xx_hal.h"

#define EN  12 // EN Enable on PortB chip pin#53
#define RW  11 // RW Read Write on PortB chip pin#52
#define RS  10 // RS Register Select on PortB chip pin#51

void initGPIO(void);
void s_init(void);
void s_data(void);
void s_latch(void);

//------------------------------------------------------------------------------
// Function Name : Init GPIO
// Description : pins ,port clock & mode initialization.
//------------------------------------------------------------------------------
void initGPIO()
{
    // Define GPIO Structures
    GPIO_InitTypeDef GPIO_InitStructure;

    // Reset and clock control - Advanced high-performance bus - Enabling GPIO Port B and Port C
    __GPIOB_CLK_ENABLE();
    __GPIOC_CLK_ENABLE();

    // Set Data Direction for Port C
    GPIO_InitStructure.Pin = RS | RW | EN;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);

    //Set Data Direction for Port A
    GPIO_InitStructure.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_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//------------------------------------------------------------------------------
// Function Name : s_init
// Description : Send Instruction Function (RS=0 & RW=0)
//------------------------------------------------------------------------------

void s_init()
{
    GPIOC->BRR=RS;
    GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_data
// Description : Send Data Select routine(RS=1 & RW=0)
//------------------------------------------------------------------------------

void s_data()
{
    GPIOC->BSRR=RS;
    GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_latch
// Description : Latch Data/Instruction on LCD Databus.
//------------------------------------------------------------------------------

void s_latch()
{
    GPIOC->BSRR=EN;
    HAL_Delay(10);
    GPIOC->BRR=EN;
    HAL_Delay(10);
}

/*******************************************************************************
* Function Name : main
* Description : Main program.
*******************************************************************************/
int main(void) //Main function
{

    initGPIO();

    int k=0;
    char a[]="WWW.EEHERALD.COM";
    char b[]="EMBEDDED SYSTEMS";

    GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction Send
    GPIOC->BRR=RW; // Select RW=0 to write Instruction/data on LCD
    GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial condition)

    HAL_Delay(10);

    s_init(); //Call Instruction Select routine
    GPIOB->ODR=0x0001; // Clear Display, Cursor to Home
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0038; // Display Function (2 rows for 8-bit data; small)
    s_latch(); //Latch this above instruction 4 times
    s_latch();
    s_latch();
    s_latch();
    GPIOB->ODR=0x000E; // Display and Cursor on, Cursor Blink off
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0010; // Cursor shift left
    s_latch(); //Latch the above instruction
    GPIOB->ODR=0x0006; // Cursor Increment, Shift off
    s_data(); //Change the input type to Data.(before it was instruction input)
    s_latch(); //Latch the above instruction

    for(k=0;a[k];k++)
    {
        GPIOB->ODR=a[k]; //It will send a[0]='P' as = '0x0050' on Port B.
        s_latch(); //Latch the above instruction only once. Or it will clone each character twice if you latch twice.
    }
    GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction Send
    GPIOC->BRR=RW; // Select RW=0 to write Instruction/data on LCD
    GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial condition)


    GPIOB->ODR=0x00C0; // Move cursor to beginning of second row
    s_latch(); //Latch the above instruction
    s_data(); //Change the input type to Data.(before it was instruction input)
    for(k=0;b[k];k++)
    {
        GPIOB->ODR=b[k]; //It will send b[0]='E' as = '0x0044' on Port B.
        s_latch();//Latch the above instruction only once. Or it will clone each character twice if you latch twice.
    }
    s_init();
}
4

0 回答 0