[application example of STM32+OLED screen display]

Keywords: C Single-Chip Microcomputer stm32

1, Related technology
. 1.1 about SPI protocol
SPI protocol is a communication protocol (Serial Peripheral Interface) proposed by Motorola, that is, serial peripheral
The standby interface is a high-speed full duplex communication bus. It is widely used between ADC, LCD and MCU,
Occasions requiring high communication rate.
SPI physical layer

SPI protocol layer
SPI basic 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.

Start and stop signals of communication
At label ① in figure 25-2, the NSS signal line changes from high to low, which is the starting signal of SPI communication. NSS is every
The slave has its own exclusive signal line. When the slave detects the start signal on its own NSS line, it knows that it has been selected by the host
Yes, start preparing to communicate with the host. At mark ⑥ in the figure, the NSS signal changes from low to high, which is the stop of SPI communication
Signal, indicating that this communication is over and the selected state of the slave is cancelled.
Data validity

SPI uses MOSI and MISO signal lines to transmit data, and SCK signal lines to synchronize data. MOSI and
MISO data line transmits one bit of data in each clock cycle of SCK, and data input and output are carried out at the same time. data
During transmission, MSB first or LSB first has no hard regulations, but it is necessary to ensure that the same is used between the two SPI communication devices
The MSB look ahead mode in figure 25-2 is generally adopted for the protocol.
CPOL/CPHA and communication mode

1.2 scrolling command of LED screen
Horizontal shift 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

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

1.3 OLED pin connection

2, SPI based OLED display
2.1 display your student number and name
2.1.1 complete code
3.1.2 modify code

#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
	while(1) 
	{	
		
		
	}
}

Modify test in test.c_ GUI in mainpage function_ ShowString,

You need to add the corresponding font dot matrix to oledfont.h

2.1.3 Chinese character module Lattice software

The hexadecimal codes corresponding to different modulo modes are different and added to oledfont.h

main program

#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)
	while(1) 
	{	
		TEST_MainPage();         //Main interface display test
		
	}
}

3.1.4 compiling and burning

3.1.5 operation results


3.2 display the temperature and humidity of AHT20
3.2.1 complete code

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

It mainly sends the temperature acquisition to the OLED screen through the serial port.

3.2.3 compiling and burning

3.2.4 operation results

3.3 left and right sliding display long characters
3.3.1 complete code
3.3.2 modify code
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
	while(1) 
	{	
		
		
	}
}

Continue adding font data

3.3.3 compiling and burning

3.3.4 operation result display

4, Summary
Through the study of ISPI protocol, I understand the underlying logic of these chips, and have a deeper understanding of the working principle of the protocol through the logic analyzer. This experiment has greatly improved my hands-on ability. I also learned a lot of practical things. I hope I can learn more about this in the future.

Posted by cdickson on Fri, 26 Nov 2021 19:45:40 -0800