|
//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
//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)
#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=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
/*!
* @brief 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
}
}