野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1248|回复: 0

野火RA6M5开发板 DHT11温湿度传感器 OLED显示测试学习

[复制链接]
发表于 2023-5-26 19:41:22 | 显示全部楼层 |阅读模式
DHT11温湿度传感器
[color=rgba(0, 0, 0, 0.87)]DHT11是一款有已校准数字信号输出的温湿度传感器。 精度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃。
[color=rgba(0, 0, 0, 0.87)]更多DHT11信息请参考:[color=var(--color-primary)]https://baike.sogou.com/v73984313.htm?fromTitle=DHT11
[color=rgba(0, 0, 0, 0.87)]下图为DHT11的引脚说明图,DATA引脚为信号输入输出。
[color=rgba(0, 0, 0, 0.87)]
[color=rgba(0, 0, 0, 0.87)]在编写DHT11驱动时,要考虑更改硬件环境的情况。我们把DHT11信号引脚相关的宏定义到 “bsp_dht11.h”文件中, 这样的话在更改或移植的时候只用改宏定义就可以。
  1. #ifndef __BSP_DHT11_H
  2. #define __BSP_DHT11_H
  3. #include "hal_data.h"


  4. #define Bit_RESET   0
  5. #define Bit_SET     1

  6. #define DHT11_LOW   0
  7. #define DHT11_HIGH  1

  8. #define DHT11_PORT  BSP_IO_PORT_00_PIN_01
  9. #define DHT_HIGH    R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_HIGH);
  10. #define DHT_LOW     R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_LOW);
  11. #define Read_Data   R_BSP_PinRead(DHT11_PORT)

  12. #define DHT11_DATA_OUT(a)   if (a)  \ DHT_HIGH\ else \  DHT_LOW

  13. typedef struct
  14. {
  15.     uint8_t  humi_int;      //湿度的整数部分
  16.     uint8_t  humi_deci;     //湿度的小数部分
  17.     uint8_t  temp_int;      //温度的整数部分
  18.     uint8_t  temp_deci;     //温度的小数部分
  19.     uint8_t  check_sum;     //校验和
  20. }DHT11_Data_TypeDef;

  21. void DHT11_Init         (void);
  22. void DHT11_Start        (void);
  23. void DHT11_DELAY_US     (uint32_t delay);
  24. void DHT11_DELAY_MS     (uint32_t delay);
  25. uint8_t Read_DHT11(DHT11_Data_TypeDef *DHT11_Data);

  26. #endif

