Embedded system development 15 - simple application of OLED screen display based on SPI protocol

This article mainly introduces the principle of OLED screen display and Chinese character dot matrix coding, and on this basis, uses the SPI interface of STM32F103, AHT20 temperature sensor and OLED screen display to realize the display of student number name, display temperature and humidity, and long character sliding display.

1. Task requirements

Understand the principle of OLED screen display and Chinese character dot matrix coding, and use the SPI or IIC interface of STM32F103 to realize the following functions:
(1) Display your student number and name;
(2) Display the temperature and humidity of AHT20;
(3) Up and down or left and right Swipe to display long characters, such as "Hello, welcome to Chongqing Jiaotong University Internet of Things 205 training room!" or a piece of lyrics or poetry (it is best to use the hardware refresh mode)

2. OLED screen display

2.1 Introduction to OLED display

OLED is Organic Light-Emitting Diode (Organic Light-Emitting Diode) , also known as Organic Electroluminesence Display (OELD). OLED is considered to be the most advanced technology due to its self-illumination, no need for backlight, high contrast, thin thickness, wide viewing angle, fast response speed, flexible panel, wide operating temperature range, and simple structure and manufacturing process. Next-generation flat-panel display emerging application technology.

LCDs all need a backlight, while OLEDs don't because it's self-illuminating. For the same display, the OLED effect is better. With the current technology, it is still difficult to enlarge the size of OLED, but the resolution can indeed be very high.

We use ALINETEK's OLED display module, which has the following characteristics:

  • The modules are available in single-color and double-color, the single-color is pure blue, and the double-color is yellow-blue.
  • The size is small, the display size is 0.96 inches, and the module size is only 27mmx26mm.
  • High resolution, the module has a resolution of 128x64.
  • A variety of interface methods, the module provides a total of 5 interfaces including: 6800, 8080 two parallel interface methods, 3-wire or 4-wire through SPI interface, IIC interface (only 2 wires are needed to control the OLED) .
  • It doesn't need high voltage, it can work by connecting directly to 3.3V.

Note: This module is not compatible with 5.0V interface, do not connect it directly to 5V when using it, otherwise it may burn out the module
insert image description here

2.2. OLED Classification

Classification from the structure of the device:
1. Single-layer device
A single-layer device is to insert a layer of organic layer that can emit light between the positive and negative electrodes of the device, and its structure is substrate/ITO/light-emitting layer/cathode. In this structure, due to the unbalanced injection and transport of electrons and holes, the efficiency and brightness of the device are low, and the stability of the device is poor
.
Adding a hole transport layer (HTL) or an electron transport layer (ETL) overcomes the problem of unbalanced carrier injection in single-layer devices, improves the voltage-current characteristics of the device, and improves the luminous efficiency of the device. 3. Three-layer
devices
The three-layer device structure is the most widely used structure, and its structure is substrate/ITO/HTL/light-emitting layer/ETL/cathode. The advantage of this structure is that the excitons are confined in the light-emitting layer, thereby improving the efficiency of the device.
4. Multi-layer
structure The performance of the multi-layer structure is a relatively good structure, which can play the role of each layer well. The light-emitting layer can also be composed of a multi-layer structure, and since each light-emitting layer is independent of each other, it can be optimized separately. Therefore, this structure can give full play to the role of each organic layer, which greatly improves the flexibility of device design.

Classification from the driving mode:
1. One is active and the other is passive.
2. The active type is generally active drive, and the passive type is passive drive. In the actual application process, active drivers are mainly used for high-resolution products, while passive drivers are mainly used in displays with relatively small display sizes.

Classification from materials:
1. It can be divided according to the type of organic matter, one is small molecules, and the other is polymers.
2. The main difference between these two devices is in the manufacturing process. Small molecule devices mainly use vacuum thermal Evaporation process, polymer devices use spin coating or spray printing process

2.3 Advantages of OLED

  • 1. Compared with the crystal layer of LED or LCD, the organic plastic layer of OLED is thinner, lighter and more flexible
  • 2. The light-emitting layer of OLED is relatively light: so its base layer can use flexible materials instead of rigid materials. OLED substrates are made of plastic, while LEDs and LCDs use glass substrates
  • 3. OLED is brighter than LED: The organic layer of OLED is much thinner than the corresponding inorganic crystal layer in LED, so the conductive layer and emission layer of OLED can adopt multi-layer structure. In addition, LEDs and LCDs require glass as a support, and glass absorbs some of the light. OLED does not need to use glass
  • 4. OLED does not need to use the backlight system in LCD: LCD will selectively block certain backlight areas when it is working, so that the image can be displayed, while OLED emits light by itself. Because OLEDs don't require a backlight system, they consume less power than LCDs (most of the power consumed by LCDs goes to the backlight system). This is especially important for battery-operated devices such as mobile phones
  • 5. OLEDs are easier to manufacture and can be made into larger sizes: OLEDs are made of plastic, so they can be made into large sheets, but if you want to use so many crystals and lay them out, you need much more difficult
  • 6. The field of view of OLED is very wide, up to about 170 degrees; while LCD needs to block light when working, so there are natural observation obstacles at certain angles. OLED itself can emit light, so the field of view is much wider

