An example of OLED screen display based on SPI protocol

catalogue

  1, SPI protocol

1.1 SPI introduction

1.2 SPI four wire

one point three   SPI four working modes  

one point four   SPI sequence diagram  

2, Project realization

2.1 experimental preparation

2.2 font extraction

2.3 program code

2.4 hardware connection

2.5 achievement display

3, References

Content of this article:

Understand the principle of OLED screen display and Chinese character dot matrix coding, and use the SPI interface of STM32F103 to realize the following functions:

1) Display your student number and name;  

2) Display the temperature and humidity of AHT20;

3) Slide up and down or left and right to display long characters.

  1, SPI protocol

1.1 SPI introduction

The full name of SPI is serial peripheral interface, that is, serial peripheral interface. SPI is a synchronous serial interface technology launched by Motorola Company. It is a high-speed, full duplex synchronous communication bus. SPI clock frequency is much higher than I2C, and can work at hundreds of MHz at most. SPI works in a master-slave mode, usually with one master device and one or more slave devices. Generally, SPI requires four wires, but three wires (unidirectional transmission) can also be used


1.2 SPI four wire

① CS/SS, Slave Select/Chip Select, this is a chip select signal line, which is used to select the slave device that needs to communicate. The I2C host selects the slave device to communicate by sending the slave device address. The SPI host does not need to send the slave device, but directly pulls down the chip selection signal of the corresponding slave device.
② SCK, Serial Clock, like SCL of I2C, provides clock for SPI communication.
③ MOSI/SDO, Master Out Slave In/Serial Data Output, referred to as master out slave in signal line for short. This data line can only be used for sending data from the host to the slave, that is, master output and slave input.
④ MISO/SDI, Master In Slave Out/Serial Data Input, referred to as master in slave out signal line for short. This data line can only send data from the user slave to the host, that is, host input and slave output.
 

SPI communication is initiated by the host, and the host needs to provide communication clock signal. The structure of the host connecting multiple slave devices through SPI line is shown in the figure below:

one point three   SPI four working modes  

SPI has four working modes, which are obtained through the combination of serial clock polarity (CPOL) and phase (CPHA):

①   CPOL=0, the idle state of the serial clock is low.
②   CPOL=1. The idle state of the serial clock is high. At this time, the specific transmission protocol can be selected by configuring the clock phase (CPHA).
③   CPHA=0, the first jump edge (rising edge or falling edge) of the serial clock collects data.
④   CPHA=1, the second jump edge (rising edge or falling edge) of the serial clock collects data.
The four working modes are shown in the figure below:

one point four   SPI sequence diagram  

Taking the working mode of CPOL=0 and CPHA=0 as an example, the timing of SPI full duplex communication is shown in the figure below:

As can be seen from the above figure, the timing diagram of SPI is very simple. Unlike I2C, it is also divided into read timing and write timing. Because SPI is full duplex, the read and write timing can be completed together. In the figure, the CS chip selection signal is pulled down first, the slave device to be communicated is selected, and then the data is transmitted and received through the MOSI and MISO data lines. The MOSI data line sends 0XD2 data to the slave device, and the slave device also returns 0X66 data to the master device through the MISO line. This is the SPI sequence diagram.
 

2, Project realization

2.1 experimental preparation

Hardware:

  • STM32 development board
  • ATH20 temperature and humidity sensor
  • 0.96 inch OLED display
  • DuPont line

Software:

  • Keil5 MDK
  • Serial port debugging assistant

2.2 font extraction

The theoretical knowledge of text modeling can be referred to : Chinese character module

If you want to display English and numbers on OLED, you can output the display directly, but if you want to display Chinese, you must encode the Chinese into a dot matrix.

Font software download link:

Link: https://pan.baidu.com/s/1cGTVbVKIcFqRbBEhvSxhBQ  
Extraction code: cqjt

  Enter the text you want to extract in the input field below, and then click generate font to generate the corresponding dot matrix.

  Note: the reason why I want to rotate the text here is probably because the OLED screen is related to data transmission. It needs to rotate 90 ° to the left and then fold up and down to get the desired effect.

2.3 program code

All engineering documents of this experiment are linked as follows:

Link: https://pan.baidu.com/s/1_BvchbiatCR2rU_388N30g  
Extraction code: cqjt

There is an oledfont.h header file under gui.c. after opening, modify the contents of cfont16 [] array to the Chinese text dot matrix you need.

