4 OLED 調試工具#
單片機常用的調試方式:
串口調試:通過串口通信,將調試信息發送到電腦端,電腦使用串口助手顯示調試信息
顯示屏調試:直接將顯示屏連接到單片機,將調試信息打印在顯示屏上
Keil 調試模式:借助 Keil 軟件的調試模式,可使用單步運行、設置斷點、查看寄存器及變量等功能
點燈調試法:在程序中加個點燈的代碼來判斷代碼是否執行到此位置
註釋調試法:新加入代碼出現錯誤,將新加入的代碼註釋掉,然後逐行接觸註釋直至錯誤出現
測試程序的基本思想:縮小範圍、控制變量、對比測試等
OLED(Organic Light Emitting Diode):有機發光二極管
OLED 顯示屏:性能優異的新型顯示屏,具有功耗低、相應速度快、寬視角、輕薄柔韌等特點
0.96 寸 OLED 模塊:小巧玲瓏、佔用接口少、簡單易用,是電子設計中非常常見的顯示屏模塊
供電:3~5.5V,通信協議:I2C/SPI,分辨率:128*64
OLED 硬件接線圖
OLED 驅動函數
函數 | 作用 |
---|---|
OLED_Init(); | 初始化 |
OLED_Clear(); | 清屏 |
OLED_ShowChar(1, 1, 'A'); | 顯示一個字符 |
OLED_ShowString(1, 3, "HelloWorld!"); | 顯示字符串 |
OLED_ShowNum(2, 1, 12345, 5); | 顯示十進制數字 |
OLED_ShowSignedNum(2, 7, -66, 2); | 顯示有符號十進制數字 |
OLED_ShowHexNum(3, 1, 0xAA55, 4); | 顯示十六進制數字 |
OLED_ShowBinNum(4, 1, 0xAA55, 16); | 顯示二進制數字 |
OLED 螢幕坐標圖及實際效果圖
GPIO 模擬 I2C 通信
OLED.c
#include "OLED.h"
#include "OLED_Font.h"
/*引腳配置*/
#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))
/*引腳初始化*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//將SCL和SDA兩個引腳都初始化為開漏輸出模式
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開始
* @param 無
* @retval 無
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 無
* @retval 無
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C發送一個字節
* @param Byte 要發送的一個字節
* @retval 無
*/
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); //額外的一個時鐘,不處理應答信號
OLED_W_SCL(0);
}
/**
* @brief OLED寫命令
* @param Command 要寫入的命令
* @retval 無
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機地址
OLED_I2C_SendByte(0x00); //寫命令
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief OLED寫數據
* @param Data 要寫入的數據
* @retval 無
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機地址
OLED_I2C_SendByte(0x40); //寫數據
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief OLED設置光標位置
* @param Y 以左上角為原點,向下方向的坐標,範圍:0~7
* @param X 以左上角為原點,向右方向的坐標,範圍:0~127
* @retval 無
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); //設置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //設置X位置低4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //設置X位置高4位
}
/**
* @brief OLED清屏
* @param 無
* @retval 無
*/
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 OLED顯示一個字符
* @param Line 行位置,範圍:1~4
* @param Column 列位置,範圍:1~16
* @param Char 要顯示的一個字符,範圍:ASCII可見字符
* @retval 無
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //設置光標位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //顯示上半部分內容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //設置光標位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //顯示下半部分內容
}
}
/**
* @brief OLED顯示字符串
* @param Line 起始行位置,範圍:1~4
* @param Column 起始列位置,範圍:1~16
* @param String 要顯示的字符串,範圍:ASCII可見字符
* @retval 無
*/
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 OLED次方函數
* @retval 返回值等於X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief OLED顯示數字(十進制,正數)
* @param Line 起始行位置,範圍:1~4
* @param Column 起始列位置,範圍:1~16
* @param Number 要顯示的數字,範圍:0~4294967295
* @param Length 要顯示數字的長度,範圍:1~10
* @retval 無
*/
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 OLED顯示數字(十進制,帶符號數)
* @param Line 起始行位置,範圍:1~4
* @param Column 起始列位置,範圍:1~16
* @param Number 要顯示的數字,範圍:-2147483648~2147483647
* @param Length 要顯示數字的長度,範圍:1~10
* @retval 無
*/
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 OLED顯示數字(十六進制,正數)
* @param Line 起始行位置,範圍:1~4
* @param Column 起始列位置,範圍:1~16
* @param Number 要顯示的數字,範圍:0~0xFFFFFFFF
* @param Length 要顯示數字的長度,範圍:1~8
* @retval 無
*/
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 OLED顯示數字(二進制,正數)
* @param Line 起始行位置,範圍:1~4
* @param Column 起始列位置,範圍:1~16
* @param Number 要顯示的數字,範圍:0~1111 1111 1111 1111
* @param Length 要顯示數字的長度,範圍:1~16
* @retval 無
*/
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 OLED初始化
* @param 無
* @retval 無
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) //上電延時
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //關閉顯示
OLED_WriteCommand(0xD5); //設置顯示時鐘分頻比/振蕩器頻率
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); //設置多路復用率
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //設置顯示偏移
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); //設置顯示開始行
OLED_WriteCommand(0xA1); //設置左右方向,0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //設置上下方向,0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //設置COM引腳硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //設置對比度控制
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //設置預充電周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //設置VCOMH取消選擇級別
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //設置整個顯示打開/關閉
OLED_WriteCommand(0xA6); //設置正常/倒轉顯示
OLED_WriteCommand(0x8D); //設置充電泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //開啟顯示
OLED_Clear(); //OLED清屏
}
OLED.h
#ifndef __OLED_H
#define __OLED_H
#include "stm32f10x.h"
void OLED_Init(void);//初始化
void OLED_Clear(void);//清屏
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);//顯示一個字符
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);//顯示字符串
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);//顯示十進制數字
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);//顯示有符號十進制數字
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);//顯示十六進制數字
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);//顯示二進制數字
#endif