2.4. Characteristics of OLED display

The reason why OLED technology can be widely used is that it has the following advantages compared with other technologies:

  • Low power consumption
  • quick response
  • wide viewing angle
  • Capable of high resolution display
  • wide temperature characteristics
  • OLED can realize soft screen
  • The quality of OLED finished products is relatively light

2.5, circuit diagram and connection method

Circuit diagram:
insert image description here

In the following experiments,
the demo program given by the seven-line OLED reference manufacturer will be used: 0.96-inch SPI_OLED module supporting information package
Baidu network disk sharing:
link: https://pan.baidu.com/s/1kRSY2vH5Ycm527dE5tnvGw?pwd=2022
Withdrawal code: 2022

Pin introduction:
insert image description here

For the introduction of 0.96-inch OLED display, please refer to the link:
http://www.lcdwiki.com/zh/0.96inch_SPI_OLED_Module

3. Introduction to Chinese character dot matrix

3.1. Concept of dot matrix

Using the previous method, one IO port can only control one LED. What if we need to use fewer IO ports to control more LEDs? So, there is a dot matrix.
For example: 8X8 dot matrix is ​​composed of 64 light-emitting diodes, and each light-emitting diode is placed at the intersection of row lines and column lines. When a corresponding row is set to 1 level and a certain column is set to 0 level, the corresponding The diode will light up; if you want to light up the first point, connect pin 1 to high level and pin a to low level, then the first point will light up.
insert image description here

Physical map
insert image description here

With the help of modeling software, the text or letters we need can be presented in the form of dot matrix.
insert image description here

We know that the number of English letters is relatively small, and we only need to use one byte (8 bits) to express. But there are a lot of Chinese characters. How to express it?

One method adopted by the predecessors is to use the high 128 bits of the ASCII code as the internal code of Chinese characters, and the low 128 bits as the internal code of English letters, and then use two bytes to represent a Chinese character. Through this internal code, we can obtain the font information of Chinese characters. Then, according to the information of these fonts, the corresponding Chinese characters are displayed.

3.2, Chinese character encoding

Location code
The dot matrix font library actually stores the font information of Chinese characters according to the order of the internal codes of Chinese characters. The 16×16 dot matrix font library has 94 areas, and each area has 94 Chinese character fonts. There are 94*94 Chinese characters in total like this.

According to the national standard GD2312-80, all national standard Chinese characters and symbols are allocated in a square matrix with 94 rows and 94 columns. Each row of the square matrix is ​​called a "zone", numbered from 01 to 94. It is a "bit", numbered from 01 to 94, and the four Arabic numerals formed by combining the area code and bit number of each Chinese character and symbol in the square matrix are their "area code".

The first two digits of the area code are its area code, and the last two digits are its bit number. A Chinese character or symbol can be uniquely determined with the location code.

Machine code
The machine code of Chinese characters refers to the code that represents a Chinese character in the computer. There is a slight difference between the internal code and the area code.

The value of the area code and the bit code of the Chinese character area code are both between 1-94, if the area code is directly used as the internal code of the machine, it will be confused with the basic ASCII code. In order to avoid the conflict between the internal code and the basic ASCII code, it is necessary to avoid the control code (00H~1FH) in the basic ASCII code, and to distinguish it from the characters in the basic ASCII code.

Therefore: first add 20H to the area code and bit code, and then add 80H on this basis.
(here "H" indicates that the first two digits are hexadecimal numbers)

After these processes, it takes two bytes to express a Chinese character with the internal code, which are called high byte and low byte respectively. The internal code of these two bytes is expressed according to the following rules:

高位字节 = 区码 + 20H + 80H(或区码 + A0H)
低位字节 = 位码 + 20H + 80H(或位码 + AOH)

Since the area code and bit code of Chinese characters range in hexadecimal from 01H-5EH (that is, 01-94 in decimal), the value range of high-order bytes and low-order bytes of Chinese characters is A1H- FEH (that is, 161-254 in decimal).

For example, the area code of the Chinese character "ah" is 1601, and the area code and the bit code are respectively expressed in hexadecimal as 1001H. The high-order byte of its internal code is B0H, and the low-order byte is A1H. The internal code is B0A1H.

3.3. Dot matrix font storage

In the dot matrix font library of Chinese characters, each bit of each byte represents a dot of a Chinese character, each Chinese character is composed of a rectangular dot matrix, 0 means no, 1 means dot, and 0 and 1 are respectively Draw in different colors to form a Chinese character.

The font library is divided into horizontal matrix and vertical matrix according to the different points represented by the bytes. At present, most of the fonts are stored in the horizontal matrix (the most used should be the early UCDOS font), and the vertical matrix is ​​generally because there are some liquid crystals. It adopts the vertical scanning display method. In order to improve the display speed, the font matrix is ​​made vertical.

