为STM32F0 LCD编写C ++

时间:2016-10-23 20:05:21

标签: c++ gpio lcd hd44780 stm32f0

我正在尝试使用CoIDE学习C ++中的嵌入式开发。我在分线板上有一个STM32F0芯片。我已经完成了一些LED教程等等。我坚持使用这个代码,它应该在LCD上写一些简单的文本字符串。我正在关注的教程是eeherald tutorial我已经将代码改编为我的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();
}

0 个答案:

没有答案
相关问题