[embedded 16] STM32+OLED screen display application example

1, Title 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 or a paragraph of lyrics or poetry.

2, SPI introduction

SPI (Serial Peripheral interface) is a Serial Peripheral interface. SPI interface is mainly used between EEPROM, FLASH, real-time clock, AD converter, digital signal processor and digital signal decoder.

SPI is a high-speed, full duplex and synchronous communication bus, and only occupies four wires on the pins of the chip, which saves the pins of the chip, saves space and provides convenience for the layout of PCB. It is precisely because of this simple and easy-to-use characteristic that more and more chips have integrated this communication protocol, and STM32 also has SPI interface.

SPI interface generally uses 4 lines for communication:

MISO master equipment data input and slave equipment data output.
MOSI master device data output and slave device data input.
SCLK clock signal is generated by the master equipment.
The chip selection signal of CS slave equipment is controlled by the master equipment.

SPI's main features are: it can send and receive serial data 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.

SPI bus has four working modes. In order to exchange data with peripherals, SPI module can configure its output serial synchronization clock polarity and phase according to the working requirements of peripherals. Clock polarity (CPOL) has no significant impact on the transmission protocol.

If CPOL=0, the idle state of the serial synchronization clock is low; If CPOL=1, the idle state of the serial synchronization clock is high.

The clock phase (CPHA) can be configured to select one of two different transmission protocols for data transmission. If CPHA=0, the data is sampled at the first jump edge (rising or falling) of the serial synchronization clock; If CPHA=1, the data is sampled at the second jump edge (up or down) of the serial synchronization clock.

The clock phase and polarity of SPI main module and external equipment communicating with it shall be consistent.

SPI communication process

MOSI and MISO signals are valid only when NSS is at low level. MOSI and MISO transmit one bit of data in each clock cycle of SCK.

3, OLED introduction

OLED is organic light emitting diode, also known as organic electroluminescence display (OELD). OLED is considered as the emerging application technology of the next generation of flat panel display because of its excellent characteristics such as self luminescence, no backlight, high contrast, thin thickness, wide viewing angle, fast reaction speed, flexible panel, wide temperature range, simple structure and manufacturing process.

LCD needs backlight, but OLED does not because it is 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.

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.

The module uses 8 * 2 2 2.54 rows of pins to connect with the outside, with a total of 16 pins. Among the 16 lines, we only use 15, and one is suspended. Among the 15 lines, power supply and ground wire account for 2, and there are 13 signal lines left. In different modes, the number of signal lines we need is different. In 8080 mode, we need all 13 lines, while in IIC mode, only 2 lines are enough! One of them is common, that is, the reset line RST (RES). The low level on RST will lead to OLED reset. The OLED module should be reset before each initialization.

In the following experiments, seven wire OLED s will be used
Refer to the Demo program given by the manufacturer: 0.96 inch SPI_OLED module supporting data package

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

4, STM32+OLED displays individual student number and name

1. Text modeling method

Theoretical introduction in [embedded 14] It is introduced in

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.

Software initial settings

Enter the target text in the text input area and ctrl+enter to get the display diagram

Click C51 format to generate dot matrix

2. Code writing

Content display TEST_MainPage function - > test. C file

void TEST_MainPage(void)
{	
//	GUI_ShowString(28,0,"abc",16,1);// English name
	GUI_ShowCHinese(28,20,16,"Yao Yier",1);//Chinese name
	GUI_ShowString(4,48,"12345678910",16,1);//Digital detail
	delay_ms(1500);		
	delay_ms(1500);
}

Text storage (example) - > oledfont. H file

const typFNT_GB16 cfont16[] = 
{
	"system",0x00,0xF8,0x3F,0x00,0x04,0x00,0x08,0x20,0x10,0x40,0x3F,0x80,0x01,0x00,0x06,0x10,
	0x18,0x08,0x7F,0xFC,0x01,0x04,0x09,0x20,0x11,0x10,0x21,0x08,0x45,0x04,0x02,0x00,/*"System ", 0*/
	"Unified",0x10,0x40,0x10,0x20,0x20,0x20,0x23,0xFE,0x48,0x40,0xF8,0x88,0x11,0x04,0x23,0xFE,
	0x40,0x92,0xF8,0x90,0x40,0x90,0x00,0x90,0x19,0x12,0xE1,0x12,0x42,0x0E,0x04,0x00,/*"System ", 1*/
	"set up",0x00,0x00,0x21,0xF0,0x11,0x10,0x11,0x10,0x01,0x10,0x02,0x0E,0xF4,0x00,0x13,0xF8,
	0x11,0x08,0x11,0x10,0x10,0x90,0x14,0xA0,0x18,0x40,0x10,0xA0,0x03,0x18,0x0C,0x06,/*"Set "2"*/
	"Set",0x7F,0xFC,0x44,0x44,0x7F,0xFC,0x01,0x00,0x7F,0xFC,0x01,0x00,0x1F,0xF0,0x10,0x10,
	0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0xFF,0xFE,0x00,0x00,/*"Set ", 3*/
};

Main function - > main. C file

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

3. Effect display

5, STM32+OLED displays the temperature and humidity of AHT20

1. Code writing

Temperature and humidity display read_AHT20 function - > BSP_ I2C. C file

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

Dot matrix display text

	"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 main.c file

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

2. Effect display


The acquisition speed can be modified.

6, STM32+OLED up and down or left and right sliding display long characters

1. Scroll settings

Horizontal left-right movement

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

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

The display data shall be transmitted before sending and scrolling. If the display data is transmitted during scrolling, the contents in RAM may be damaged and cannot be displayed normally.

2. Code writing

Add text font code - > oledfont. H file

OLED display function test.c

void TEST_MainPage(void)
{	
	GUI_ShowCHinese(10,20,16,"We have a bright future",1);
	delay_ms(1500);		
	delay_ms(1500);
}

Main function main.c file

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

3. Effect display

7, Summary

Through three experiments, I am basically proficient in the operation of STM32+OLED. It shows that it is not difficult to complete three applications without problems in code and pin configuration.
Note that the word length should be set during OLED display, otherwise it cannot be fully displayed.
Pay attention to the difference between horizontal mold taking, vertical mold taking and reverse order when taking mold, otherwise you will get a piece of fuzzy dots instead of normal and clear Chinese characters.

OLED is an interesting peripheral. When more hardware projects are completed later, OLED can be used for debugging and display, which will be of great help. Therefore, it is necessary to master the use of OLED and practice more, which will benefit a lot.

other
Data link (including all codes)
Link: https://pan.baidu.com/s/1gD4f5UOWS4tslH8N5MTBsw
Extraction code: v5ti

Tags: Single-Chip Microcomputer stm32

Posted on Sat, 20 Nov 2021 19:13:43 -0500 by Sako