What is described later refers to the horizontal matrix font.


For a 1616 matrix, the 16*16 dot matrix font library requires a total of 1616=256 bits, and each character is 8 bits. Therefore, each Chinese character needs to use 256/8=32 bytes To represent.

Every two bytes represent 16 points of a line, and a total of 16 lines are needed. When displaying Chinese characters, it only needs to read 32 bytes at a time and print out every two bytes as a line to form a Chinese character.
insert image description here

1414 and 1212 dot matrix fonts
For the 1414 and 1212 fonts, theoretically, the dot matrix required by them is (1414/8)=25, (1212/8)=18 bytes, but if according to this Therefore, when the dot matrix is ​​taken and displayed, since each line is not an integer number of 8 , it will involve the calculation and processing of the dot matrix, which will increase the complexity of the program and reduce the efficiency of the program.

To solve this problem, some dot matrix fonts store 1414 and 1212 fonts as 1614 and 1612, that is, each line is still stored as two bytes, but for 14 14 fonts , the last two bytes of each two bytes Bits are not used, 12 12 bytes, the last 4 bits of every two bytes are not used , this will be handled differently according to different fonts, so pay attention to this problem when using fonts.

3.4. Obtain Chinese characters by using the internal code of the Chinese character machine

The relationship between the location code of Chinese characters and the internal code is as follows:

High byte of internal code = area code + 20H + 80H (or area code + A0H)
low byte of internal code = bit code + 20H + 80H (or bit code + AOH)

Conversely, we can also obtain the area code according to the internal code:

Area code = high byte of internal code - A0H
bit code = low byte of internal code - AOH

In this way, after calculating the location code, we can use it to address and find fonts in the Chinese character library. Specific ways:
该汉字的偏移地址 =(区码-1)×94×一个字占用的字节数 + 位码×一个字占用的字节数

4. SPI communication protocol

4.1, SPI communication protocol

SPI is the abbreviation of English Serial Peripheral interface, as the name implies, it is a serial peripheral device interface. Motorola (Motorola) first defined it on its MC68HCXX series processors.

SPI is a high-speed , full-duplex, synchronous communication bus, and it only occupies four wires on the pins of the chip, which saves the pins of the chip, and at the same time saves space for the PCB layout and provides convenience. It is used between EEPROM, FLASH, real-time clock, AD converter, and digital signal processor and digital signal decoder.

SPI is a synchronous data bus, which means it uses a separate data line and a separate clock signal to ensure perfect synchronization between the sender and receiver.

A clock is an oscillating signal that tells the receiving end when to sample the signal on the data line.

The side that generates the clock is called the master and the other side is called the slave . There is always only one master (which can be a microcontroller ), but there can be multiple slaves (more on that later);

The timing of data collection may be the rising edge (from low to high) or falling edge (from high to low) of the clock signal, depending on the configuration of the SPI.

4.2. Transmission process

insert image description here

The overall transmission can be roughly divided into the following processes:

  • The host first pulls down the NSS signal to ensure that it starts to receive data;

  • When the receiving end detects the edge signal of the clock, it will immediately read the signal on the data line, thus obtaining one bit of data (1bit);

Since the clock is sent with the data, it is not important to specify how fast the data is transferred , although the device will be at the highest speed it can run at.

  • When the master sends to the slave: the master generates a corresponding clock signal, and then the data is sent to the slave from the MOSI signal line one by one;

The master receives slave data: If the slave needs to send data back to the master, the master will continue to generate a predetermined number of clock signals, and the slave will send the data through the MISO signal line;
insert image description here

Note that SPI is "full-duplex" (with separate transmit and receive lines), so data can be sent and received at the same time, and the receive hardware for SPI can be a simple shift register. This is much simpler and less expensive than the full UART required for asynchronous serial communication;

4.3, SPI characteristics

The SPI bus includes 4 logic lines, which are defined as follows:

  • MISO: Master input slave output master input, slave output (data comes from the slave);

  • MOSI: Master output slave input master output, slave input (data from the host);

  • SCLK: Serial Clock serial clock signal, generated by the master and sent to the slave;

  • SS: Slave Select chip select signal, sent by the host to control which slave to communicate with, usually a low-level active signal.

Other manufacturers may follow other naming conventions, but ultimately they refer to the same thing. Below are some commonly used terms;

  • MISO can also be SIMO, DOUT, DO, SDO or SO (on the host side)

  • MOSI can also be SOMI, DIN, DI, SDI or SI (on the host side)

  • NSS can also be CE, CS or SSEL

  • SCLK can also be SCK

The diagram below shows a typical SPI connection between a single master and a single slave.
insert image description here

4.4. Clock

Clock Frequency
The master on the SPI bus must be configured and generate the corresponding clock signal at the beginning of the communication. During every SPI clock cycle, a full-duplex .