const typFNT_GB16 cfont16[] = 
{
  "Yuan",0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00,0x1F,0xF0,0x10,0x10,
	0x10,0x10,0x1F,0xF0,0x05,0x04,0x08,0x88,0x38,0x50,0xCA,0x20,0x0C,0x18,0x08,0x06,/*"Yuan ", 0*/

	"some",0x08,0x20,0x08,0x20,0xFF,0xFE,0x08,0x20,0x08,0x20,0x0F,0xE0,0x08,0x20,0x08,0x20,
	0x0F,0xE0,0x01,0x00,0xFF,0xFE,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00,/*""0"*/
	"people",0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x80,
  0x04,0x40,0x04,0x40,0x08,0x20,0x08,0x20,0x10,0x10,0x20,0x08,0x40,0x04,0x80,0x02,/*"Person ", 0*/
	
	"Joyous",0x00,0x80,0x00,0x80,0xFC,0x80,0x04,0xFC,0x05,0x04,0x49,0x08,0x2A,0x40,0x14,0x40,
	0x10,0x40,0x28,0xA0,0x24,0xA0,0x45,0x10,0x81,0x10,0x02,0x08,0x04,0x04,0x08,0x02,/*"Huan ", 0*/
  "Welcome",0x00,0x00,0x20,0x80,0x13,0x3C,0x12,0x24,0x02,0x24,0x02,0x24,0xF2,0x24,0x12,0x24,
	0x12,0x24,0x12,0xB4,0x13,0x28,0x12,0x20,0x10,0x20,0x28,0x20,0x47,0xFE,0x00,0x00,/*"Welcome ", 1*/
	"come",0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x11,0x10,0x09,0x10,0x09,0x20,
	0xFF,0xFE,0x03,0x80,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00,0x01,0x00,/*"Come on, "2*/
	"reach",0x00,0x04,0xFF,0x84,0x08,0x04,0x10,0x24,0x22,0x24,0x41,0x24,0xFF,0xA4,0x08,0xA4,
	0x08,0x24,0x08,0x24,0x7F,0x24,0x08,0x24,0x08,0x04,0x0F,0x84,0xF8,0x14,0x40,0x08,/*"To ", 3*/
	"heavy",0x00,0x10,0x00,0xF8,0x3F,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x1F,0xF0,0x11,0x10,
	0x1F,0xF0,0x11,0x10,0x1F,0xF0,0x01,0x00,0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00,/*"Weight ", 4*/
	"Celebrate",0x01,0x00,0x00,0x80,0x3F,0xFE,0x20,0x00,0x20,0x80,0x20,0x80,0x20,0x80,0x2F,0xFC,
	0x20,0x80,0x21,0x40,0x21,0x40,0x22,0x20,0x42,0x20,0x44,0x10,0x88,0x08,0x10,0x06,/*"Qing ", 5*/
	"hand over",0x02,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x10,0x10,0x10,0x08,0x20,0x24,
	0x48,0x24,0x04,0x40,0x02,0x80,0x01,0x00,0x02,0x80,0x0C,0x40,0x30,0x30,0xC0,0x0E,/*"Hand in ", 6*/
	"through",0x00,0x00,0x47,0xF8,0x20,0x10,0x21,0xA0,0x00,0x40,0x07,0xFC,0xE4,0x44,0x24,0x44,
	0x27,0xFC,0x24,0x44,0x24,0x44,0x27,0xFC,0x24,0x44,0x24,0x54,0x54,0x08,0x8F,0xFE,/*"Through ", 7*/
	"large",0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,
	0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06,/*"Large ", 8*/
	"learn",0x22,0x08,0x11,0x08,0x11,0x10,0x00,0x20,0x7F,0xFE,0x40,0x02,0x80,0x04,0x1F,0xE0,
	0x00,0x40,0x01,0x80,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,/*""Learning", 9*/
	"matter",0x10,0x80,0x10,0x80,0x50,0x80,0x50,0xFC,0x7D,0x54,0x52,0x54,0x90,0x54,0x10,0x94,
	0x1C,0x94,0xF1,0x24,0x52,0x24,0x10,0x44,0x10,0x44,0x10,0x84,0x11,0x28,0x10,0x10,/*"Object ", 10*/
	"Couplet",0x00,0x88,0xFC,0x48,0x48,0x50,0x48,0x00,0x79,0xFC,0x48,0x20,0x48,0x20,0x78,0x20,
	0x4B,0xFE,0x48,0x20,0x4C,0x50,0x78,0x50,0xC8,0x88,0x08,0x88,0x09,0x04,0x0A,0x02,/*"Couplet ", 11*/
	"network",0x00,0x00,0x7F,0xFC,0x40,0x04,0x40,0x04,0x42,0x14,0x52,0x94,0x4A,0x54,0x44,0x24,
	0x44,0x24,0x4A,0x54,0x4A,0x54,0x52,0x94,0x61,0x04,0x40,0x04,0x40,0x14,0x40,0x08,/*"Net ", 12*/
	"real",0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
	0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04,/*"Real ", 13*/
	"Training",0x01,0x04,0x21,0x24,0x11,0x24,0x11,0x24,0x01,0x24,0x01,0x24,0xF1,0x24,0x11,0x24,
	0x11,0x24,0x11,0x24,0x11,0x24,0x15,0x24,0x19,0x24,0x12,0x24,0x02,0x04,0x04,0x04,/*"Training ", 14*/
	"room",0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,
	0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,/*"Room ", 15*/

	"temperature",0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
  0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,/*"Temperature ", 0*/
	"degree",0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
  0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,/*"Degrees ", 0*/
	"wet",0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
  0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,/*"Wet ", 0*/
	": ",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,/*": ",0*/
	"℃",0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
  0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00,/*"℃",0*/
	"%",0x00,0x00,0x18,0x04,0x24,0x08,0x24,0x10,0x24,0x20,0x24,0x40,0x24,0x80,0x19,0x00,
  0x02,0x60,0x04,0x90,0x08,0x90,0x10,0x90,0x20,0x90,0x40,0x90,0x00,0x60,0x00,0x00,/*"%",0*/
};

