[untitled] OLED screen display experiment (STM32)

Keywords: stm32

1, Experimental 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. Slide up and down or left and right to display long characters, such as "Hello, welcome to the Internet of things 205 training room of Chongqing Jiaotong University!" or a lyrics or poem (it is best to use the hardware screen brushing mode).

2, SPI introduction

1. What is SPI?

SPI is the abbreviation of serial peripheral interface. It is a product launched by Motorola
A synchronous serial interface technology is a high-speed, full duplex, synchronous communication bus.

2. SPI benefits

Support full duplex communication
Simple communication
Data transfer rate block

3. Shortcomings

There is no specified flow control and no response mechanism to confirm whether the data is received, so the data is compared with the IIC bus protocol
There are some defects in reliability.

4. Characteristics

1) : high speed, synchronous, full duplex, non differential, bus type
2) : master slave communication mode
3) : serial data can be sent and received at the same time; It can work as a master or slave; Provide frequency programmable clock; Sending end interrupt flag; Write conflict protection; Bus contention protection, etc

5. Detailed explanation of protocol communication sequence

1) : the communication principle of SPI is very simple. It works in a master-slave mode. This mode usually has one master device and one or more
A slave device requires at least 4 wires. In fact, 3 wires can also be used (in case of unidirectional transmission). It is also common to all SPI based devices
Yes, they are SDI (data input), SDO (data output), SCLK (clock), CS (chip selection).
(1)SDO/MOSI - Master equipment data output and slave equipment data input;
(2)SDI/MISO - Master equipment data input and slave equipment data output;
(3)SCLK – clock signal, generated by the master equipment;
(4)CS/SS – slave enable signal, controlled by master. When there are multiple slaves, because each slave
A chip selection pin is connected to the master device on the standby. When our master device communicates with a slave device, it will be necessary to
Pull the chip selection pin level corresponding to the slave device low or high.

It should be noted that SPI communication has four different modes. Different slave devices may be configured to a certain mode at the factory, which cannot be changed; However, the first mock exam is the same as the two sides of the communication. So we can configure the SPI mode of our main device and control the communication mode of our main device through CPOL (clock polarity) and CPHA (clock phase).
Mode0: CPOL=0,CPHA=0
Mode1: CPOL=0,CPHA=1
Mode2: CPOL=1,CPHA=0
Mode3: CPOL=1,CPHA=1

The clock polarity CPOL is used to configure which state the SCLK level is in, the idle state or the active state, and the clock phase CPHA
It is used to configure the edges of data sampling:
CPOL=0 means that SCLK is in idle state when SCLK=0, so the effective state is that SCLK is at high level
CPOL=1 indicates that SCLK is in idle state when SCLK=1, so the effective state is that SCLK is at low level
CPHA=0, indicating that data sampling is at the first edge and data transmission is at the second edge
CPHA=1, indicating that data sampling is at the second edge and data transmission is at the first edge

Communication process of SPI:

3, OLED screen introduction

Organic light emitting diode (OLED), also known as organic electroluminescence display (OLED), refers to the phenomenon that organic semiconductor materials and light-emitting materials emit light through carrier injection and recombination driven by electric field. Generally speaking, OLEDs can be divided into two kinds according to luminescent materials: small molecule OLED and polymer OLED (also known as PLED). OLED is an electroluminescent device using multilayer organic thin film structure. It is easy to manufacture and only needs low driving voltage. These main characteristics make OLED very prominent in meeting the application of flat panel display. OLED display is lighter and thinner than LCD, with high brightness, low power consumption, fast response, high definition, good flexibility and high luminous efficiency, which can meet the new needs of consumers for display technology.

CD s need backlight, but OLEDs don't because it's self luminous. This same display, OLED effect is better. With the current technology, the size of OLED is still difficult to be large-scale, but the resolution can be very high.
Interface:

1. This module supports IIC, 3-wire SPI and 4-wire SPI interface bus mode switching (as shown in the red box in Figure 2). The specific instructions are as follows:
A. If only R3 and R4 are welded with 4.7K resistance, select 4-wire SPI bus interface (default);
B. If only R2 and R3 are welded with 4.7K resistance, select 3-wire SPI bus interface;
C. Use 4.7K resistance to weld only R1, R4, R6, R7 and R8, then select IIC bus interface;
2. After the interface bus mode is switched, it is necessary to select the corresponding supporting software and corresponding wiring pins (as shown in Figure 1) before the module can operate normally. The corresponding wiring pins are described as follows:
A. Select 4-wire SPI bus interface, and all pins need to be used;
B. Select 3-wire SPI bus interface, only DC pin does not need to be used (can not be connected), and other pins need to be used;
C. To select IIC bus interface, only GND, VCC, D0 and D1 pins need to be used, RES is connected to high level (VCC can be connected), and DC and CS are grounded;


