找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3557|回复: 4
打印 上一主题 下一主题
收起左侧

stm32 DHT11数字温湿度传感器源程序

[复制链接]
跳转到指定楼层
楼主
附件是DHT1的温湿度传感器代码 分享给大家1、首先 将USB转串口与开发板相连,如图,再与电脑相连
2、将DHT11模块插入开发板
3、打开串口助手,设置波特率为115200,下载DHT11程序后可以看。


单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32l4xx_hal.h"
  4. #include "usart.h"
  5. #include "gpio.h"
  6. #include "dht11.h"

  7. /* USER CODE BEGIN Includes */

  8. /* USER CODE END Includes */

  9. /* Private variables ---------------------------------------------------------*/

  10. /* USER CODE BEGIN PV */
  11. /* Private variables ---------------------------------------------------------*/

  12. /* USER CODE END PV */

  13. /* Private function prototypes -----------------------------------------------*/
  14. void SystemClock_Config(void);

  15. /* USER CODE BEGIN PFP */
  16. /* Private function prototypes -----------------------------------------------*/

  17. /* USER CODE END PFP */

  18. /* USER CODE BEGIN 0 */

  19. /* USER CODE END 0 */

  20. /**
  21.   * @brief  The application entry point.
  22.   *
  23.   * @retval None
  24.   */
  25. int main(void)
  26. {
  27.   /* USER CODE BEGIN 1 */
  28.         uint8_t str[] = "dht11 test\r\n";
  29.         DHT11_Data_TypeDef dht11;
  30.   /* USER CODE END 1 */

  31.   /* MCU Configuration----------------------------------------------------------*/

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

  34.   /* USER CODE BEGIN Init */

  35.   /* USER CODE END Init */

  36.   /* Configure the system clock */
  37.   SystemClock_Config();

  38.   /* USER CODE BEGIN SysInit */

  39.   /* USER CODE END SysInit */

  40.   /* Initialize all configured peripherals */
  41.   MX_GPIO_Init();
  42.   MX_USART1_UART_Init();
  43.   /* USER CODE BEGIN 2 */
  44.         HAL_UART_Transmit(&huart1, str, 12,100);
  45.         DHT11_Init();
  46.        
  47.   /* USER CODE END 2 */

  48.   /* Infinite loop */
  49.   /* USER CODE BEGIN WHILE */
  50.   while (1)
  51.   {

  52.   /* USER CODE END WHILE */
  53.                 DHT11_Read_TempAndHumidity(&dht11);
  54.                 printf("temp = %.2f ; humi = %.2f\r\n",dht11.temperature,dht11.humidity);
  55.                 HAL_Delay(3000);
  56.   /* USER CODE BEGIN 3 */

  57.   }
  58.   /* USER CODE END 3 */

  59. }

  60. /**
  61.   * @brief System Clock Configuration
  62.   * @retval None
  63.   */
  64. void SystemClock_Config(void)
  65. {

  66.   RCC_OscInitTypeDef RCC_OscInitStruct;
  67.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  68.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  69.     /**Initializes the CPU, AHB and APB busses clocks
  70.     */
  71.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  72.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  73.   RCC_OscInitStruct.MSICalibrationValue = 0;
  74.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  75.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  76.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  77.   RCC_OscInitStruct.PLL.PLLM = 1;
  78.   RCC_OscInitStruct.PLL.PLLN = 40;
  79.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  80.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  81.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  82.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  83.   {
  84.     _Error_Handler(__FILE__, __LINE__);
  85.   }

  86.     /**Initializes the CPU, AHB and APB busses clocks
  87.     */
  88.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  89.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  90.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  91.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  92.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  93.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  94.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  95.   {
  96.     _Error_Handler(__FILE__, __LINE__);
  97.   }

  98.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  99.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  100.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  101.   {
  102.     _Error_Handler(__FILE__, __LINE__);
  103.   }

  104.     /**Configure the main internal regulator output voltage
  105.     */
  106.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  107.   {
  108.     _Error_Handler(__FILE__, __LINE__);
  109.   }

  110.     /**Configure the Systick interrupt time
  111.     */
  112.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  113.     /**Configure the Systick
  114.     */
  115.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  116.   /* SysTick_IRQn interrupt configuration */
  117.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  118. }

  119. /* USER CODE BEGIN 4 */

  120. /* USER CODE END 4 */

  121. /**
  122.   * @brief  This function is executed in case of error occurrence.
  123.   * @param  file: The file name as string.
  124.   * @param  line: The line in file as a number.
  125.   * @retval None
  126.   */
  127. void _Error_Handler(char *file, int line)
  128. {
  129.   /* USER CODE BEGIN Error_Handler_Debug */
  130.   /* User can add his own implementation to report the HAL error return state */
  131.   while(1)
  132.   {
  133.   }
  134.   /* USER CODE END Error_Handler_Debug */
  135. }

  136. #ifdef  USE_FULL_ASSERT
  137. /**
  138.   * @brief  Reports the name of the source file and the source line number
  139.   *         where the assert_param error has occurred.
  140.   * @param  file: pointer to the source file name
  141.   * @param  line: assert_param error line source number
  142.   * @retval None
  143.   */
  144. void assert_failed(uint8_t* file, uint32_t line)
  145. {
  146.   /* USER CODE BEGIN 6 */
  147.   /* User can add his own implementation to report the file name and line number,
  148.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  149.   /* USER CODE END 6 */
  150. }
  151. #endif /* USE_FULL_ASSERT */

  152. /**
  153.   * @}
  154.   */

  155. /**
  156.   * @}
  157.   */

  158. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
DHT11.7z (3.95 MB, 下载次数: 73)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:63317 发表于 2019-12-2 16:49 | 只看该作者

感谢分享 下载了
回复

使用道具 举报

板凳
ID:616896 发表于 2019-12-4 16:41 | 只看该作者
感谢分享
回复

使用道具 举报

地板
ID:425090 发表于 2019-12-7 10:31 | 只看该作者
感谢分享
回复

使用道具 举报

5#
ID:701902 发表于 2020-3-4 13:02 | 只看该作者
感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表