The master sends a bit of data on the MOSI line and the slave reads it, while the slave sends a bit of data on the MISO line and the master reads it.

Even if only one-way data transmission is performed, this order must be maintained. This means that whatever data is received, something must actually be sent! In this case, we call it dummy data;

Theoretically, the clock rate can be any rate you want as long as it is practical, of course, this rate is limited by how much system clock frequency each system can provide, and the maximum SPI transfer rate.

Clock Polarity CKP/Clock Polarity
In addition to configuring the serial clock rate (frequency), the SPI master also needs to configure the clock polarity .

Depending on the naming rules of the hardware manufacturer, the clock polarity is usually written as CKP or CPOL . The clock polarity and phase together determine the way to read data, such as reading data on the rising edge of the signal or reading data on the falling edge of the signal;

CKP can be configured as 1 or 0. This means you can set the clock's default state (IDLE) to be high or low according to your needs. Polarity reversal can be achieved with a simple logic inverter. You must refer to the device's data sheet to properly set CKP and CKE .

  • CKP = 0: clock idle IDLE is low 0
  • CKP = 1: clock idle IDLE is high 1

Clock Phase CKE /Clock Phase (Edge)
In addition to configuring the serial clock rate and polarity, the SPI master should also configure the clock phase (or edge). Depending on the hardware manufacturer, the clock phase is usually written as CKE or CPHA ;

As the name implies, the clock phase/edge, that is, the specific phase or edge of the clock signal when collecting data;

  • CKE = 0: Sampling on the first transition edge of the clock signal SCK ;
  • CKE = 1: Sampling on the second transition edge of the clock signal SCK ;

Clock configuration summary
To sum up the above situations, the following figure summarizes all clock configuration combinations and highlights the actual sampling data moment;

The black line is the moment of sampling data;
the blue line is the SCK clock signal;

The details are shown in the figure below;
insert image description here

4.5, SPI mode

Mode number
The configuration of the clock polarity and phase of SPI is usually called SPI mode, and all possible modes follow the following conventions; the details are shown in the following table:

SPI Mode CPOL WASH
0 [00] 0 0
1 [01] 0 1
2 [10] 1 0
3 [11] 1 1

The four modes of SPI are simply to set the signal of the SCLK clock signal line as a valid signal

There are 4 different operation modes for SPI communication . Different slave devices may be configured in a certain mode at the factory, which cannot be changed; but our communication parties must work in the same mode, so we can configure our Configure the SPI mode of the master device, and
control the communication mode of our master device through CPOL (clock polarity) and CPHA (clock phase), as follows:

Clock polarity (CPOL) defines the clock idle state level:

  • CPOL=0, means that it is idle when SCLK=0, so the effective state is when SCLK is at high level
  • CPOL=1 means that it is idle when SCLK=1, so the active state is when SCLK is at low level

The clock phase (CPHA) defines the acquisition time of the data:

  • CPHA=0, data sampling is performed on the first jump edge (rising edge or falling edge) of the clock. , send data on the second edge
  • CPHA=1, data sampling is performed on the second jump edge (rising edge or falling edge) of the clock. , send data on the first edge

Mode0:CPOL=0,CPHA=0:At this time, in the idle state, SCLK is at low level, and the data sampling is on the first edge, that is, the transition of SCLK from low level to high level, so the data sampling is on the rising edge (preparing data), (sending Data) Data transmission is on the falling edge.

Mode1:CPOL=0,CPHA=1:At this time, in the idle state, SCLK is at low level, and data transmission is on the first edge, that is, the transition of SCLK from low level to high level, so data sampling is on the falling edge, and data transmission is on the rising edge .

Mode2:CPOL=1,CPHA=0:At this time, in the idle state, SCLK is at high level, and data acquisition is on the first edge, that is, the transition of SCLK from high level to low level, so data acquisition is on the falling edge, and data transmission is on the rising edge .

Mode3:CPOL=1,CPHA=1:At this time, in the idle state, SCLK is at high level, and data transmission is on the first edge, that is, the transition of SCLK from high level to low level, so data acquisition is on the rising edge, and data transmission is on the falling edge .

4.6. Multi-slave mode

As mentioned earlier, the SPI bus must have a master, and there can be multiple slaves, so there are two specific methods for connecting to the SPI bus:

The first method: multiple NSS

  1. Typically, each slave requires a separate SS line.
    If you want to communicate with a specific slave, you can pull down the corresponding NSS signal line and keep the state of other NSS signal lines at high level;
  2. If the two NSS signal lines are pulled low at the same time, garbled characters may appear, because the slaves may both try to transmit data on the same MISO line, resulting in garbled received data.

The specific connection method is shown in the figure below;
insert image description here

The second method: daisy chain
In the world of digital communication, the way in which device signals (bus signals or interrupt signals) are transmitted from one device to the next in a serial manner, and the loop continues until the data reaches the target device is called daisy chain .

  1. The biggest disadvantage of the daisy chain is that because the signal is transmitted serially, once a device in the data link fails, the lower priority device below it cannot be served;

  2. On the other hand, the farther the slave is from the master, the lower the service priority is, so it is necessary to arrange the priority of the slave and set the bus detector. If a slave times out, short-circuit the slave. , to prevent the damage of a single slave from causing the entire link to collapse;