The Chinese characters I added here are: yuan, Mou, Ren, Huan, Ying, Lai, Lai, Chong, Qing, Jiao, Tong, Da, Xue, Wu, Lian, Wang, Shi, Xun, room, temperature, degree, humidity,:, ℃,%

Then set the content you want to display at the end of the test.c file.

void TEST_ShowMyName(void)
{
	GUI_ShowCHinese(0,0,16,"chongqing jiaotong university ",1);
	GUI_ShowString(0,16,"631907031",16,1);
	GUI_ShowCHinese(80,16,16,"Yuan sb",1);
}

Function Description:

GUI_ Parameters for showchinese()
Parameter 1: X coordinate
Parameter 2: Y coordinate
Parameter 3: Chinese character dot matrix size (16 used here) × 16, the parameter should be 16)
Parameter 4: Chinese characters to be displayed
Parameter 5: display style (1: white and black background; 0: black and white background)

GUI_ Parameters of showstring()
Parameter 1: X coordinate
Parameter 2: Y coordinate
Parameter 3: string (in Classl code)
Parameter 4: bit (indicates the character display format. Here I use 16, which is as high as Chinese characters)
Parameter 5: display style (1: white and black background; 0: black and white background)

2.4 hardware connection

The OLED screen can be connected according to the connection method of the code main function annotation:

The connection of ATH20 temperature and humidity sensor is as follows:

 VCC→3V3,GND→GND,SCL→PB6,SDA→PB7

2.5 achievement display

Holding AHT20 chip in one hand, you can obviously see the rise of temperature. The reason for the peak humidity may be the sweat on your hands.

3, References

SPI protocol details_ A green wood - CSDN blog_ SPI protocol details SPI protocol details 1. SPI introduction 2. SPI four wire 3. SPI four working modes 4. SPI timing chart 1. SPI introduction the full name of SPI is serial peripheral interface, that is, serial peripheral interface. SPI is a synchronous serial interface technology launched by Motorola Company. It is a high-speed, full duplex synchronous communication bus. SPI clock frequency is much higher than I2C, and can work at hundreds of MHz at most. SPI works in the master-slave mode, usually with one master device and one or more slave devices. Generally, SPI requires 4 lines, but three lines (unidirectional transmission) 2 and four SPI lines can also be usedhttps://blog.csdn.net/weixin_45309916/article/details/108713843?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163741536916780271972904%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163741536916780271972904&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-108713843.first_rank_v2_pc_rank_v29&utm_term=spi%E5%8D%8F%E8%AE%AE%E8%AF%A6%E8%A7%A3&spm=1018.2226.3001.4187

Keywords: Single-Chip Microcomputer stm32 ARM

Added by dsdsdsdsd on Sun, 21 Nov 2021 05:12:04 +0200