复制代码
  1. #include "bsp_dht11.h"             // Device header


  2. /* DHT11初始化函数 */
  3. void DHT11_Init(void)
  4. {
  5.     /* 初始化配置引脚(这里重复初始化了,可以注释掉) */
  6.     R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
  7. }

  8. void DHT11_DELAY_US(uint32_t delay)
  9. {
  10.     R_BSP_SoftwareDelay(delay, BSP_DELAY_UNITS_MICROSECONDS);
  11. }

  12. void DHT11_DELAY_MS(uint32_t delay)
  13. {
  14.     R_BSP_SoftwareDelay(delay, BSP_DELAY_UNITS_MILLISECONDS);
  15. }

  16. //主机发送开始信号
  17. void DHT11_Start(void)
  18. {

  19.     DHT_HIGH; //先拉高
  20.     DHT11_DELAY_US(30);

  21.     DHT_LOW; //拉低电平至少18us
  22.     DHT11_DELAY_MS(20);

  23.     DHT_HIGH; //拉高电平20~40us
  24.     DHT11_DELAY_US(30);


  25. }

  26. /*
  27. * 从DHT11读取一个字节,MSB先行
  28. */
  29. static uint8_t Read_Byte(void)
  30. {
  31.     uint8_t i, temp=0;

  32.     for(i=0;i<8;i++)
  33.     {
  34.         /*每bit以50us低电平标置开始,轮询直到从机发出 的50us 低电平 结束*/
  35.         while(Read_Data == Bit_RESET);

  36.         /*DHT11 以26~28us的高电平表示“0”,以70us高电平表示“1”,
  37.          *通过检测 x us后的电平即可区别这两个状 ,x 即下面的延时
  38.          */
  39.         DHT11_DELAY_US(40); //延时x us 这个延时需要大于数据0持续的时间即可

  40.         if(Read_Data == Bit_SET)/* x us后仍为高电平表示数据“1” */
  41.         {
  42.             /* 等待数据1的高电平结束 */
  43.             while( Read_Data ==Bit_SET);

  44.             temp|=(uint8_t)(0x01<<(7-i));  //把第7-i位置1,MSB先行
  45.         }
  46.         else     // x us后为低电平表示数据“0”
  47.         {
  48.             temp&=(uint8_t)~(0x01<<(7-i)); //把第7-i位置0,MSB先行
  49.         }
  50.     }
  51.     return temp;
  52. }


  53. /*
  54. * 一次完整的数据传输为40bit,高位先出
  55. * 8bit 湿度整数 + 8bit 湿度小数 + 8bit 温度整数 + 8bit 温度小数 + 8bit 校验和
  56. */
  57. uint8_t Read_DHT11(DHT11_Data_TypeDef *DHT11_Data)
  58. {
  59.     uint16_t count;
  60.     DHT11_Start();
  61.     DHT_HIGH; //拉高电平

  62.     /*判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行*/
  63.     if( Read_Data == Bit_RESET)
  64.     {
  65.     count=0;
  66.         /*轮询直到从机发出 的80us 低电平 响应信号结束*/
  67.         while( Read_Data ==Bit_RESET)
  68.         {
  69.             count++;
  70.             if(count>1000)
  71.                     return 0;
  72.             DHT11_DELAY_US(10);
  73.         }

  74.     count=0;
  75.         /*轮询直到从机发出的 80us 高电平 标置信号结束*/
  76.         while( Read_Data==Bit_SET)
  77.         {
  78.                 count++;
  79.                 if(count>1000)
  80.                     return 0;
  81.                 DHT11_DELAY_US(10);
  82.         }
  83.         /*开始接收数据*/
  84.         DHT11_Data->humi_int= Read_Byte();

  85.         DHT11_Data->humi_deci= Read_Byte();

  86.         DHT11_Data->temp_int= Read_Byte();

  87.         DHT11_Data->temp_deci= Read_Byte();

  88.         DHT11_Data->check_sum= Read_Byte();

  89.         DHT_LOW;
  90.         DHT11_DELAY_US(55);
  91.         DHT_HIGH;

  92.         /*检查读取的数据是否正确*/
  93.         if(DHT11_Data->check_sum == DHT11_Data->humi_int + DHT11_Data->humi_deci + DHT11_Data->temp_int+ DHT11_Data->temp_deci)
  94.             return 1;
  95.         else
  96.             return 0;
  97.     }
  98.     else
  99.     {
  100.         return 0;
  101.     }
  102. }