The specific connection is shown in the figure below;
insert image description here

The red line is the flow of data

So the final data flow graph can be expressed as:
insert image description here

SCK is the clock signal, 8clks means 8 edge signals;
where D is data, X is invalid data;

So it is not difficult to find that the daisy chain mode makes full use of the function of SPI's shift register, the whole chain acts as a communication shift register, and each slave copies the input data to the output in the next clock cycle.

4.7 Advantages and disadvantages

Advantages of SPI Communication

  • Full-duplex serial communication;
  • High data transfer rate.
  • Simple software configuration;
  • Extremely flexible data transmission, not limited to 8 bits, it can be any size word;
  • Very simple hardware structure. A slave does not need a unique address (unlike I2C). The slave uses the master clock and does not require a precision clock oscillator/crystal (unlike UART). No transceiver required (unlike CAN).

Disadvantages of SPI

  • No hardware slave acknowledge signal (the master may have nowhere to send without knowing);
  • Usually only one master device is supported;
  • Requires more pins (unlike I2C);
  • There is no error checking protocol defined at the hardware level;
  • Compared with RS-232 and CAN bus, it can only support very short distance;

4.8. Comparison with UART

Because the UART does not have a clock signal, it cannot control when to send data, nor can it guarantee that the two transmissions will receive data at exactly the same speed. Therefore, the two sides receive and send data at different speeds, and problems arise.

If you want to solve this problem, the UART adds extra start and stop bits to each byte to help the receiver synchronize when the data arrives;

Both parties must also reach a consensus on the transmission (set the same baud rate, such as 9600 bits per second).

Small differences in transfer rates are not a problem because the receiver resynchronizes at the beginning of each byte. The corresponding protocol is shown in the figure below;
insert image description here

In the above picture 11001010is not equal to 0x53, this is a detail. Serial protocols usually send the least significant bit first, so the least significant bit is the leftmost LSB . The lower nibble actually is 0011 = 0x3, the upper nibble is 0101 = 0x5.

Asynchronous serial works very well, but there is a lot of overhead in the extra start and stop bits that are sent with each byte and the complex hardware required to send and receive data.

It is not difficult to find that if the speeds set by the receiving end and the sending end are inconsistent, the received data will be garbled.

5. Display name and student number

5.1.

  • Use the modulo software to express the text to be displayed in hexadecimal
    Link: https://pan.baidu.com/s/1YbUiGlyfxHi_h2B_rqogBg?pwd=2022
    Extraction code: 2022

  • Open the software for initial setup
    insert image description here

  • Click Parameter Settings -> Other Options , set the modulus method to horizontal modulus , and do not check byte reverse order
    insert image description here

  • Enter the target text in the text input area, and ctrl+enter to get the displayed image
    insert image description here

  • Click the C51 format to generate a dot matrix
    insert image description here

5.2. Code writing

  • Unzip the file that starts the download and open the file
    insert image description here

  • Open 1-Demo->Demo_STM32->0.96inch_OLED_Demo_STM32F103RCT6_Software_4-wire_SPI->PROJECT
    insert image description here
    insert image description here
    insert image description here
    insert image description here

  • Double click to open the project
    insert image description here

  • Modify the TEST_MainPage function under test.c

void TEST_MainPage(void)
{
    
    	
	GUI_ShowCHinese(28,20,16,"许文杰",1);       //中文姓名
	GUI_ShowString(4,48,"632007030615",16,1);  //数字详细
	delay_ms(1500);		
	delay_ms(1500);
}

insert image description here

  • Find the cfont16 array in the oledfont.h file in the project under USER- >gui.c , and add the dot matrix code output by the font extractor in it
const typFNT_GB16 cfont16[] = 
{
    
    
	"许",0x00,0x80,0x20,0x80,0x10,0x80,0x11,0xFC,0x01,0x20,0x02,0x20,0xF0,0x20,0x10,0x20,
0x13,0xFE,0x10,0x20,0x10,0x20,0x10,0x20,0x14,0x20,0x18,0x20,0x10,0x20,0x00,0x20,
	"文",0x02,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x10,0x10,0x10,0x10,0x08,0x20,0x08,0x20,
0x04,0x40,0x02,0x80,0x01,0x00,0x02,0x80,0x04,0x40,0x08,0x20,0x30,0x18,0xC0,0x06,
	"杰",0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFC,0x03,0x80,0x05,0x40,0x09,0x20,0x11,0x10,
0x21,0x08,0xC1,0x06,0x01,0x00,0x00,0x00,0x24,0x88,0x22,0x44,0x42,0x44,0x80,0x04,
};

insert image description here

  • Modify the main function of main.c
