Technology Sharing

04OLED Introduction and Debugging Methods

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Debugging mode

insert image description here
The computer can print any variable it wants to see directly to the screen, but the microcontroller often does not have a strong evaluation due to cost and circuit structure limitations.
Just like learning C language, you can't learn it without the printf function. So you need a debugging tool.

Serial port debugging

Serial port debugging is very common. Connecting the MCU to the computer with the serial port is equivalent to hanging the computer screen on the MCU, which is convenient for the MCU to display debugging information. When designing the PCB, there will be a serial port communication pin for program debugging (the advantage is that you can use a powerful computer to debug. The software on the computer can not only display individual parameters, but also curves, graphics, images, etc. You can also make your own software to achieve powerful user interaction functions) (The disadvantage is that you have to drag the computer when debugging, and the usual serial port assistant can only present data in the form of information flow, that is, it can only be printed line by line. If there is a lot of constantly changing data that needs to be displayed, then the computer can only refresh the screen to display)

Display debugging

The advantage of display debugging is that it can refresh the display for constantly changing data, and the display can always be connected to the microcontroller, and the display method is very direct. When doing something slightly more complicated, a human-computer interaction interface is required, so that the display can be regarded as part of the product, which is more convenient to use (the disadvantage is that the screen is too small and the display content is limited)
The screen can be used for debugging, and the driver function will be discussed later. First, treat OLED as a program debugging module

Other debugging methods

There is also a light debugging method. If you don't know where the program is executed, you can put a light code at that position.
Comment debugging method: if the program was originally good, but it crashed after adding a certain section, you can remove all the newly added comments to restore it to normal operation, and then uncomment it line by line until an error occurs, or comment out one part to test another part, reduce the part of the program running, and locate where the problem is.
Comparison method: find a program without problems, and gradually replace its program logic with your program logic.

Summarize:

The basic idea of ​​the testing procedure: narrow the scope, control variables, comparative testing, etc.

OLED

Introduction

insert image description here
Each pixel of OLED is a separate light-emitting diode, while LCD requires a backlight, so it is relatively energy-saving.
Fast response speed can make OLED have a higher refresh rate
Fast bus timing can also avoid blocking programs
Wide viewing angle: Because OLED is self-luminous, the displayed content is clear at any angle

Driving this simple OLED only requires a few wires, which takes up relatively few resources.

The pixel colors are: white, blue, and yellow-blue (1/4 is yellow, 3/4 is blue, it is not a true two-color, but fixed in different areas, suitable for an interface that needs to display a title row)

Hardware Circuit

insert image description here
Pin 4 generally uses the IIC protocol, and pin 7 generally uses the SPI protocol.

OLED drive function

insert image description here
If the STM32 pins are not initialized, the default is floating input mode

To use this OLED module, you only need to change the pins, the peripherals and clocks that use the pins, and the rest of the functions are packaged
insert image description here

The OLED_FONT.h file stores the font data of OLED. Since the OLED display does not have a font library, if you want to display character graphics, you must first define the dot matrix data of the characters. Therefore, this stores the dot matrix data (font library) of the characters.
C language cannot write binary numbers, only hexadecimal numbers can be used instead. As a low-level computer programming language, C language does not support writing binary numbers, and C language must be translated into assembly language, but assembly language supports binary writing. But this may be a rule.

Keil debug mode

How to enter

insert image description here
The project option Debug can configure the debugging options. The default option is to perform online simulation on the hardware on the right. You need to connect STLINK and the microcontroller.
The one on the left is using an emulator, which is a computer simulating the operation of the microcontroller.
Before simulation, make sure that there are no problems with the project compilation (if there are any problems, solve them first)

Click "red d" to enter debug mode. If STLINK is not connected, an error will be reported.
insert image description here

Keil debugging interface window

insert image description here
If you use assembly language programming, you need to understand the register groups and status flags very clearly.
If you use C language, then you don't have to worry about these.

insert image description here

Simple function description

This part is the program operation control
1. Reset 2. Run at full speed 3. Stop running at full speed 4. Single-step 5. Skip the current line and run single-step 6. Jump out of the current function and run single-step 7. Move to the cursor specified line and run single-step

insert image description here
The yellow arrow indicates the next line of code to be executed, and the blue arrow indicates the line where the cursor is located.
insert image description here
Click the dark gray area on the left to set a breakpoint. Click Run at Full Speed ​​and the program will continue to run until it stops at the breakpoint. If there is no breakpoint, the program will not stop automatically when running at full speed. You need to click the Stop button for the program to stop.

insert image description here
Click RST to reset, the program will return to the beginning, and you can see that the program is in the reset interrupt function, which means that the program starts executing from here after reset.

First, the program will jump to the SystemInit function, and then the function will execute into the main function.

This method can accurately track how our program runs. You can explore how the function is executed step by step in this mode. This will give you a deeper understanding of the program's logic.

This is a simple function in debug mode. There are more powerful functions in debug mode.

More powerful features

insert image description here
The first is the command window, which can be opened and closed
insert image description here
Second Disassembly Window
The third symbol window can view the values ​​of all variables in the program in real time
insert image description here
insert image description here
If you want to see the changes in the structure value, you can right-click and add it to the Watch1 window.
insert image description here
By clicking single-step run, you can see the changes in variable values, which is very convenient.

There is also a serial port display
insert image description here
logic analyzer
insert image description here
Wait, you can take some time to learn more about it.

You can also click on the peripheral menu and view the system resources to see all the peripheral registers.
insert image description here
For example, if you select GPIOA, all the registers of the GPIOA peripheral will be displayed on the right.
insert image description here

During debugging, STM32 is also executing the program in real time. When the simulation point stops, the STM32 program also stops running. When you click Run again, STM32 continues to execute. Although there will be some delays in real-time changes, the online debugging function is still very powerful. STM32 executes the program in real time, and the Keil software displays the status of peripheral registers in real time. You can also control the start, stop, and single-step operation of the program at any time. When you encounter a program that is difficult to debug, such as not knowing how the program is executed, or wanting to see a lot of variables that are not convenient to display, or wanting to see if the register value is configured correctly, you can use Keil's built-in debugging mode. If you modify the program, you cannot modify it directly in the debugging mode. To modify the program, you must first exit the debugging mode, recompile, and then enter the debugging mode.

Keil has more features waiting for further exploration.
insert image description here
Help opens the official help document, which has the most authoritative and detailed introduction, and more black technologies on how to use Keil