为什么此代码会清除 SparkFun 16x2 SerLCD

问题描述

我一直在使用 SparkFun 的 SparkFun 16x2 SerLCD LCD,并通过 Tiva C EK-TM4C123GXL 板控制它。我设法通过 SPI 通信连接 LCD,并编写了代码以在板上显示字符串。然而,我在编写代码时遇到了麻烦,直到我在网上看到这段代码:

#include <stdint.h>
#include <stdlib.h>
#include "inc/tm4c123gh6pm.h"

void spi_master_ini(void){ //Setup SPI
    SYSCTL_RCGCSSI_R|=(1<<2);
    //SYSCTL_RCGCGPIO_R |=(1<<1);
    SYSCTL_RCGC2_R |=(1<<1);
    GPIO_PORTB_AFSEL_R|=(1<<4)|(1<<5)|(1<<6)|(1<<7);
    GPIO_PORTB_PCTL_R=0x22220000;
    GPIO_PORTB_DEN_R|=(1<<4)|(1<<5)|(1<<6)|(1<<7);
    GPIO_PORTB_PUR_R|=(1<<4)|(1<<5)|(1<<6)|(1<<7);
    SSI2_CR1_R=0;
    SSI2_CC_R=0;
    SSI2_CR1_R=64;
    SSI2_CR0_R=0x7;
    SSI2_CR1_R|=(1<<1);
}

void send_byte(char data){
    SSI2_DR_R=data;
    while((SSI2_SR_R&(1<<0))==0);
}

void send_str(char *buffer){
  while(*buffer!=0){
  send_byte(*buffer);
        buffer++;
    }
}

int main(){
    spi_master_ini();
    SSI2_DR_R=0x7C; //Put into setting mode.
    SSI2_DR_R=0x2D; //Clear screen,move cursor to home position.
    send_str("Testing");
}

特别是让我困惑的两行代码:

SSI2_DR_R=0x7C; //Put into setting mode.
SSI2_DR_R=0x2D; //Clear screen,move cursor to home position.

在通读 HD44780U 数据表后,我无法看到将这些十六进制值发送到数据行除了打印“|”之外还有什么作用和“-”到液晶显示器。然而,令我惊讶的是,当我运行代码时,它可以工作并清除我的 LCD 屏幕。

解决方法

HD44780U 的数据表无关紧要 - 您没有直接与显示控制器对话。在 SparkFun 16x2 SerLCD 上,SPI 通信与 ATmega328P 通信,ATmega328P 又与显示控制器通信。

这简化了显示器的接口,因为您只需要一个 SPI 或 I2C 链接,不需要显示控制器所需的 4/8 位数据总线和额外的控制线。

在 ATMega328P 上运行的软件独立于显示控制器解释和翻译命令。 https://github.com/sparkfun/OpenLCD 处的源代码适用。

settings.h 具有:

#define SPECIAL_SETTING '|' //124,0x7C,the pipe character: The command to do special settings: baud,lines,width,backlight,splash,etc

然后在 OpenLCD.ino void updateDisplay() 中有:

    //Check to see if the incoming byte is special
    if (incoming == SPECIAL_SETTING) //SPECIAL_SETTING is 127
    {
      currentMode = MODE_SETTING;
    }
    ...

注意注释是错误的,它是 124 而不是 127(也许说明了这段代码的质量)。

然后:

  else if (currentMode == MODE_SETTING)
  {
    currentMode = MODE_NORMAL; //In general,return to normal mode

    ...

    //Clear screen and buffer
    else if (incoming == 45) //- character
    {
      SerLCD.clear();
      SerLCD.setCursor(0,0);

      clearFrameBuffer(); //Get rid of all characters in our buffer
    }
    ...

然后 clearFrameBuffer() 只是用空格填充缓冲区,而不是使用 HD44780“清除显示”指令:

//Flushes all characters from the frame buffer
void clearFrameBuffer()
{
  //Clear the frame buffer
  characterCount = 0;
  for (byte x = 0 ; x < (settingLCDwidth * settingLCDlines) ; x++)
    currentFrame[x] = ' ';
}

大部分文档都是针对 Arduino 的,但是 https://learn.sparkfun.com/tutorials/avr-based-serial-enabled-lcds-hookup-guide/firmware-overview 处的命令表无论如何都是有效的。它是字符而不是整数代码,所以有:

enter image description here ... enter image description here ...

还有更多您可能会觉得有用的命令。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...