int main(void)
{
    
    	
	delay_init();	    	       //延时函数初始化	  
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0);             //清屏(全黑)
	while(1) 
	{
    
    	
		TEST_MainPage();         //主界面显示测试
	}
}

insert image description here

5.3. Hardware connection

Connection method of power supply module:

USB to TTL STM32F103C8T6
GND G
3V3 3V3
RXD PA9
TXD PA10

Note that BOOT0 on the core board is set to 1, and BOOT1 is set to 0

insert image description here
insert image description here

Screen connection:
insert image description here
insert image description here

5.4, ​​compile and burn

  • compile
    insert image description here

  • burning
    insert image description here

5.5. Achievement effect

insert image description here

5.6, complete code

Baidu Netdisk:
Link: https://pan.baidu.com/s/1-bktJppbVh54TvyERdEg0A?pwd=2022
Extraction code: 2022

gitee warehouse address:
link https://gitee.com/xuwenjie10086/qrs/tree/master/OLED%E5%B1%8F%E5%B9%95%E6%98%BE%E7%A4%BA%E5%90 %8D%E5%AD%97%E5%AD%A6%E5%8F%B7

6. Display temperature and humidity

6.1.

  • Enter temperature , press ctrl+enter , click C51 format to take the modulus, and get the bitmap
    insert image description here

6.2. Code writing

  • Find oledfont.h in the project under USER->gui.c , and add the required text dot matrix in cfont16[ ]
const typFNT_GB16 cfont16[] = 
{
    
    
  "温",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,
	"湿",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,
	"度",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,
	"显",0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x04,0x40,0x44,0x44,0x24,0x44,0x14,0x48,0x14,0x50,0x04,0x40,0xFF,0xFE,0x00,0x00,
	"示",0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x01,0x00,
0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x41,0x02,0x81,0x02,0x05,0x00,0x02,0x00,
};

insert image description here

  • Rewrite the function read_AHT20 in bsp_i2c.c
void read_AHT20(void)
{
    
    
	uint8_t   i;
	for(i=0; i<6; i++)
	{
    
    
		readByte[i]=0;
	}

	//-------------
	I2C_Start();

	I2C_WriteByte(0x71);
	ack_status = Receive_ACK();
	readByte[0]= I2C_ReadByte();
	Send_ACK();

	readByte[1]= I2C_ReadByte();
	Send_ACK();

	readByte[2]= I2C_ReadByte();
	Send_ACK();

	readByte[3]= I2C_ReadByte();
	Send_ACK();

	readByte[4]= I2C_ReadByte();
	Send_ACK();

	readByte[5]= I2C_ReadByte();
	SendNot_Ack();
	//Send_ACK();

	I2C_Stop();

	//--------------
	if( (readByte[0] & 0x68) == 0x08 )
	{
    
    
		H1 = readByte[1];
		H1 = (H1<<8) | readByte[2];
		H1 = (H1<<8) | readByte[3];
		H1 = H1>>4;

		H1 = (H1*1000)/1024/1024;

		T1 = readByte[3];
		T1 = T1 & 0x0000000F;
		T1 = (T1<<8) | readByte[4];
		T1 = (T1<<8) | readByte[5];

		T1 = (T1*2000)/1024/1024 - 500;

		AHT20_OutData[0] = (H1>>8) & 0x000000FF;
		AHT20_OutData[1] = H1 & 0x000000FF;

		AHT20_OutData[2] = (T1>>8) & 0x000000FF;
		AHT20_OutData[3] = T1 & 0x000000FF;
	}
	else
	{
    
    
		AHT20_OutData[0] = 0xFF;
		AHT20_OutData[1] = 0xFF;

		AHT20_OutData[2] = 0xFF;
		AHT20_OutData[3] = 0xFF;
		printf("lyy");

	}
	t=T1/10;
	t1=T1%10;
	a=(float)(t+t1*0.1);
	h=H1/10;
	h1=H1%10;
	b=(float)(h+h1*0.1);
	sprintf(strTemp,"%.1f",a);   //调用Sprintf函数把DHT11的温度数据格式化到字符串数组变量strTemp中  
    sprintf(strHumi,"%.1f",b);    //调用Sprintf函数把DHT11的湿度数据格式化到字符串数组变量strHumi中  
	GUI_ShowCHinese(16,00,16,"温湿度显示",1);
	GUI_ShowCHinese(16,20,16,"温度",1);
	GUI_ShowString(53,20,strTemp,16,1);
	GUI_ShowCHinese(16,38,16,"湿度",1);
	GUI_ShowString(53,38,strHumi,16,1);
	delay_ms(1500);		
	delay_ms(1500);
	
}

insert image description here

  • Rewrite the main function of main.c
int main(void)
{
    
    	
	delay_init();	    	       //延时函数初始化    	  
	uart_init(115200);	 
	IIC_Init();
		  
	NVIC_Configuration(); 	   //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 	
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0); 
	while(1)
	{
    
    
		//printf("温度湿度显示");
		read_AHT20_once();
		OLED_Clear(0); 
		delay_ms(1500);
  }
}

