简体   繁体   English

STM32f4与LCD16x2的接口

[英]Interface STM32f4 with LCD16x2

I write program interface STM32F411vet6 with LCD 16x2 text but when I deliver code to kit it doesn't run. 我用LCD 16x2文本编写程序接口STM32F411vet6,但是当我将代码传递给工具包时,它无法运行。 Can someone check my code and find error please? 有人可以检查我的代码并找到错误吗? Thank you so much! 非常感谢!

Describe: 描述:

  • void ENEable(void): Wait microsecond to RS pin can receive command. void ENEable(void):等待微秒,直到RS引脚可以接收命令。

  • void WriteNibble(int8_t data, int8_t flag): Write high nibble(flag = 0) and low nibble(flag = 1) void WriteNibble(int8_t数据,int8_t标志):写高半字节(标志= 0)和低半字节(标志= 1)

  • void LCDCmd(int8_t cmd): Require KIT run command void LCDCmd(int8_t cmd):需要KIT运行命令

  • void LCDInit(void): initialize LCD. void LCDInit(void):初始化LCD。

Code LCD 16x2: 代码LCD 16x2:

void ENEable(void)
{
    HIGH_PIN(EN_PIN);
    DelayMicros(5);
    LOW_PIN(EN_PIN);
    DelayMicros(100);
}

void WriteNibble(int8_t data, int8_t flag)
{
    flag = 4 * flag;
    if(data & (0x80 >> flag))
    {
        HIGH_PIN(D7_PIN);
    }
    else
    {
        LOW_PIN(D7_PIN);
    }

    if(data & (0x40 >> flag))
    {
        HIGH_PIN(D6_PIN);
    }
    else
    {
        LOW_PIN(D6_PIN);
    }

    if(data & (0x20 >> flag))
    {
        HIGH_PIN(D5_PIN);
    }
    else
    {
        LOW_PIN(D5_PIN);
    }

    if(data & (0x10 >> flag))
    {
        HIGH_PIN(D4_PIN);
    }
    else
    {
        LOW_PIN(D4_PIN);
    }
}

void LCDCmd(int8_t cmd)
{
    LOW_PIN(RS_PIN);
    DelayMicros(100);

    WriteNibble(cmd, 0);
    ENEable();
    WriteNibble(cmd, 1);
    ENEable();

    switch(cmd){
        case 0x01:
        case 0x02: 
            DelayMilis(2);
            DelayMicros(250);
            break;
        default:
            DelayMicros(53);
            break;
    }
}

void LCDInit(void)
{
    DelayMilis(15);

    LCDCmd(0x03);
    ENEable();

    DelayMilis(4); //delay 4.1 milisencond
    DelayMicros(100);

    LCDCmd(0x03);
    ENEable();
    DelayMicros(100); //delay 100 microsecond

    LCDCmd(0x03);
    ENEable();
    DelayMicros(100); //delay 100 microsecond

    LCDCmd(0x02);
    ENEable();
    DelayMicros(100); //delay 100 microsecond

    LCDCmd(0x28); // use control: 4 bit, 2 line, 5x8;
    LCDCmd(0x0F); // turn on display, setting, cusor blink
    LCDCmd(0x01); // clear lcd
    LCDCmd(0x06); // set entry mode
}

void SendData(int8_t data)
{
    HIGH_PIN(RS_PIN);
    DelayMicros(5);

    WriteNibble(data, 0);
    ENEable();
    WriteNibble(data, 1);
    ENEable();
}

Delay function: 延时功能:

Describe: 描述:

void DelayMicros(uint32_t timer): Delay micro second void DelayMicros(uint32_t timer):延迟微秒
void DelayMilis(uint32_t timer): Delay mili second void DelayMilis(uint32_t timer):延迟毫秒

void DelayMicros(uint32_t timer)
{
    uint32_t count = 0;

    while(timer--)
    {
        count = SystemCoreClock / 1000 / 1000 / 4;
        while(count--);
    }
}

void DelayMilis(uint32_t timer)
{
    uint32_t count = 0;

    while(timer--)
    {
        count = SystemCoreClock / 1000 / 4;
        while(count--);
    }
}

I searched the Internet but I couldn't find the reason. 我搜索了Internet,但找不到原因。 I am newbie. 我是新手。

Thank you! 谢谢! Have a nice day! 祝你今天愉快!

Did you check the contrast? 您检查对比度了吗?

Remember that the LCD 16x2 has a pin that adjusts the contrast (pin 3). 请记住,LCD 16x2的引脚可以调节对比度(引脚3)。 You should vary the voltage at this pin between 0 to Vcc until you see something in the LCD. 您应该在0到Vcc之间改变此引脚上的电压,直到在LCD上看到东西为止。

Othewise... 否则...

If this is not the case there are so many things that can go wrong when working on the classic 16x2 LCDs. 如果不是这种情况,那么在传统的16x2 LCD上工作时,可能会发生很多错误。 Bad contact in the pins, inadequate Vcc, bugs in the timing or the correct bit to set. 引脚接触不良,Vcc不足,时序错误或设置的正确位。 It is hard to tell without the board in the hands. 没有董事会就很难说。 I had to bang my head against the wall for days to get it working the first time, and it was not in the first attempt. 为了使它第一次工作,我不得不将头撞在墙上好几天,但这并不是第一次尝试。 I wish you good luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM