stm32f103c8t6OLED显示温湿度代码
时间: 2023-07-10 18:21:09 浏览: 83
以下是使用STM32F103C8T6和OLED屏幕显示温湿度的代码示例:
```c
#include "stm32f10x.h"
#include "dht11.h"
#include "OLED_IIC.h"
#include "delay.h"
#include "stdlib.h"
#define DHT11_GPIO GPIOB
#define DHT11_PIN GPIO_Pin_10
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Configuration(void);
void TIM_Configuration(void);
void OLED_Display_Temp_Humi(float temp, float humi);
volatile uint32_t TimingDelay;
int main(void)
{
SysTick_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
TIM_Configuration();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"Temperature/Humidity");
while(1)
{
float temp, humi;
if(DHT11_Read_Data(DHT11_GPIO, DHT11_PIN, &temp, &humi) == SUCCESS)
{
OLED_Display_Temp_Humi(temp, humi);
}
Delay_ms(2000);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_GPIO, &GPIO_InitStructure);
GPIO_SetBits(DHT11_GPIO, DHT11_PIN);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void SysTick_Configuration(void)
{
if(SysTick_Config(SystemCoreClock / 1000))
{
while(1);
}
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
}
void OLED_Display_Temp_Humi(float temp, float humi)
{
char temp_str[10], humi_str[10];
gcvt(temp, 2, temp_str);
gcvt(humi, 2, humi_str);
OLED_ShowString(0,20,"Temp:");
OLED_ShowString(32,20,temp_str);
OLED_ShowChar(96,20,'C',8);
OLED_ShowString(0,40,"Humi:");
OLED_ShowString(32,40,humi_str);
OLED_ShowChar(96,40,'%',8);
}
void Delay_ms(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_SendData(USART1, USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
其中,dht11.h和OLED_IIC.h是DHT11温湿度传感器和OLED屏幕的库文件。注意修改DHT11_GPIO和DHT11_PIN为实际使用的GPIO和引脚号。