STM32温湿度OLED显示

📅 发布时间:2026/7/9 13:27:07
STM32温湿度OLED显示 面包板布局建议:把 STM32 插中间,两侧红蓝电源轨分别接 3.3V 和 GND,OLED 和 AHT10 各占一侧,SDA/SCL 用短跳线到 PB8/PB9 汇合。二、完整 Keil 工程代码工程基于STM32 标准外设库 (StdPeriph_Lib),主频 72MHz。目录结构Project/├── User/│ ├── main.c│ ├── delay.c / delay.h│ ├── i2c_soft.c / i2c_soft.h│ ├── oled.c / oled.h│ ├── oledfont.h│ └── aht10.c / aht10.h└── ...标准库1.delay.h#ifndef __DELAY_H #define __DELAY_H #include "stm32f10x.h" void Delay_Init(void); void Delay_us(uint32_t us); void Delay_ms(uint32_t ms); #endif2.delay.c#include "delay.h" static uint32_t fac_us = 0; static uint32_t fac_ms = 0; void Delay_Init(void) { SysTick-CTRL = ~(1 2); // 选择外部时钟 HCLK/8 = 9MHz fac_us = SystemCoreClock / 8000000; // 9 fac_ms = fac_us * 1000; } void Delay_us(uint32_t us) { uint32_t temp; SysTick-LOAD = us * fac_us; SysTick-VAL = 0x00; SysTick-CTRL |= 0x01; do { temp = SysTick-CTRL; } while ((temp 0x01) !(temp (1 16))); SysTick-CTRL = ~0x01; SysTick-VAL = 0x00; } void Delay_ms(uint32_t ms) { while (ms--) Delay_us(1000); }3.i2c_soft.h#ifndef __I2C_SOFT_H #define __I2C_SOFT_H #include "stm32f10x.h" #define I2C_SCL_PIN GPIO_Pin_8 #define I2C_SDA_PIN GPIO_Pin_9 #define I2C_GPIO_PORT GPIOB #define I2C_GPIO_CLK RCC_APB2Periph_GPIOB #define SCL_H GPIO_SetBits(I2C_GPIO_PORT, I2C_SCL_PIN) #define SCL_L GPIO_ResetBits(I2C_GPIO_PORT, I2C_SCL_PIN) #define SDA_H GPIO_SetBits(I2C_GPIO_PORT, I2C_SDA_PIN) #define SDA_L GPIO_ResetBits(I2C_GPIO_PORT, I2C_SDA_PIN) #define SDA_READ GPIO_ReadInputDataBit(I2C_GPIO_PORT, I2C_SDA_PIN) void IIC_Init(void); void IIC_Start(void); void IIC_Stop(void); uint8_t IIC_WaitAck(void); void IIC_Ack(void); void IIC_NAck(void); void IIC_SendByte(uint8_t byte); uint8_t IIC_ReadByte(uint8_t ack); #endif4.i2c_soft.c#include "i2c_soft.h" #include "delay.h" static void SDA_OUT(void) { GPIO_InitTypeDef g; g.GPIO_Pin = I2C_SDA_PIN; g.GPIO_Mode = GPIO_Mode_Out_OD; // 开漏 g.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(I2C_GPIO_PORT, g); } static void SDA_IN(void) { GPIO_InitTypeDef g; g.GPIO_Pin = I2C_SDA_PIN; g.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(I2C_GPIO_PORT, g); } void IIC_Init(void) { GPIO_InitTypeDef g; RCC_APB2PeriphClockCmd(I2C_GPIO_CLK, ENABLE); g.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN; g.GPIO_Mode = GPIO_Mode_Out_OD; g.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(I2C_GPIO_PORT, g); SCL_H; SDA_H; } void IIC_Start(void) { SDA_OUT(); SDA_H; SCL_H; Delay_us(4); SDA_L; Delay_us(4); SCL_L; } void IIC_Stop(void) { SDA_OUT(); SCL_L; SDA_L; Delay_us(4); SCL_H; SDA_H; Delay_us(4); } uint8_t IIC_WaitAck(void) { uint16_t t = 0; SDA_IN(); SDA_H; Delay_us(1); SCL_H; Delay_us(1); while (SDA_READ) { if (++t 250) { IIC_Stop(); return 1; } } SCL_L; return 0; } void IIC_Ack(void) { SCL_L; SDA_OUT(); SDA_L; Delay_us(2); SCL_H; Delay_us(2); SCL_L; } void IIC_NAck