insert image description here

6.3. Hardware connection

Connection method of power supply module:

USB to TTL STM32F103C8T6
GND G
3V3 3V3
RXD PA9
TXD PA10

Note that BOOT0 on the core board is set to 1, and BOOT1 is set to 0

insert image description here
insert image description here

AHT20 sensor module connection method:

STM32F103C8T6 AHT20 sensor
3.3V Pin 1 (VDD)
PB7 Pin 2 (SDA)
GND Pin 3 (GND)
PB6 Pin 4 (SCL)

insert image description here
insert image description here

Screen connection:
insert image description here
insert image description here

overall effect:
insert image description here

6.4, compile and burn

  • compile
    insert image description here

  • burning
    insert image description here

6.5. Realize the effect

insert image description here

6.6, complete code

Baidu Netdisk:
Link: https://pan.baidu.com/s/1cAklk_5GPxIzaUmWQ4dxrA?pwd=2022
Extraction code: 2022

gitee warehouse address:
link: https://gitee.com/xuwenjie10086/qrs/tree/master/OLED%E5%B1%8F%E5%B9%95%E6%98%BE%E7%A4%BA%E6% B8%A9%E6%B9%BF%E5%BA%A6

7. Swipe to display long characters

7.1.

  • Enter in the text input area that there will be long winds and waves, hang the clouds and sail directly to the sea , press ctrl+enter , click the C51 format to take the modulus, and get the bitmap
    insert image description here

7.2. Code writing

  • Find the oledfont.h file under gui.c , and store the generated Chinese character lattice in **cfont[ ]**
const typFNT_GB16 cfont16[] = 
{
    
    
"长",0x08,0x00,0x08,0x10,0x08,0x20,0x08,0x40,0x08,0x80,0x09,0x00,0x08,0x00,0xFF,0xFE,
0x0A,0x00,0x09,0x00,0x08,0x80,0x08,0x40,0x09,0x20,0x0A,0x18,0x0C,0x06,0x08,0x00,
"风",0x00,0x00,0x3F,0xF0,0x20,0x10,0x20,0x10,0x28,0x50,0x24,0x50,0x22,0x90,0x22,0x90,
0x21,0x10,0x21,0x10,0x22,0x90,0x22,0x92,0x24,0x4A,0x48,0x4A,0x40,0x06,0x80,0x02,
"破",0x00,0x10,0x00,0x10,0xFC,0x10,0x11,0xFE,0x11,0x12,0x21,0x14,0x3D,0x10,0x65,0xFC,
0x65,0x44,0xA5,0x44,0x25,0x28,0x25,0x28,0x3D,0x10,0x26,0x28,0x22,0x44,0x04,0x82,
"浪",0x00,0x80,0x20,0x40,0x13,0xF8,0x12,0x08,0x82,0x08,0x43,0xF8,0x4A,0x08,0x0A,0x08,
0x13,0xF8,0x12,0x40,0xE2,0x44,0x22,0x28,0x22,0x10,0x22,0x88,0x23,0x06,0x02,0x00,
"会",0x01,0x00,0x01,0x00,0x02,0x80,0x04,0x40,0x08,0x20,0x30,0x18,0xCF,0xE6,0x00,0x00,
0x00,0x00,0x7F,0xFC,0x02,0x00,0x04,0x00,0x08,0x20,0x10,0x10,0x3F,0xF8,0x10,0x08,
"有",0x02,0x00,0x02,0x00,0xFF,0xFE,0x04,0x00,0x04,0x00,0x0F,0xF0,0x08,0x10,0x18,0x10,
0x2F,0xF0,0x48,0x10,0x88,0x10,0x0F,0xF0,0x08,0x10,0x08,0x10,0x08,0x50,0x08,0x20,
",",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x10,0x00,0x20,0x00,0x00,0x00,
"时",0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,
0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10,
"直",0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0xFF,0xFE,0x00,0x00,
"挂",0x10,0x20,0x10,0x20,0x11,0xFC,0x10,0x20,0xFC,0x20,0x10,0x20,0x13,0xFE,0x10,0x00,
0x18,0x20,0x30,0x20,0xD1,0xFC,0x10,0x20,0x10,0x20,0x10,0x20,0x53,0xFE,0x20,0x00,
"云",0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x02,0x00,
0x04,0x00,0x04,0x00,0x08,0x40,0x10,0x20,0x20,0x10,0x7F,0xF8,0x20,0x08,0x00,0x08,
"帆",0x10,0x00,0x11,0xF0,0x11,0x10,0x7D,0x10,0x55,0x10,0x55,0x10,0x55,0x90,0x55,0x50,
0x55,0x50,0x55,0x10,0x55,0x10,0x5D,0x12,0x11,0x12,0x12,0x12,0x12,0x0E,0x14,0x00,
"济",0x00,0x80,0x20,0x40,0x17,0xFE,0x12,0x08,0x81,0x10,0x40,0xA0,0x40,0x40,0x11,0xB0,
0x16,0x0E,0x21,0x10,0xE1,0x10,0x21,0x10,0x21,0x10,0x22,0x10,0x22,0x10,0x04,0x10,
"沧",0x00,0x40,0x40,0x40,0x20,0xA0,0x21,0x10,0x02,0x08,0x84,0x04,0x4B,0xF2,0x42,0x10,
0x12,0x10,0x22,0x10,0xE2,0x50,0x22,0x20,0x22,0x04,0x22,0x04,0x21,0xFC,0x00,0x00,
"海",0x01,0x00,0x21,0x00,0x11,0xFC,0x12,0x00,0x85,0xF8,0x41,0x08,0x49,0x48,0x09,0x28,
0x17,0xFE,0x11,0x08,0xE2,0x48,0x22,0x28,0x23,0xFC,0x20,0x08,0x20,0x50,0x00,0x20,
};

