用IAR写出STM32利用定时器4中断处理功能,实现每隔2s,读取实现将温湿度传感器DHT11的值上传到串口小助手
时间: 2024-03-13 13:42:27 浏览: 69
STM32温湿度传感器DHT11代码,通过串口上传
3星 · 编辑精心推荐
好的,以下是用IAR写出STM32利用定时器4中断处理功能,实现每隔2s,读取实现将温湿度传感器DHT11的值上传到串口小助手的示例代码:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define DHT11_PIN GPIO_Pin_0
#define DHT11_PORT GPIOA
uint8_t DHT11_Data[5];
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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_PORT, &GPIO_InitStructure);
}
void Delay_us(uint32_t n)
{
for (; n > 0; n--)
{
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
}
}
void DHT11_Start(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
GPIO_SetBits(DHT11_PORT, DHT11_PIN);
Delay_us(1000);
GPIO_ResetBits(DHT11_PORT, DHT11_PIN);
Delay_us(18000);
GPIO_SetBits(DHT11_PORT, DHT11_PIN);
}
uint8_t DHT11_Check_Response(void)
{
uint8_t Response = 0;
uint16_t Time_Counter = 0;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
Time_Counter++;
Delay_us(1);
if (Time_Counter > 1000)
{
return 1;
}
}
Time_Counter = 0;
while (!GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
Time_Counter++;
Delay_us(1);
if (Time_Counter > 1000)
{
return 1;
}
}
Time_Counter = 0;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
Time_Counter++;
Delay_us(1);
if (Time_Counter > 1000)
{
return 1;
}
}
return 0;
}
uint8_t DHT11_Read_Byte(void)
{
uint8_t i, j;
uint8_t Data = 0;
for (j = 0; j < 8; j++)
{
while (!GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
;
Delay_us(40);
if (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
Data |= (1 << (7 - j));
}
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
;
}
return Data;
}
void DHT11_Read_Data(void)
{
uint8_t i;
DHT11_Start();
if (DHT11_Check_Response())
{
printf("DHT11 Check Response Failed\r\n");
return;
}
for (i = 0; i < 5; i++)
{
DHT11_Data[i] = DHT11_Read_Byte();
}
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 1999;
TIM_TimeBaseStructure.TIM_Prescaler = 35999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
DHT11_Read_Data();
printf("Temperature: %dC Humidity: %d%%\r\n", DHT11_Data[2], DHT11_Data[0]);
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
TIM_Configuration();
while (1)
;
}
```
其中,我们使用定时器4产生2s的中断,中断处理函数 `TIM4_IRQHandler()` 中读取 DHT11 的数据,并通过串口 `USART1` 将数据上传到串口小助手。需要注意的是,在读取 DHT11 数据时,需要先发送起始信号,然后进行数据的接收和解析,具体细节请参考代码中的注释。
阅读全文