|
使用 MCUXpresso Config Tools工具配置FRDM-KW41Z的GPIO驱动DHT22温湿度传感器
[复制链接]
本帖最后由 dql2016 于 2017-6-17 21:58 编辑
DHT22是已校准的数字温湿度传感器,用于检测环境温湿度,采用DHT22(AM2302),标准单总线接口。
相比DHT11,拥有更高的精度和更大的量程。
【参数】
温度
分辨率:0.1°C
精度:±0.5℃
检测范围:-40°C ~ 80°C
湿度
分辨率:0.1%RH
精度:±2%RH (25°C)
检测范围:0%RH ~ 99.9%RH
工作电压 :3.3V ~ 5.5 V
推荐存储环境:
温度:10°C ~40°C
湿度:60%RH以下
【应用】
气象站、湿度调节器和测试及检测设备等
【接口说明】
以接入MCU为例:
VCC:接3.3V ~ 5.5V
GND:接GND
DOUT:接MCU.IO
选择KW41Z的PTC19(对应FRDM-KW41Z板卡的D2)来驱动DHT22,需要把它配置为输入、输出模式;
step1:配置时钟;
step2:配置PTC19为输出;
step3:生成工程Keil MDK;
step4:DHT22驱动代码网上很多,这里只需要做几个接口就行了,分别是设置GPIO输入输出方向,输出时输出高低电平,读取IO值;
现在DHT22.h文件定义下端口:
- //choose a gpio pin to drive dht22
- //in FRDM-KW41Z board is Arduino UNO R3's D2
- #define DHT22_FGPIO FGPIOC
- #define DHT22_GPIO GPIOC
- #define DHT22_Port PORTC
- #define DHT22_Pin 19U
复制代码
然后在DHT22.c编写接口函数:
- //Set GPIO Direction
- void DHT22_IO_IN(void)
- {
- /* Input pin configuration */
- gpio_pin_config_t inconfig =
- {
- kGPIO_DigitalInput,
- 0,
- };
- /* Input pin PORT configuration */
- port_pin_config_t config =
- {
- kPORT_PullUp,
- kPORT_FastSlewRate,
- kPORT_PassiveFilterDisable,
- kPORT_LowDriveStrength,
- kPORT_MuxAsGpio,
- };
-
- PORT_SetPinConfig(DHT22_Port, DHT22_Pin, &config);//config as pull-up
- FGPIO_PinInit(DHT22_FGPIO, DHT22_Pin, &inconfig);//config as input
- }
- //Set GPIO Direction
- void DHT22_IO_OUT(void)
- {
- /* Output pin configuration */
- gpio_pin_config_t outconfig =
- {
- kGPIO_DigitalOutput,
- 0,
- };
- GPIO_PinInit(DHT22_GPIO, DHT22_Pin, &outconfig);//config as output
- }
- //output 1 or 0
- #define DHT22_DQ_OUT_0 GPIO_WritePinOutput(DHT22_GPIO, DHT22_Pin, 0)
- #define DHT22_DQ_OUT_1 GPIO_WritePinOutput(DHT22_GPIO, DHT22_Pin, 1)
- //read 1 or 0
- #define DHT22_DQ_IN FGPIO_ReadPinInput(DHT22_FGPIO,DHT22_Pin)
复制代码
DHT22.c文件如下,采用接口形式很容易移植到其它MCU:
- #include "DHT22.h"
-
- //follow are 4 gpio interface
- //Set GPIO Direction
- void DHT22_IO_IN(void)
- {
- /* Input pin configuration */
- gpio_pin_config_t inconfig =
- {
- kGPIO_DigitalInput,
- 0,
- };
- /* Input pin PORT configuration */
- port_pin_config_t config =
- {
- kPORT_PullUp,
- kPORT_FastSlewRate,
- kPORT_PassiveFilterDisable,
- kPORT_LowDriveStrength,
- kPORT_MuxAsGpio,
- };
-
- PORT_SetPinConfig(DHT22_Port, DHT22_Pin, &config);//config as pull-up
- FGPIO_PinInit(DHT22_FGPIO, DHT22_Pin, &inconfig);//config as input
- }
- //Set GPIO Direction
- void DHT22_IO_OUT(void)
- {
- /* Output pin configuration */
- gpio_pin_config_t outconfig =
- {
- kGPIO_DigitalOutput,
- 0,
- };
- GPIO_PinInit(DHT22_GPIO, DHT22_Pin, &outconfig);//config as output
- }
- //output 1 or 0
- #define DHT22_DQ_OUT_0 GPIO_WritePinOutput(DHT22_GPIO, DHT22_Pin, 0)
- #define DHT22_DQ_OUT_1 GPIO_WritePinOutput(DHT22_GPIO, DHT22_Pin, 1)
- //read 1 or 0
- #define DHT22_DQ_IN FGPIO_ReadPinInput(DHT22_FGPIO,DHT22_Pin)
- //4 gpio interface end
- //2 delay interface begin
- static void delay_ms(unsigned long xms)
- {
- volatile uint32_t i = 0;
- while(xms--)
- {
- for (i = 0; i < 100; ++i)
- {
- __asm("nop"); /* delay */
- }
- }
- }
- static void delay_us(unsigned long xus)
- {
- volatile uint32_t i = 0;
- while(xus--)
- {
- for (i = 0; i < 3; ++i)
- {
- __asm("nop"); /* delay */
- }
- }
- }
- //2 delay interface end
- //Reset DHT22
- void DHT22_Rst(void)
- {
- DHT22_IO_OUT(); //SET OUTPUT
- DHT22_DQ_OUT_0; //GPIOA.0=0
- delay_ms(30); //Pull down Least 800us
- DHT22_DQ_OUT_1; //GPIOA.0=1
- delay_us(30); //Pull up 20~40us
- }
- u8 DHT22_Check(void)
- {
- u8 retry=0;
- DHT22_IO_IN();//SET INPUT
- while (DHT22_DQ_IN&&retry<100)//DHT22 Pull down 40~80us
- {
- retry++;
- delay_us(1);
- };
- if(retry>=100)
- {
- return 1;
- }
- else
- retry=0;
- while (!DHT22_DQ_IN&&retry<100)//DHT22 Pull up 40~80us
- {
- retry++;
- delay_us(1);
- };
- if(retry>=100)
- {
- return 1;//chack error
-
- }
- return 0;
- }
- u8 DHT22_Read_Bit(void)
- {
- u8 retry=0;
- while(DHT22_DQ_IN&&retry<100)//wait become Low level
- {
- retry++;
- delay_us(1);
- }
- retry=0;
- while(!DHT22_DQ_IN&&retry<100)//wait become High level
- {
- retry++;
- delay_us(1);
- }
- delay_us(40);//wait 40us
- if(DHT22_DQ_IN)
- return 1;
- else
- return 0;
- }
- u8 DHT22_Read_Byte(void)
- {
- u8 i,dat;
- dat=0;
- for (i=0;i<8;i++)
- {
- dat<<=1;
- dat|=DHT22_Read_Bit();
- }
- return dat;
- }
- u8 DHT22_Read_Data(float *temperature,float *humidity)
- {
- u8 buf[5];
- u8 i;
- u8 sum;
- *humidity=0;
- *temperature=0;
- DHT22_Rst();
- if(DHT22_Check()==0)
- {
- for(i=0;i<5;i++)
- {
- buf[i]=DHT22_Read_Byte();
- }
- sum = buf[0]+buf[1]+buf[2]+buf[3];
- if(sum == buf[4])
- {
- *humidity=(float)((buf[0]<<8)+buf[1])/10;
- *temperature=(float)((buf[2]<<8)+buf[3])/10;
- }
- else
- {
- *humidity=(float)((buf[0]<<8)+buf[1])/10;
- *temperature=(float)((buf[2]<<8)+buf[3])/10;
- }
- }
- else
- {
- return 1;
- }
- return 0;
- }
-
- u8 DHT22_Init(void)
- {
-
- DHT22_IO_OUT();
-
- DHT22_Rst();
- return DHT22_Check();
- }
复制代码
主函数测试代码如下:
- /**
- * This is template for main module created by MCUXpresso Project Generator. Enjoy!
- **/
- #include "board.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- //user code begin
- #include "fsl_gpio.h"
- #include "fsl_port.h"
- #include "DHT22.h"
- float temperature = 0;
- float humidity = 0;
- static void mydelay_ms(unsigned long xms)
- {
- volatile uint32_t i = 0;
- while(xms--)
- {
- for (i = 0; i < 4000; ++i)
- {
- __asm("nop"); /* delay */
- }
- }
- }
- //user code end
- /*!
- * [url=home.php?mod=space&uid=159083]@brief[/url] Application entry point.
- */
- int main(void) {
- /* Init board hardware. */
- BOARD_InitBootPins();
- BOARD_InitBootClocks();
- BOARD_InitDebugConsole();
- /* Add your code here */
-
- //user code begin
- while(DHT22_Init());//init DHT22
- printf("Hell EEworld!\r\n");
- DbgConsole_Printf("Hell FRDM-KW41Z!\r\n");
-
- //user code end
- for(;;) { /* Infinite loop to avoid leaving the main function */
- __asm("NOP"); /* something to use as a breakpoint stop while looping */
- //user code begin
- mydelay_ms(500);
-
- DHT22_Read_Data(&temperature,&humidity);
- printf("temperature = %.2f humidity = %.2f%%\r\n",temperature,humidity);
-
- //user code end
- }
- }
复制代码
精度还是不错的:
DHT22数据手册:
AM2302_V1.1.pdf
(865.45 KB, 下载次数: 1, 售价: 1 分芯积分)
工程源码:
KW41Z_Test.zip
(10.89 MB, 下载次数: 41, 售价: 1 分芯积分)
|
|