insert image description here

  • Modify the Test_MainPage function in test.c
void TEST_MainPage(void)
{
    
    	
	GUI_ShowCHinese(10,20,16,"长风破浪会有时,直挂云帆济沧海",1);
	delay_ms(500);		
	delay_ms(500);
}

insert image description here

  • Modify the main function of the main.c file, add the corresponding OLED scrolling code; delete the function Test_MainPage in the while loop
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{
    
    	
	delay_init();	    	       //延时函数初始化	  
	NVIC_Configuration(); 	   //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 	
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0);             //清屏(全黑)
	OLED_WR_Byte(0x2E,OLED_CMD);        //关闭滚动
  OLED_WR_Byte(0x27,OLED_CMD);        //水平向左或者右滚动 26/27
  OLED_WR_Byte(0x00,OLED_CMD);        //虚拟字节
	OLED_WR_Byte(0x00,OLED_CMD);        //起始页 0
	OLED_WR_Byte(0x07,OLED_CMD);        //滚动时间间隔
	OLED_WR_Byte(0x07,OLED_CMD);        //终止页 7
	OLED_WR_Byte(0x00,OLED_CMD);        //虚拟字节
	OLED_WR_Byte(0xFF,OLED_CMD);        //虚拟字节
	TEST_MainPage();
	OLED_WR_Byte(0x2F,OLED_CMD);        //开启滚动
	while(1) 
	{
    
    	
	}
}

insert image description here

7.3. Hardware connection

Connection method of power supply module:

USB to TTL STM32F103C8T6
GND G
3V3 3V3
RXD PA9
TXD PA10

Note that BOOT0 on the core board is set to 1, and BOOT1 is set to 0

insert image description here
insert image description here

Screen connection:
insert image description here
insert image description here

7.4, compile and burn

  • compile
    insert image description here

  • burning
    insert image description here

7.5. Realize the effect

OLED sliding display long characters

7.6, complete code

Baidu Netdisk:
Link: https://pan.baidu.com/s/13rgixlMhU1Rt9q8LrU5-YA?pwd=2022
Extraction code: 2022

gitee warehouse address:
link: https://gitee.com/xuwenjie10086/qrs/tree/master/OLED%E5%B1%8F%E5%B9%95%E6%BB%91%E5%8A%A8%E6% 98%BE%E7%A4%BA%E9%95%BF%E5%AD%97%E7%AC%A6%E4%B8%B2

8. Summary

This article mainly introduces the principle of OLED screen display and Chinese character dot matrix coding, and on this basis, uses the SPI interface of STM32F103, AHT20 temperature sensor and OLED screen display to realize the display of student number name, display temperature and humidity, and long character sliding display.

This experiment has a lot of content, it is not easy to implement, and it takes a long time to complete, so it needs to be patient and persevere. Although it takes a long time, when you see the corresponding content displayed on the OLED screen according to your preferences, you will feel very happy and satisfied.

Then there are some problems and solutions encountered in this experiment, that is, when the OLED screen is used to display the temperature and humidity, the screen will display, but the read temperature and humidity are always 0.0, no matter how to change it, the temperature and humidity cannot be displayed normally. Then I changed to a classmate’s board, the connection method is exactly the same, the code also uses the code I burned to my own board, but the outrageous thing is that the temperature and humidity can be displayed normally on the classmate’s board (very metaphysics) .

I asked the teacher later, and said that it might be because the voltage is too low to support the OLED and the AHT20 temperature and humidity sensor to work together, so it needs to be connected to 5V.

The last thing is to insist on hands-on practice in hardware learning, and "discover problems-analyze problems-solve problems" in practice, so as to improve yourself.


Reference list:
1. Detailed explanation of SPI protocol (illustrated and super detailed)
2. Super detailed explanation of SPI principle-worth a look
3. Reading and display of dot-matrix Chinese characters
4. Display data on 0.96-inch OLED display based on STM32
5. STM32+OLED screen display application examples

Guess you like

Origin blog.csdn.net/qq_52068373/article/details/127691138