基于stm32F407的温湿度采集(dht11)

自己写的小程序,随手一水。

DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,它应用专用的数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性和卓越的长期稳定性。传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一个高性能8位单片机相连接。因此该产品具有品质卓越、超快响应、抗干扰能力强、性价比极高等优点。每个DHT11传感器都在极为精确的湿度校验室中进行校准。校准系数以程序的形式存在OTP内存中,传感器内部在检测信号的处理过程中要调用这些校准系数。单线制串行接口,使系统集成变得简易快捷。超小的体积、极低的功耗,使其成为该类应用中,在苛刻应用场合的最佳选择。产品为4针单排引脚封装,连接方便。
DHT11是一款有已校准数字信号输出的温湿度传感器。 其精度湿度±5%RH, 温度±2℃,量程湿度5~95%RH, 温度-20~+60℃。

正文:这传感器练手用可以,误差太大,实测湿度数据串没有小数位,温度也不是很精确,冗余数字做校验用,时序图百度很详细。这款传感器总体来说不适用于工业控制系统,对工作环境要求比较高。需要精细采集的请选用DHT22。

程序:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

#include "stdio.h"
#define USS (uint16_t)35
uint8_t wendu;
	uint8_t shidu;
	uint8_t jiaoyan;
	uint8_t arr[40]; 
int fputc(int c,FILE *f)
{
	uint8_t ch;
	ch = c;
	HAL_UART_Transmit(&huart1,&ch,1,1000);
	return c;
}
void TIM6_delay_us(uint16_t us)
{
    // 配置定时器时钟
    RCC->APB1ENR |= RCC_APB1ENR_TIM6EN; // 使能TIM6的时钟
    // 设置预分频器
    TIM6->PSC = 71;  // 预分频器的值为72-1,将计时频率设置为1MHz

    // 设置自动重装载寄存器
    TIM6->ARR = us - 1; // 计算自动重装载值,根据延时的微秒数来设置

    // 启动定时器
    TIM6->CR1 |= TIM_CR1_CEN; // 启动计时器,使其开始计数

    // 等待定时器计数完成
    while ((TIM6->SR & TIM_SR_UIF) == 0); // 等待计数完成,等待计数器溢出标志位被设置

    // 清除中断标志
    TIM6->SR &= ~TIM_SR_UIF; // 清除计数器溢出标志位

    // 停止定时器
    TIM6->CR1 &= ~TIM_CR1_CEN; // 停止计时器,禁止计数

    // 关闭定时器时钟
    RCC->APB1ENR &= ~RCC_APB1ENR_TIM6EN; // 关闭TIM6的时钟
}
uint8_t caiji(uint8_t *shidu,uint8_t *wendu,uint8_t *jiaoyan)
{
	/
	HAL_Delay(2000);//等待传感器越过平稳状态
	GPIOF->MODER &=~ ( (0x03) << (2*0) );//设置推挽
	GPIOF->MODER |= ( (0x01) << (2*0) );
	
	HAL_GPIO_WritePin(GPIOF,GPIO_PIN_0,GPIO_PIN_RESET);//拉低
	HAL_Delay(20);
	HAL_GPIO_WritePin(GPIOF,GPIO_PIN_0,GPIO_PIN_SET);//拉高
	TIM6_delay_us(50);
///	
	GPIOF->MODER &=~ ( (0x03) << (2*0) );
	while((GPIOF->IDR&1)==1);//越过拉高等待
	while((GPIOF->IDR&1)==0);//越过响应信号
	while((GPIOF->IDR&1)==1);//越过拉高延时
	while((GPIOF->IDR&1)==0);//越过第一个数据的低电平信号
	
	for(uint8_t i=0;i<40;i++)
	{
	  TIM6_delay_us(USS);
	if((GPIOF->IDR&1)==0)
		{
		arr[i]=0;		
		}
		else
		{
		arr[i]=1;
		}
		while((GPIOF->IDR&1)==1);
		while((GPIOF->IDR&1)==0);
	}
	*shidu=0;
	*wendu=0;
	for(uint8_t i=0;i<8;i++)
	{
		*shidu=(*shidu)*2+arr[i];
	}
	for(uint8_t i=16;i<24;i++)
	{
		*wendu=(*wendu)*2+arr[i];
	}
	for(uint8_t i=24;i<32;i++)
	{
		*jiaoyan=(*jiaoyan)*2+arr[i];
	}
	return 1;
}
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    caiji(&shidu,&wendu,&jiaoyan);
		for(uint8_t i=0;i<8;i++)
		{
		printf("%d",arr[i]);
		}
		putchar('\n');
		for(uint8_t i=8;i<16;i++)
		{
		printf("%d",arr[i]);
		}
		putchar('\n');
		for(uint8_t i=16;i<24;i++)
		{
		printf("%d",arr[i]);
		}
		putchar('\n');
		for(uint8_t i=24;i<32;i++)
		{
		printf("%d",arr[i]);
		}
		putchar('\n');
		for(uint8_t i=32;i<40;i++)
		{
		printf("%d",arr[i]);
		}
		putchar('\n');
		printf("当前湿度%d%% 当前温度%d.%d°C \n",shidu,wendu,jiaoyan);
		HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 144;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
		
		
		
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

需要代码的小伙伴自取,所有函数都在这里,没有分文件编程。
串口转usb数据传电脑上了,没有写液晶控制。
技术支持:QQ296199797(FOR FREE)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
STM32F407单片机读写OneWire_DHT11温湿度传感器(串口屏显示)软件工程源码,可以做为你的学习设计参考。 int main(void) { /* 复位所有外设,初始化Flash接口和系统滴答定时器 */ HAL_Init(); /* 配置系统时钟 */ SystemClock_Config(); /* 初始化串口并配置串口中断优先级 */ MX_DEBUG_USART_Init(); HMI_USARTx_Init(); DHT11_Init(); /* 无限循环 */ while (1) { /*调用DHT11_Read_TempAndHumidity读取温湿度,若成功则输出该信息*/ if(DHT11_Read_TempAndHumidity(&DHT11_Data)==SUCCESS) { HMI_value_setting("page1.gross.val",DHT11_Data.humidity*10); HMI_value_setting("page1.net.val",DHT11_Data.temperature*10); printf("读取DHT11成功!-->湿度为%.1f %RH ,温度为 %.1f℃ \n",DHT11_Data.humidity,DHT11_Data.temperature); } else { printf("读取DHT11信息失败\n"); } HAL_Delay(1000); } } /** * 函数功能: 向串口屏发送数据 * 输入参数: 无 * 返 回 值: 无 * 说 明: 无 */ void HMI_value_setting(const char *val_str,uint32_t value) { uint8_t tmp_str[30]={0}; uint8_t i; sprintf((char *)tmp_str,"%s=%d",val_str,value); for(i=0;iDR=tmp_str[i]; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } /** * 函数功能: 向串口屏发送浮点数据 * 输入参数: 无 * 返 回 值: 无 * 说 明: 无 */ void HMI_string_setting(const char *val_str,int32_t value) { uint8_t tmp_str[50]={0}; uint8_t i; float temp=(float)value; sprintf((char *)tmp_str,"%s=\"%.1f\"",val_str,temp/100); for(i=0;iDR=tmp_str[i]; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } HMI_USARTx->DR=0xFF;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值