We use ALINETEK's OLED display module, which has the following characteristics:
1) The module is available in monochrome and two colors. The monochrome is pure blue, while the two colors are yellow and blue.
2) The size is small, the display size is 0.96 inches, while the size of the module is only 27mmx26mm.
3) High resolution, the resolution of the module is 128x64.
4) Multiple interface modes. The module provides a total of 5 interfaces, including 6800 and 8080 parallel interface modes, 3-wire or 4-wire through SPI interface mode and IIC interface mode (OLED can be controlled by only 2 wires).
5) Without high voltage, it can work directly connected to 3.3V.
Note that the module is not compatible with the 5.0V interface, so be careful not to connect directly to the 5V system, otherwise the module may be burned.

For more information about OLED s, see the following connections:
Link: link.

4, Experimental process

(1) Display your student number and name

First, take the model of the text to be displayed on the OLED screen:
Because there are already numbers and letters in the oledfont.h file, we only need to add the font of our name to the oledfont.h file:
The text to be displayed is expressed in hexadecimal by using the modeling software, and the modeling software will be placed in the data link at the end of the article.

First select the font. Here I choose Song typeface
The size of the dot matrix is 16 * 16
Then we click the user-defined code table and input the word we want to take the module (my name is tie Changfeng)
After selecting the. c file,
Finally, click Save font to complete the mold taking.
Select Notepad to open the font we have taken:

Select and copy to the 16 * 16 location in oledfont.h in gui.c in USER:

After you add in here, the double quotation marks and commas need to be English letters, otherwise you will make mistakes

Then in test.c

mian.c:

int main(void)
{	
	delay_init();	    	       //Delay function initialization	  
	OLED_Init();			         //Initialize OLED  
	OLED_Clear(0);             //Clear screen (all black)
	while(1) 
	{	
		TEST_MainPage();         //Interface display
	}

Write in the name and number to be displayed in this function;
If you write Chinese characters into it?, You need to
Open Keil, click "Edit" in the upper left corner, and then click "Configuration" at the bottom to pop up a dialog box;

Select Chinese and click OK to enter
Then compile:

freedom from error
Experimental results:

(2) OLED displays the temperature and humidity measured by AHT20

Code writing: (display temperature)

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

	}
	/*Display the collected temperature and humidity through the serial port
	printf("\r\n");
	printf("Temperature:% d%d.%d",T1/100,(T1/10)%10,T1%10);
	printf("Humidity:% d%d.%d",H1/100,(H1/10)%10,H1%10);
	printf("\r\n");*/
	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);   //Call the Sprintf function to format the temperature data of DHT11 into the string array variable strTemp  
    sprintf(strHumi,"%.1f",b);    //Call the Sprintf function to format the humidity data of DHT11 into the string array variable strHumi  
	GUI_ShowCHinese(16,00,16,"Temperature and humidity display",1);
	GUI_ShowCHinese(16,20,16,"temperature",1);
	GUI_ShowString(53,20,strTemp,16,1);
	GUI_ShowCHinese(16,38,16,"humidity",1);
	GUI_ShowString(53,38,strHumi,16,1);
	delay_ms(1500);		
	delay_ms(1500);
}

Add the corresponding dot matrix character (which can be obtained by using the font tool) as in the above steps

	"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*/
	"display",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,/*"Display ", 0*/
	"show",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,/*"Display ", 0*/

main function:

#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"
#include "sys.h"

#include "oled.h"
#include "gui.h"
#include "test.h"

int main(void)
{	
	delay_init();	    	       //Delay function initialization    	  
	uart_init(115200);	 
	IIC_Init();
		  
	NVIC_Configuration(); 	   //Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority 	
	OLED_Init();			         //Initialize OLED  
	OLED_Clear(0); 
	while(1)
	{
		//printf("temperature and humidity display");
		read_AHT20_once();
		OLED_Clear(0); 
		delay_ms(1500);
  }
}

Burning results:


When I put my hand over the sensor, the temperature and humidity changed

(3) 0.96 inch OLED realizes scrolling display of long characters on STM32f103

1. Move horizontally left and right

OLED_WR_Byte(0x2E,OLED_CMD);        //Turn off scrolling
OLED_WR_Byte(0x26,OLED_CMD);        //Scroll horizontally left or right 26 / 27
OLED_WR_Byte(0x00,OLED_CMD);        //virtual byte
OLED_WR_Byte(0x00,OLED_CMD);        //Start page 0
OLED_WR_Byte(0x07,OLED_CMD);        //Rolling interval
OLED_WR_Byte(0x07,OLED_CMD);        //Termination page 7
OLED_WR_Byte(0x00,OLED_CMD);        //virtual byte
OLED_WR_Byte(0xFF,OLED_CMD);        //virtual byte
OLED_WR_Byte(0x2F,OLED_CMD);        //Turn on scrolling

2. Vertical and horizontal scrolling

