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的引脚说明图,DATA引脚为信号输入输出。 [color=rgba(0, 0, 0, 0.87)] [color=rgba(0, 0, 0, 0.87)]在编写DHT11驱动时,要考虑更改硬件环境的情况。我们把DHT11信号引脚相关的宏定义到 “bsp_dht11.h”文件中, 这样的话在更改或移植的时候只用改宏定义就可以。 - #ifndef __BSP_DHT11_H
- #define __BSP_DHT11_H
- #include "hal_data.h"
- #define Bit_RESET 0
- #define Bit_SET 1
- #define DHT11_LOW 0
- #define DHT11_HIGH 1
- #define DHT11_PORT BSP_IO_PORT_00_PIN_01
- #define DHT_HIGH R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_HIGH);
- #define DHT_LOW R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_LOW);
- #define Read_Data R_BSP_PinRead(DHT11_PORT)
- #define DHT11_DATA_OUT(a) if (a) \ DHT_HIGH\ else \ DHT_LOW
- typedef struct
- {
- uint8_t humi_int; //湿度的整数部分
- uint8_t humi_deci; //湿度的小数部分
- uint8_t temp_int; //温度的整数部分
- uint8_t temp_deci; //温度的小数部分
- uint8_t check_sum; //校验和
- }DHT11_Data_TypeDef;
- void DHT11_Init (void);
- void DHT11_Start (void);
- void DHT11_DELAY_US (uint32_t delay);
- void DHT11_DELAY_MS (uint32_t delay);
- uint8_t Read_DHT11(DHT11_Data_TypeDef *DHT11_Data);
- #endif
复制代码- //--------------------------------------------------------------------------------------------------
- // 函数头文件 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
- //--------------------------------------------------------------------------------------------------
- #include "hal_data.h"
- #include "oled.h"
- #include "bmp.h"
- #include "bsp_debug_uart.h"
- #include "bsp_led.h"
- #include "bsp_dht11.h"
- #include "bsp_gpt_timing.h"
- #define SUCCESS 1
- void Hardware_init(void);
- FSP_CPP_HEADER
- void R_BSP_WarmStart(bsp_warm_start_event_t event);
- FSP_CPP_FOOTER
- /* Callback function */
- i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
- void sci_i2c_master_callback(i2c_master_callback_args_t *p_args)
- {
- i2c_event = I2C_MASTER_EVENT_ABORTED;
- if (NULL != p_args)
- {
- /* capture callback event for validating the i2c transfer event*/
- i2c_event = p_args->event;
- }
- }
- fsp_err_t err = FSP_SUCCESS;
- uint32_t timeout_ms = 1000;
- DHT11_Data_TypeDef DHT11_Data;
- uint8_t Temperature,Humidity;
- extern uint8_t temp_humi_flag;
- //==================================================================================================
- // 函数说明: 硬件初始化
- // 函数备注: Hardware_init
- //--------------------------------------------------------------------------------------------------
- // | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
- //==================================================================================================
- void Hardware_init(void)
- {
- Debug_UART4_Init(); // SCI4 UART 调试串口初始化
- GPT0_Timing_Init();
- printf("Debug-UART4-Init OK \r\n");
- LED_Init();
- printf("LED_Init OK \r\n");
- printf("IIC-Config Start \r\n");
- DHT11_Init();
- printf("DHT11_Init OK \r\n");
- /* Initialize the I2C module */
- err = R_SCI_I2C_Open(&g_i2c5_ctrl, &g_i2c5_cfg);
- /* Handle any errors. This function should be defined by the user. */
- assert(FSP_SUCCESS == err);
- printf("IIC-Config OK \r\n");
- OLED_Init(); //初始化OLED
- OLED_Clear();
- printf("oled-Init OK \r\n");
- }
- //==================================================================================================
- // 函数说明: 主函数入口
- // 函数备注: hal_entry
- //--------------------------------------------------------------------------------------------------
- // | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
- //==================================================================================================
- /*******************************************************************************************************************//**
- * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
- * is called by main() when no RTOS is used.
- **********************************************************************************************************************/
- void hal_entry(void)
- {
- /* TODO: add your own code here */
- Hardware_init();
- printf("RA6M5-Board-Init OK \r\n");
- R_BSP_PinAccessEnable();
- R_BSP_PinWrite(BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_HIGH); //DHT11端口配置
- while(1)
- {
- /*调用DHT11_Read_TempAndHumidity读取温湿度,若成功则输出该信息*/
- if( Read_DHT11 ( & DHT11_Data ) == SUCCESS )
- {
- Temperature = DHT11_Data.temp_int;
- Humidity = DHT11_Data.humi_int;
- }
- OLED_ShowCHinese(0,0,0);//瑞
- OLED_ShowCHinese(16,0,1);//萨
- OLED_ShowCHinese(32,0,5);//电
- OLED_ShowCHinese(48,0,6);//子
- OLED_ShowString(60,0,"Renesas",16);
- OLED_ShowNum(0,2,2023,4,16);//显示ASCII字符的码值
- OLED_ShowCHinese(32,2,2);//中文字->年
- OLED_ShowNum(48,2,5,2,16);//显示ASCII字符的码值
- OLED_ShowCHinese(64,2,3);//中文字->月
- OLED_ShowNum(80,2,26,2,16);//显示ASCII字符的码值
- OLED_ShowCHinese(96,2,4);//中文字->日
- OLED_ShowCHinese(0,4,7); //中文字->温
- OLED_ShowCHinese(16,4,9); //中文字->度
- OLED_ShowNum(32,4,Temperature,2,16);
- OLED_ShowCHinese(64,4,8); //中文字->湿
- OLED_ShowCHinese(80,4,9); //中文字->度
- OLED_ShowNum(96,4,Humidity,2,16);
- OLED_ShowString(0,6,"RA6M5-Embedfire",16);
- }
- #if BSP_TZ_SECURE_BUILD
- /* Enter non-secure code */
- R_BSP_NonSecureEnter();
- #endif
- }
- /*******************************************************************************************************************//**
- * This function is called at various points during the startup process. This implementation uses the event that is
- * called right before main() to set up the pins.
- *
- * @param[in] event Where at in the start up process the code is currently at
- **********************************************************************************************************************/
- void R_BSP_WarmStart(bsp_warm_start_event_t event)
- {
- if (BSP_WARM_START_RESET == event)
- {
- #if BSP_FEATURE_FLASH_LP_VERSION != 0
- /* Enable reading from data flash. */
- R_FACI_LP->DFLCTL = 1U;
- /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
- * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
- #endif
- }
- if (BSP_WARM_START_POST_C == event)
- {
- /* C runtime environment and system clocks are setup. */
- /* Configure pins. */
- R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
- }
- }
- #if BSP_TZ_SECURE_BUILD
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
- /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
- {
- }
- #endif
复制代码 测试效果图片
|