4 OLED Debugging Tools#
Common debugging methods for microcontrollers:
Serial debugging: Send debugging information to the computer via serial communication, and the computer uses a serial assistant to display the debugging information.
Display debugging: Directly connect the display to the microcontroller and print debugging information on the display.
Keil debugging mode: Use the debugging mode of Keil software to perform functions such as single-step execution, setting breakpoints, and viewing registers and variables.
Light debugging method: Add a light code in the program to determine if the code has executed to this position.
Comment debugging method: If an error occurs in the newly added code, comment out the new code, and then uncomment line by line until the error appears.
Basic idea of testing programs: Narrow the scope, control variables, comparative testing, etc.
OLED (Organic Light Emitting Diode): Organic Light Emitting Diode
OLED display: A new type of display with excellent performance, characterized by low power consumption, fast response speed, wide viewing angle, and lightweight flexibility.
0.96-inch OLED module: Compact and exquisite, occupying few interfaces, simple and easy to use, it is a very common display module in electronic design.
Power supply: 3~5.5V, communication protocol: I2C/SPI, resolution: 128*64
OLED hardware wiring diagram
OLED driver functions
Function | Functionality |
---|---|
OLED_Init(); | Initialize |
OLED_Clear(); | Clear screen |
OLED_ShowChar(1, 1, 'A'); | Display a character |
OLED_ShowString(1, 3, "HelloWorld!"); | Display string |
OLED_ShowNum(2, 1, 12345, 5); | Display decimal number |
OLED_ShowSignedNum(2, 7, -66, 2); | Display signed decimal number |
OLED_ShowHexNum(3, 1, 0xAA55, 4); | Display hexadecimal number |
OLED_ShowBinNum(4, 1, 0xAA55, 16); | Display binary number |
OLED screen coordinate diagram and actual effect diagram
GPIO simulating I2C communication
OLED.c
#include "OLED.h"
#include "OLED_Font.h"
/*Pin configuration*/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
/*Pin initialization*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; // Initialize SCL and SDA pins as open-drain output mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C start
* @param None
* @retval None
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C stop
* @param None
* @retval None
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief Send a byte via I2C
* @param Byte The byte to be sent
* @retval None
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_W_SDA(Byte & (0x80 >> i));
OLED_W_SCL(1);
OLED_W_SCL(0);
}
OLED_W_SCL(1); // Extra clock, do not process acknowledgment signal
OLED_W_SCL(0);
}
/**
* @brief Write command to OLED
* @param Command The command to be written
* @retval None
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); // Slave address
OLED_I2C_SendByte(0x00); // Write command
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief Write data to OLED
* @param Data The data to be written
* @retval None
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); // Slave address
OLED_I2C_SendByte(0x40); // Write data
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief Set cursor position on OLED
* @param Y Coordinate from the top left corner, downward, range: 0~7
* @param X Coordinate from the top left corner, rightward, range: 0~127
* @retval None
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); // Set Y position
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); // Set low 4 bits of X position
OLED_WriteCommand(0x00 | (X & 0x0F)); // Set high 4 bits of X position
}
/**
* @brief Clear OLED screen
* @param None
* @retval None
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j++)
{
OLED_SetCursor(j, 0);
for(i = 0; i < 128; i++)
{
OLED_WriteData(0x00);
}
}
}
/**
* @brief Display a character on OLED
* @param Line Line position, range: 1~4
* @param Column Column position, range: 1~16
* @param Char The character to be displayed, range: ASCII visible characters
* @retval None
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); // Set cursor position in the upper half
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); // Display upper half content
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); // Set cursor position in the lower half
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); // Display lower half content
}
}
/**
* @brief Display string on OLED
* @param Line Starting line position, range: 1~4
* @param Column Starting column position, range: 1~16
* @param String The string to be displayed, range: ASCII visible characters
* @retval None
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++)
{
OLED_ShowChar(Line, Column + i, String[i]);
}
}
/**
* @brief Power function for OLED
* @retval Returns X raised to the power of Y
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief Display number on OLED (decimal, positive)
* @param Line Starting line position, range: 1~4
* @param Column Starting column position, range: 1~16
* @param Number The number to be displayed, range: 0~4294967295
* @param Length The length of the displayed number, range: 1~10
* @retval None
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief Display number on OLED (decimal, signed)
* @param Line Starting line position, range: 1~4
* @param Column Starting column position, range: 1~16
* @param Number The number to be displayed, range: -2147483648~2147483647
* @param Length The length of the displayed number, range: 1~10
* @retval None
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0)
{
OLED_ShowChar(Line, Column, '+');
Number1 = Number;
}
else
{
OLED_ShowChar(Line, Column, '-');
Number1 = -Number;
}
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief Display number on OLED (hexadecimal, positive)
* @param Line Starting line position, range: 1~4
* @param Column Starting column position, range: 1~16
* @param Number The number to be displayed, range: 0~0xFFFFFFFF
* @param Length The length of the displayed number, range: 1~8
* @retval None
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++)
{
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10)
{
OLED_ShowChar(Line, Column + i, SingleNumber + '0');
}
else
{
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
}
}
}
/**
* @brief Display number on OLED (binary, positive)
* @param Line Starting line position, range: 1~4
* @param Column Starting column position, range: 1~16
* @param Number The number to be displayed, range: 0~1111 1111 1111 1111
* @param Length The length of the displayed number, range: 1~16
* @retval None
*/
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
}
}
/**
* @brief Initialize OLED
* @param None
* @retval None
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) // Power-on delay
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); // Port initialization
OLED_WriteCommand(0xAE); // Turn off display
OLED_WriteCommand(0xD5); // Set display clock divide ratio/oscillator frequency
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); // Set multiplex ratio
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); // Set display offset
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); // Set display start line
OLED_WriteCommand(0xA1); // Set left-right direction, 0xA1 normal 0xA0 left-right inverted
OLED_WriteCommand(0xC8); // Set up-down direction, 0xC8 normal 0xC0 up-down inverted
OLED_WriteCommand(0xDA); // Set COM pin hardware configuration
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); // Set contrast control
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); // Set pre-charge period
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); // Set VCOMH deselect level
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); // Set entire display on/off
OLED_WriteCommand(0xA6); // Set normal/inverse display
OLED_WriteCommand(0x8D); // Set charge pump
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); // Turn on display
OLED_Clear(); // Clear OLED screen
}
OLED.h
#ifndef __OLED_H
#define __OLED_H
#include "stm32f10x.h"
void OLED_Init(void);// Initialize
void OLED_Clear(void);// Clear screen
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);// Display a character
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);// Display string
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);// Display decimal number
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);// Display signed decimal number
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);// Display hexadecimal number
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);// Display binary number
#endif