复制代码
  1. //--------------------------------------------------------------------------------------------------
  2. // 函数头文件    |   0   |   1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9
  3. //--------------------------------------------------------------------------------------------------
  4. #include "hal_data.h"
  5. #include "oled.h"
  6. #include "bmp.h"
  7. #include "bsp_debug_uart.h"
  8. #include "bsp_led.h"
  9. #include "bsp_dht11.h"
  10. #include "bsp_gpt_timing.h"

  11. #define SUCCESS 1

  12. void Hardware_init(void);
  13. FSP_CPP_HEADER
  14. void R_BSP_WarmStart(bsp_warm_start_event_t event);
  15. FSP_CPP_FOOTER

  16. /* Callback function */
  17. i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
  18. void sci_i2c_master_callback(i2c_master_callback_args_t *p_args)
  19. {
  20.     i2c_event = I2C_MASTER_EVENT_ABORTED;
  21.     if (NULL != p_args)
  22.     {
  23.         /* capture callback event for validating the i2c transfer event*/
  24.         i2c_event = p_args->event;
  25.     }

  26. }

  27. fsp_err_t err = FSP_SUCCESS;
  28. uint32_t  timeout_ms = 1000;
  29. DHT11_Data_TypeDef DHT11_Data;
  30. uint8_t Temperature,Humidity;
  31. extern uint8_t temp_humi_flag;

  32. //==================================================================================================
  33. //  函数说明: 硬件初始化
  34. //  函数备注: Hardware_init
  35. //--------------------------------------------------------------------------------------------------
  36. //  |   -   |   -   |   0   |   1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9
  37. //==================================================================================================
  38. void Hardware_init(void)
  39. {
  40.         Debug_UART4_Init(); // SCI4 UART 调试串口初始化
  41.         GPT0_Timing_Init();
  42.         printf("Debug-UART4-Init OK \r\n");
  43.         LED_Init();
  44.         printf("LED_Init    OK \r\n");
  45.         printf("IIC-Config  Start \r\n");
  46.         DHT11_Init();
  47.         printf("DHT11_Init OK \r\n");
  48.         /* Initialize the I2C module */
  49.         err = R_SCI_I2C_Open(&g_i2c5_ctrl, &g_i2c5_cfg);
  50.         /* Handle any errors. This function should be defined by the user. */
  51.         assert(FSP_SUCCESS == err);
  52.         printf("IIC-Config OK \r\n");
  53.         OLED_Init();            //初始化OLED
  54.         OLED_Clear();
  55.         printf("oled-Init OK \r\n");

  56. }
  57. //==================================================================================================
  58. //  函数说明: 主函数入口
  59. //  函数备注: hal_entry
  60. //--------------------------------------------------------------------------------------------------
  61. //  |   -   |   -   |   0   |   1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9
  62. //==================================================================================================

  63. /*******************************************************************************************************************//**
  64. * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
  65. * is called by main() when no RTOS is used.
  66. **********************************************************************************************************************/
  67. void hal_entry(void)
  68. {
  69.     /* TODO: add your own code here */

  70.           Hardware_init();
  71.           printf("RA6M5-Board-Init OK \r\n");

  72.           R_BSP_PinAccessEnable();
  73.           R_BSP_PinWrite(BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_HIGH); //DHT11端口配置
  74.           while(1)
  75.           {

  76.               /*调用DHT11_Read_TempAndHumidity读取温湿度,若成功则输出该信息*/
  77.               if( Read_DHT11 ( & DHT11_Data ) == SUCCESS )
  78.               {
  79.                   Temperature =  DHT11_Data.temp_int;
  80.                   Humidity    =  DHT11_Data.humi_int;

  81.               }
  82.              OLED_ShowCHinese(0,0,0);//瑞
  83.              OLED_ShowCHinese(16,0,1);//萨
  84.              OLED_ShowCHinese(32,0,5);//电
  85.              OLED_ShowCHinese(48,0,6);//子
  86.              OLED_ShowString(60,0,"Renesas",16);
  87.              OLED_ShowNum(0,2,2023,4,16);//显示ASCII字符的码值
  88.              OLED_ShowCHinese(32,2,2);//中文字->年
  89.              OLED_ShowNum(48,2,5,2,16);//显示ASCII字符的码值
  90.              OLED_ShowCHinese(64,2,3);//中文字->月
  91.              OLED_ShowNum(80,2,26,2,16);//显示ASCII字符的码值
  92.              OLED_ShowCHinese(96,2,4);//中文字->日

  93.              OLED_ShowCHinese(0,4,7);   //中文字->温
  94.              OLED_ShowCHinese(16,4,9);  //中文字->度
  95.              OLED_ShowNum(32,4,Temperature,2,16);
  96.              OLED_ShowCHinese(64,4,8);  //中文字->湿
  97.              OLED_ShowCHinese(80,4,9);  //中文字->度
  98.              OLED_ShowNum(96,4,Humidity,2,16);
  99.              OLED_ShowString(0,6,"RA6M5-Embedfire",16);

  100.           }



  101. #if BSP_TZ_SECURE_BUILD
  102.     /* Enter non-secure code */
  103.     R_BSP_NonSecureEnter();
  104. #endif
  105. }

  106. /*******************************************************************************************************************//**
  107. * This function is called at various points during the startup process.  This implementation uses the event that is
  108. * called right before main() to set up the pins.
  109. *
  110. * @param[in]  event    Where at in the start up process the code is currently at
  111. **********************************************************************************************************************/
  112. void R_BSP_WarmStart(bsp_warm_start_event_t event)
  113. {
  114.     if (BSP_WARM_START_RESET == event)
  115.     {
  116. #if BSP_FEATURE_FLASH_LP_VERSION != 0

  117.         /* Enable reading from data flash. */
  118.         R_FACI_LP->DFLCTL = 1U;

  119.         /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
  120.          * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
  121. #endif
  122.     }

  123.     if (BSP_WARM_START_POST_C == event)
  124.     {
  125.         /* C runtime environment and system clocks are setup. */

  126.         /* Configure pins. */
  127.         R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
  128.     }
  129. }

  130. #if BSP_TZ_SECURE_BUILD

  131. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

  132. /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
  133. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
  134. {

  135. }
  136. #endif

复制代码
测试效果图片
Snipaste_2023-05-26_19-40-11.png
Snipaste_2023-05-26_19-40-25.png



回复

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-26 01:10 , Processed in 0.082377 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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