276 lines
7.1 KiB
C
276 lines
7.1 KiB
C
// ======================================================
|
|
// OLED.c (最终修正版, 支持中文)
|
|
// ======================================================
|
|
#include "stm32f10x.h"
|
|
#include "OLED_Font.h" // 包含ASCII和中文的字模
|
|
|
|
/*引脚配置*/
|
|
#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))
|
|
|
|
/* ... 从 OLED_I2C_Init() 到 OLED_ShowBinNum() 的所有函数保持不变 ... */
|
|
/* ... 请确保您文件里这部分内容是完整的 ... */
|
|
|
|
/*引脚初始化*/
|
|
/* 函数作用:初始化 OLED 软件 I2C 使用的 SCL 和 SDA GPIO。 */
|
|
void OLED_I2C_Init(void)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
|
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);
|
|
}
|
|
|
|
/* 函数作用:产生 OLED 软件 I2C 起始信号。 */
|
|
void OLED_I2C_Start(void)
|
|
{
|
|
OLED_W_SDA(1);
|
|
OLED_W_SCL(1);
|
|
OLED_W_SDA(0);
|
|
OLED_W_SCL(0);
|
|
}
|
|
|
|
/* 函数作用:产生 OLED 软件 I2C 停止信号。 */
|
|
void OLED_I2C_Stop(void)
|
|
{
|
|
OLED_W_SDA(0);
|
|
OLED_W_SCL(1);
|
|
OLED_W_SDA(1);
|
|
}
|
|
|
|
/* 函数作用:通过软件 I2C 向 OLED 发送一个字节。 */
|
|
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);
|
|
}
|
|
|
|
/* 函数作用:向 OLED 写入一条控制命令。 */
|
|
void OLED_WriteCommand(uint8_t Command)
|
|
{
|
|
OLED_I2C_Start();
|
|
OLED_I2C_SendByte(0x78);
|
|
OLED_I2C_SendByte(0x00);
|
|
OLED_I2C_SendByte(Command);
|
|
OLED_I2C_Stop();
|
|
}
|
|
|
|
/* 函数作用:向 OLED 写入一个显示数据字节。 */
|
|
void OLED_WriteData(uint8_t Data)
|
|
{
|
|
OLED_I2C_Start();
|
|
OLED_I2C_SendByte(0x78);
|
|
OLED_I2C_SendByte(0x40);
|
|
OLED_I2C_SendByte(Data);
|
|
OLED_I2C_Stop();
|
|
}
|
|
|
|
/* 函数作用:设置 OLED 显存写入光标位置。 */
|
|
void OLED_SetCursor(uint8_t Y, uint8_t X)
|
|
{
|
|
OLED_WriteCommand(0xB0 | Y);
|
|
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));
|
|
OLED_WriteCommand(0x00 | (X & 0x0F));
|
|
}
|
|
|
|
/* 函数作用:清空 OLED 屏幕显示内容。 */
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 函数作用:在指定行列显示一个 ASCII 字符。 */
|
|
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]);
|
|
}
|
|
}
|
|
|
|
/* 函数作用:从指定行列开始显示字符串。 */
|
|
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]);
|
|
}
|
|
}
|
|
|
|
/* 函数作用:计算整数 X 的 Y 次方,用于数字拆位显示。 */
|
|
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
|
|
{
|
|
uint32_t Result = 1;
|
|
while (Y--)
|
|
{
|
|
Result *= X;
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
/* 函数作用:在 OLED 上显示指定长度的无符号十进制数字。 */
|
|
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');
|
|
}
|
|
}
|
|
|
|
/* 函数作用:在 OLED 上显示带正负号的十进制数字。 */
|
|
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');
|
|
}
|
|
}
|
|
|
|
/* 函数作用:在 OLED 上显示指定长度的十六进制数字。 */
|
|
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');
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 函数作用:在 OLED 上显示指定长度的二进制数字。 */
|
|
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显示一个16*16的汉字
|
|
* @param Line 行位置,范围:1~4 (一个汉字占用2行高度)
|
|
* @param Column 列位置,范围:1~8 (一个汉字占用2个字符宽度)
|
|
* @param num 要显示的汉字在OLED_F16x16_CN字模数组中的索引
|
|
* @retval 无
|
|
*/
|
|
/* 函数作用:按照字模索引在 OLED 上显示一个 16x16 汉字。 */
|
|
void OLED_ShowChinese(uint8_t Line, uint8_t Column, uint8_t num)
|
|
{
|
|
uint8_t i;
|
|
// 计算X坐标:一个汉字宽度为16像素,等于2个8像素的字符宽度
|
|
uint8_t x = (Column - 1) * 16;
|
|
// 计算Y坐标:一个汉字高度为16像素,等于2个8像素的行(页)高度
|
|
uint8_t y = (Line - 1) * 2;
|
|
|
|
// 检查坐标是否越界
|
|
if (x > 127 || y > 7) return;
|
|
|
|
// 显示上半部分 (16*8)
|
|
OLED_SetCursor(y, x);
|
|
for (i = 0; i < 16; i++)
|
|
{
|
|
OLED_WriteData(OLED_F16x16_CN[num][i]);
|
|
}
|
|
|
|
// 显示下半部分 (16*8)
|
|
OLED_SetCursor(y + 1, x);
|
|
for (i = 0; i < 16; i++)
|
|
{
|
|
OLED_WriteData(OLED_F16x16_CN[num][i + 16]);
|
|
}
|
|
}
|
|
|
|
|
|
/* ... OLED_Init() 函数保持不变 ... */
|
|
/* 函数作用:初始化 OLED 控制器参数并清屏。 */
|
|
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();
|
|
}
|