LCD Display (SPI)

LCD Display (SPI)1. Learning ObjectivesSPI ProtocolSPI Hardware InterfaceSPI AdvantagesSPI Determinism2. Hardware Construction3. Experimental steps1. Open the SYSCONFIG configuration tool2. SPI parameter configuration3. Pin parameter configurationLCD screenExternal Flash4. Use of SPI protocol5. Write program6. Compile4. Program Analysis5. Experimental phenomenon

1. Learning Objectives

  1. Learn the basic knowledge of SPI communication.
  2. Read the data in the flash and display it on the onboard LCD screen.

SPI Protocol

The SPI protocol (Serial Peripheral Interface) is a commonly used synchronous serial communication protocol designed for high-speed, short-distance communication between microcontrollers and external devices. It supports full-duplex communication, that is, data can be sent and received between the master and slave devices at the same time, and is commonly used in devices such as memory chips, sensors, display drivers, and wireless modules.

SPI Hardware Interface

The SPI protocol usually uses four lines for data transmission:

SPI Advantages

SPI Determinism

2. Hardware Construction

W25Q32 is a common serial flash memory device that uses the SPI (Serial Peripheral Interface) interface protocol. It has high-speed read, write and erase functions and is widely used in high-performance electronic devices such as embedded systems, storage devices, routers, etc. The capacity of W25Q32 is 32 Mbit (ie 4 MB), and the numbers in the model represent different capacity options, such as W25Q16, W25Q64, W25Q128, etc., to meet the needs of different application scenarios.

The memory of the W25Q32 chip is allocated by sector (Sector) and block (Block). Specifically:

These storage unit structures make data management and storage operations more flexible, and are particularly suitable for embedded systems and storage applications that require frequent read and write operations.

We use the hardware SPI method to drive W25Q32, so we need to determine whether the pin we set has a hardware SPI peripheral interface. In the data sheet, PB14~PB17 can be multiplexed as 4 communication lines of SPI1.

image-20241129202635131

This course does not require additional hardware equipment, and can directly use the onboard LCD screen and external storage flash on the MSPM0G3507 motherboard.

MSPM0G3507 main control diagram:

image-20241118203957884

W25Q32 partial schematic diagram:

image-20241129202038392

LCD screen partial schematic diagram:

image-20241126145019204

3. Experimental steps

1. Open the SYSCONFIG configuration tool

Create a blank project empty in CCS.

image-20241126182939655

image-20241126183128934

Find and open the empty.syscfg file in the left workspace of CCS.

image-20241127152938881

2. SPI parameter configuration

Find the SPI column on the left, click to enter, and add SPI peripherals.

image-20241130082640872

image-20241130082953317

In most SPI protocols, the chip select is always pulled low during the entire timing when sending and receiving, and the chip select of the SPI peripheral will be pulled high after each frame is sent and received, so the CS chip select line needs to be independently controlled by the MCU's IO port, and there is no way to use the CS pin of the SPI peripheral. Here, the GPIO method (software method) is used to control the output of the CS pin.

image-20241130083026472

3. Pin parameter configuration

LCD screen

Backlight BLK:

image-20241126153008730

CS, DC, RES, MOSI, SCLK are as follows:

image-20241126153332062

image-20241126153404464

External Flash

CS1:

image-20241130083259274

4. Use of SPI protocol

We create a new folder under the project folder: BSP. Create four more files under the BSP folder, namely lcd.c, lcd.h, bsp_spi.c and bsp_spi.h.

Include the following LCD display drivers and fonts, and put them in the BSP folder

image-20241128210143217

Update the header file path and right-click the project folder.

image-20241128210532713

image-20241128210500585

5. Write program

Define the operation interface and related functions of the LCD display

lcd.h

Next is the LCD display driver

lcd.c (only part of it is captured here, please check the project source code for details)

bsp_spi.h

The initialization of SPI has been configured in SYSCONFIG, but we still need to prepare the SPI read and write steps. To ensure the success of sending and receiving data, when sending, you need to ensure that the data in the send buffer is sent, that is, the send buffer is empty, before the next data can be sent; when receiving, you need to ensure that there is data in the receive buffer before receiving.

bsp_spi.c (only part of it is intercepted here, please refer to the project source code for details)

Then write the following code in the empty.c file

6. Compile

image-20241127155238783

Once the compilation is successful, you can download the program to the development board.

4. Program Analysis

image-20241128210926030

The LCD_ShowPicture function draws the picture at the specified starting point (x, y), sets the display area according to the given length and width, and writes the color data of each pixel to the LCD by traversing the picture array to complete the picture display.

image-20241128211007947

The LCD_ShowString function is used to display a string at the specified coordinates (x, y), and calls the LCD_ShowChar function in sequence to draw each character in the string. At the same time, the character spacing is adjusted according to the font size. Both overlay mode and non-overlay mode display are supported.

image-20241128211055384

The LCD_ShowChinese function is used to display a Chinese character string at the specified coordinates (x, y). It draws each Chinese character by calling the LCD_ShowChinese16x16 function one by one and adjusts the display position of the next Chinese character according to the font size. It supports both overlay mode and non-overlay mode display.

image-20241130085215556

The W25Q32_read function is used to read data from the W25Q32 flash chip. The function first starts SPI communication by pulling the CS pin low, then sends the read command (0x03) and the high, middle, and low bytes of the read address. Next, the data is read byte by byte and stored in the provided buffer (buffer) according to the specified read length. Finally, the function restores the CS pin to a high level to end the data transfer. This process ensures that the specified length of data is read from the specified address.

image-20241130085328525

The W25Q32_write function is used to write data to the W25Q32 flash chip. First, the function erases the sector data at the target address. Then, it enables the write operation and checks if the chip is busy. Next, the function sends a write command (0x02) and a data address (24-bit address) to the chip through the SPI interface, and writes the data byte by byte according to the specified write length. Finally, the function waits for the write operation to complete and restores the CS pin to a high level to ensure that the data is written correctly and the chip is in an idle state.

image-20241128210813735

The main functions of this program are to initialize the development board, read and write data from the W25Q32 flash chip, and display the read content through the LCD. First, the program initializes the development board and related hardware. Then, it reads the chip ID of the W25Q32 and outputs it through the serial port. Next, the program reads 7 bytes of data from the starting address (address 0) of the flash memory to the buff array and outputs this data through the serial port. Next, it writes the string "Yahboom" to address 0 of the flash memory and reads and displays this data again. Finally, the program initializes the LCD screen, displays a welcome message and a picture, and displays the read buff data character by character on the same line on the screen.

5. Experimental phenomenon

After the program is downloaded, the text "Hello!", the data in the flash, and the logo of Yabo Smart will be displayed in lines on the LCD display.

image-20241126182011363