This commit is contained in:
Zengtudor 2025-04-12 09:47:48 +08:00
parent 3ee23ec1d5
commit 8199ee4c5d
2 changed files with 91 additions and 90 deletions

View File

@ -1,4 +1,5 @@
#include "Delay.hpp" #include "Delay.hpp"
#include "stm32f10x_gpio.h"
#include "ztLed.hpp" #include "ztLed.hpp"
#include "ztOLED.hpp" #include "ztOLED.hpp"
@ -7,10 +8,10 @@ int main(){
zt::Led ledc13(RCC_APB2Periph_GPIOC,GPIO_Pin_13,GPIOC); zt::Led ledc13(RCC_APB2Periph_GPIOC,GPIO_Pin_13,GPIOC);
zt::Oled oled(RCC_APB2Periph_GPIOB,GPIOB,GPIO_Pin_8,GPIO_Pin_9); zt::Oled oled(RCC_APB2Periph_GPIOB,GPIOB,GPIO_Pin_8,GPIO_Pin_9);
oled.OLED_ShowString(1, 1, (char*)"Hello,world!") oled.ShowString(1, 1, "Hello,world!")
.OLED_ShowString(2, 1, (char*)"by Zengtudor") .ShowString(2, 1, "by Zengtudor")
.OLED_ShowString(3, 1, (char*)"From BDFS") .ShowString(3, 1, "From BDFS")
.OLED_ShowString(4, 1, (char*)" EEC-RPD"); .ShowString(4, 1, " EEC-RPD");
u32 delayTime = 500; u32 delayTime = 500;
while(true){ while(true){

View File

@ -13,12 +13,12 @@ namespace zt{
const u16 _GPIO_PIN_SCL; const u16 _GPIO_PIN_SCL;
const u16 _GPIO_PIN_SDA; const u16 _GPIO_PIN_SDA;
Oled&OLED_W_SCL(const u8 &bit){ Oled&W_SCL(const u8 &bit){
GPIO_WriteBit(_GPIOx, _GPIO_PIN_SCL, (BitAction)(bit)); GPIO_WriteBit(_GPIOx, _GPIO_PIN_SCL, (BitAction)(bit));
return *this; return *this;
} }
Oled&OLED_W_SDA(const u8 &bit){ Oled&W_SDA(const u8 &bit){
GPIO_WriteBit(_GPIOx, _GPIO_PIN_SDA, (BitAction)(bit)); GPIO_WriteBit(_GPIOx, _GPIO_PIN_SDA, (BitAction)(bit));
return *this; return *this;
} }
@ -28,66 +28,66 @@ namespace zt{
* @param * @param
* @retval * @retval
*/ */
void OLED_I2C_Start() void I2C_Start()
{ {
OLED_W_SDA(1); W_SDA(1);
OLED_W_SCL(1); W_SCL(1);
OLED_W_SDA(0); W_SDA(0);
OLED_W_SCL(0); W_SCL(0);
} }
/** /**
* @brief I2C停止 * @brief I2C停止
* @param * @param
* @retval * @retval
*/ */
void OLED_I2C_Stop() void I2C_Stop()
{ {
OLED_W_SDA(0); W_SDA(0);
OLED_W_SCL(1); W_SCL(1);
OLED_W_SDA(1); W_SDA(1);
} }
/** /**
* @brief I2C发送一个字节 * @brief I2C发送一个字节
* @param Byte * @param Byte
* @retval * @retval
*/ */
void OLED_I2C_SendByte(const uint8_t &Byte) void I2C_SendByte(const uint8_t &Byte)
{ {
uint8_t i; uint8_t i;
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
OLED_W_SDA(!!(Byte & (0x80 >> i))); W_SDA(!!(Byte & (0x80 >> i)));
OLED_W_SCL(1); W_SCL(1);
OLED_W_SCL(0); W_SCL(0);
} }
OLED_W_SCL(1); //额外的一个时钟,不处理应答信号 W_SCL(1); //额外的一个时钟,不处理应答信号
OLED_W_SCL(0); W_SCL(0);
} }
/** /**
* @brief OLED写命令 * @brief OLED写命令
* @param Command * @param Command
* @retval * @retval
*/ */
void OLED_WriteCommand(uint8_t Command) void WriteCommand(uint8_t Command)
{ {
OLED_I2C_Start(); I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址 I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x00); //写命令 I2C_SendByte(0x00); //写命令
OLED_I2C_SendByte(Command); I2C_SendByte(Command);
OLED_I2C_Stop(); I2C_Stop();
} }
/** /**
* @brief OLED写数据 * @brief OLED写数据
* @param Data * @param Data
* @retval * @retval
*/ */
void OLED_WriteData(uint8_t Data) void WriteData(uint8_t Data)
{ {
OLED_I2C_Start(); I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址 I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x40); //写数据 I2C_SendByte(0x40); //写数据
OLED_I2C_SendByte(Data); I2C_SendByte(Data);
OLED_I2C_Stop(); I2C_Stop();
} }
/** /**
* @brief OLED设置光标位置 * @brief OLED设置光标位置
@ -95,18 +95,18 @@ namespace zt{
* @param X 0~127 * @param X 0~127
* @retval * @retval
*/ */
void OLED_SetCursor(uint8_t Y, uint8_t X) void SetCursor(uint8_t Y, uint8_t X)
{ {
OLED_WriteCommand(0xB0 | Y); //设置Y位置 WriteCommand(0xB0 | Y); //设置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位 WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位 WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
} }
/** /**
* @brief OLED次方函数 * @brief OLED次方函数
* @retval X的Y次方 * @retval X的Y次方
*/ */
uint32_t OLED_Pow(uint32_t X, uint32_t Y) uint32_t Pow(uint32_t X, uint32_t Y)
{ {
uint32_t Result = 1; uint32_t Result = 1;
while (Y--) while (Y--)
@ -124,7 +124,7 @@ namespace zt{
* @param * @param
* @retval * @retval
*/ */
void OLED_Init() void Init()
{ {
uint32_t i, j; uint32_t i, j;
@ -133,47 +133,47 @@ namespace zt{
for (j = 0; j < 1000; j++); for (j = 0; j < 1000; j++);
} }
// OLED_I2C_Init(); //端口初始化 // I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //关闭显示 WriteCommand(0xAE); //关闭显示
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率 WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80); WriteCommand(0x80);
OLED_WriteCommand(0xA8); //设置多路复用率 WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F); WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //设置显示偏移 WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00); WriteCommand(0x00);
OLED_WriteCommand(0x40); //设置显示开始行 WriteCommand(0x40); //设置显示开始行
OLED_WriteCommand(0xA1); //设置左右方向0xA1正常 0xA0左右反置 WriteCommand(0xA1); //设置左右方向0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向0xC8正常 0xC0上下反置 WriteCommand(0xC8); //设置上下方向0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置 WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12); WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度控制 WriteCommand(0x81); //设置对比度控制
OLED_WriteCommand(0xCF); WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //设置预充电周期 WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1); WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别 WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30); WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭 WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/倒转显示 WriteCommand(0xA6); //设置正常/倒转显示
OLED_WriteCommand(0x8D); //设置充电泵 WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14); WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示 WriteCommand(0xAF); //开启显示
OLED_Clear(); //OLED清屏 Clear(); //OLED清屏
} }
public: public:
/** /**
@ -181,15 +181,15 @@ namespace zt{
* @param * @param
* @retval * @retval
*/ */
Oled& OLED_Clear(void) Oled& Clear(void)
{ {
uint8_t i, j; uint8_t i, j;
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
{ {
OLED_SetCursor(j, 0); SetCursor(j, 0);
for(i = 0; i < 128; i++) for(i = 0; i < 128; i++)
{ {
OLED_WriteData(0x00); WriteData(0x00);
} }
} }
return *this; return *this;
@ -201,18 +201,18 @@ namespace zt{
* @param Char ASCII可见字符 * @param Char ASCII可见字符
* @retval * @retval
*/ */
Oled& OLED_ShowChar(uint8_t Line, uint8_t Column, char Char) Oled& ShowChar(uint8_t Line, uint8_t Column, char Char)
{ {
uint8_t i; uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分 SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容 WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
} }
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分 SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容 WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
} }
return *this; return *this;
} }
@ -224,12 +224,12 @@ namespace zt{
* @param String ASCII可见字符 * @param String ASCII可见字符
* @retval * @retval
*/ */
Oled& OLED_ShowString(uint8_t Line, uint8_t Column, char *String) Oled& ShowString(uint8_t Line, uint8_t Column, const char *const String)
{ {
uint8_t i; uint8_t i;
for (i = 0; String[i] != '\0'; i++) for (i = 0; String[i] != '\0'; i++)
{ {
OLED_ShowChar(Line, Column + i, String[i]); ShowChar(Line, Column + i, String[i]);
} }
return *this; return *this;
} }
@ -242,12 +242,12 @@ namespace zt{
* @param Length 1~10 * @param Length 1~10
* @retval * @retval
*/ */
Oled& OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) Oled& ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{ {
uint8_t i; uint8_t i;
for (i = 0; i < Length; i++) for (i = 0; i < Length; i++)
{ {
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0'); ShowChar(Line, Column + i, Number / Pow(10, Length - i - 1) % 10 + '0');
} }
return *this; return *this;
} }
@ -260,23 +260,23 @@ namespace zt{
* @param Length 1~10 * @param Length 1~10
* @retval * @retval
*/ */
Oled& OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length) Oled& ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{ {
uint8_t i; uint8_t i;
uint32_t Number1; uint32_t Number1;
if (Number >= 0) if (Number >= 0)
{ {
OLED_ShowChar(Line, Column, '+'); ShowChar(Line, Column, '+');
Number1 = Number; Number1 = Number;
} }
else else
{ {
OLED_ShowChar(Line, Column, '-'); ShowChar(Line, Column, '-');
Number1 = -Number; Number1 = -Number;
} }
for (i = 0; i < Length; i++) for (i = 0; i < Length; i++)
{ {
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0'); ShowChar(Line, Column + i + 1, Number1 / Pow(10, Length - i - 1) % 10 + '0');
} }
return *this; return *this;
} }
@ -289,19 +289,19 @@ namespace zt{
* @param Length 1~8 * @param Length 1~8
* @retval * @retval
*/ */
Oled& OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) Oled& ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{ {
uint8_t i, SingleNumber; uint8_t i, SingleNumber;
for (i = 0; i < Length; i++) for (i = 0; i < Length; i++)
{ {
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16; SingleNumber = Number / Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10) if (SingleNumber < 10)
{ {
OLED_ShowChar(Line, Column + i, SingleNumber + '0'); ShowChar(Line, Column + i, SingleNumber + '0');
} }
else else
{ {
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A'); ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
} }
} }
return *this; return *this;
@ -315,12 +315,12 @@ namespace zt{
* @param Length 1~16 * @param Length 1~16
* @retval * @retval
*/ */
Oled& OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) Oled& ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{ {
uint8_t i; uint8_t i;
for (i = 0; i < Length; i++) for (i = 0; i < Length; i++)
{ {
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0'); ShowChar(Line, Column + i, Number / Pow(2, Length - i - 1) % 2 + '0');
} }
return *this; return *this;
} }
@ -338,10 +338,10 @@ namespace zt{
GPIO_InitStructure.GPIO_Pin = _GPIO_PIN_SDA; GPIO_InitStructure.GPIO_Pin = _GPIO_PIN_SDA;
GPIO_Init(_GPIOx, &GPIO_InitStructure); GPIO_Init(_GPIOx, &GPIO_InitStructure);
OLED_W_SCL(1); W_SCL(1);
OLED_W_SDA(1); W_SDA(1);
OLED_Init(); Init();
} }
virtual ~Oled(){ virtual ~Oled(){
GPIO_DeInit(_GPIOx); GPIO_DeInit(_GPIOx);