简体   繁体   中英

Why does this code clear the screen on the SparkFun 16x2 SerLCD

I have been playing around with SparkFun 16x2 SerLCD LCD from SparkFun and controlling it via the Tiva C EK-TM4C123GXL board. I have managed to connect the LCD via SPI communication and written code to display strings on the board. However I was having trouble writing code that would clear the screen for me until I came across this code online:

#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");
}

Specifically the 2 lines of code that are puzzling me:

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

After reading through the HD44780U datasheet I wasn't able to see how sending those HEX values to the data lines would do anything other than print "|"and "-" to the LCD. However, to my surprise when I ran the code it works and clears my LCD screen.

The data sheet for the HD44780U is irrelevant - you are not talking directly to the display controller. On the SparkFun 16x2 SerLCD, the SPI communication is with the ATmega328P which in turn communicates with the display controller.

This simplifies the interface to the display, since you only need an SPI or I2C link and do not need a 4/8 bit data bus and additional control lines required by the display controller.

The software running on the ATMega328P interprets and translates commands independently of the display controller. The source code at https://github.com/sparkfun/OpenLCD applies.

settings.h has:

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

Then in OpenLCD.ino void updateDisplay() there is:

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

note the comment is erroneous, it is 124 not 127 (perhaps says something about the quality of this code).

Then later:

  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
    }
    ...

Then clearFrameBuffer() simply fills the buffer with spaces rather then using the HD44780 "Clear Display" instruction:

//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] = ' ';
}

Much of the documentaton is for Arduino, but the command table at https://learn.sparkfun.com/tutorials/avr-based-serial-enabled-lcds-hookup-guide/firmware-overview is valid regardless. It is interms of characters rather then integer codes so has:

在此处输入图像描述 ... 在此处输入图像描述 ...

And many more commands you might find useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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