OLED_WR_Byte(0x2e,OLED_CMD);        //Turn off scrolling
OLED_WR_Byte(0x29,OLED_CMD);        //Horizontal vertical and horizontal scroll left and right 29/2a
OLED_WR_Byte(0x00,OLED_CMD);        //virtual byte
OLED_WR_Byte(0x00,OLED_CMD);        //Start page 0
OLED_WR_Byte(0x07,OLED_CMD);        //Rolling interval
OLED_WR_Byte(0x07,OLED_CMD);        //Termination page 1
OLED_WR_Byte(0x01,OLED_CMD);        //Vertical scroll offset
OLED_WR_Byte(0x2F,OLED_CMD);        //Turn on scrolling

Note: before setting, it is necessary to first send the instruction 2E to turn off scrolling, and then send the scrolling instruction 29 (right) or 2A (left). Then send five parameter setting instructions to set the continuous horizontal scrolling parameters and determine the scrolling start page, end page, scrolling speed and vertical scrolling offset. Finally, send the instruction 2F to start scrolling.
Note: the display data shall be transmitted before sending the start of scrolling (2F). If the display data is transmitted during scrolling, the contents in RAM may be damaged and cannot be displayed normally.

Next, you need to take a template for the text you want to display:

	//U + 829c (Wuhu)
"Barren",0x08,0x20,0x08,0x20,0xFF,0xFE,0x08,0x20,0x00,0x00,0x3F,0xF0,0x02,0x00,0x02,0x00,
0x7F,0xFC,0x02,0x80,0x04,0x80,0x04,0x80,0x08,0x84,0x10,0x84,0x20,0x7C,0xC0,0x00,

//U + 6e56 (Lake)
"lake",0x01,0x00,0x21,0x1E,0x11,0x12,0x17,0xD2,0x81,0x12,0x41,0x1E,0x41,0x12,0x17,0xD2,
0x14,0x52,0x24,0x5E,0xE4,0x52,0x27,0xD2,0x24,0x52,0x20,0x22,0x20,0x2A,0x00,0x44,

//U + 8d77 (from)
"rise",0x08,0x00,0x08,0x00,0x08,0xF8,0x7E,0x08,0x08,0x08,0x08,0x08,0xFE,0xF8,0x08,0x88,
0x28,0x80,0x28,0x80,0x2E,0x84,0x28,0x84,0x28,0x7C,0x58,0x00,0x4F,0xFE,0x80,0x00,

//U + 98de (flying)
"fly",0x00,0x00,0xFF,0xC0,0x00,0x40,0x00,0x44,0x00,0x48,0x00,0x50,0x00,0x60,0x00,0x50,
0x00,0x48,0x00,0x44,0x00,0x20,0x00,0x20,0x00,0x12,0x00,0x0A,0x00,0x06,0x00,0x02,

Main codes:
main.c:

#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{	
	delay_init();	    	       //Delay function initialization	  
	NVIC_Configuration(); 	   //Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority 	
	OLED_Init();			         //Initialize OLED  
	OLED_Clear(0);             //Clear screen (all black)
	OLED_WR_Byte(0x2E,OLED_CMD);        //Turn off scrolling
    OLED_WR_Byte(0x27,OLED_CMD);        //Scroll horizontally left or right 26 / 27
    OLED_WR_Byte(0x00,OLED_CMD);        //virtual byte
	OLED_WR_Byte(0x00,OLED_CMD);        //Start page 0
	OLED_WR_Byte(0x07,OLED_CMD);        //Rolling interval
	OLED_WR_Byte(0x07,OLED_CMD);        //Termination page 7
	OLED_WR_Byte(0x00,OLED_CMD);        //virtual byte
	OLED_WR_Byte(0xFF,OLED_CMD);        //virtual byte
	TEST_MainPage();
	OLED_WR_Byte(0x2F,OLED_CMD);        //Turn on scrolling
}

Display data function:

void TEST_MainPage(void)
{	
	GUI_ShowCHinese(10,20,16,"Wuhu take off!",1);
	delay_ms(1500);		
	delay_ms(1500);
}

The results are as follows:

5, Summary

The first part of this experiment is about using STM32 to collect the temperature measured by AHT20 through the I2C interface and upload it to the host computer. This experiment is that we understand the SPI protocol and the working mode of I2C. In fact, there are few steps, because the code teacher has provided it. The requirements of the following two experiments are about the OLED display flat display of the temperature and humidity measured by AHT20 , and then use OLED to display scrolling subtitles. In fact, doing scrolling subtitles is the same as doing the student number and name, but a child parent scrolling module is added on the basis of that. Using OLED to display the temperature measured by AHT20 is actually transmitting the obtained data to OLED.

6, References

https://blog.csdn.net/qq_45237293/article/details/111712565
https://blog.csdn.net/qq_45237293/article/details/111088715
https://blog.csdn.net/qq_43279579/article/details/111500137
https://blog.csdn.net/qq_43279579/article/details/111500137

The project of the above experiment:
https://pan.baidu.com/s/1N0ljQ8Y7uJG92jomt6JbYQ
Extraction code: wpj0

Posted by unknown1 on Tue, 30 Nov 2021 21:37:50 -0800