JACKPING DHT11
#include "stm32f4xx.h"
#include "sys.h"
#include "stdio.h"
static GPIO_InitTypeDef GPIO_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
static uint16_t d;
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
USART_SendData(USART3,ch);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
return ch;
}
void delay_ms(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0;
SysTick->LOAD = (168000)-1;
SysTick->VAL = 0;
SysTick->CTRL = 5;
while ((SysTick->CTRL & 0x00010000)==0);
}
SysTick->CTRL = 0;
}
void delay_us(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0;
SysTick->LOAD = (168)-1;
SysTick->VAL = 0;
SysTick->CTRL = 5;
while ((SysTick->CTRL & 0x00010000)==0);
}
SysTick->CTRL = 0;
}
void init_usart3(uint32_t baud)
{
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART3,ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}
void dht11_outputMode(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
void dht11_inputMode(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
uint8_t dht11_start(void)
{
int i = 0;
dht11_outputMode();
PGout(9) = 0;
delay_ms(20);
PGout(9) = 1;
delay_us(30);
dht11_inputMode();
while(i < 100)
{
if(PGin(9) == 1)
{
break;
}
delay_us(1);
i++;
}
if(i == 100)
return 1;
i = 0;
while(i < 100)
{
if(PGin(9) == 0)
{
break;
}
delay_us(1);
i++;
}
if(i == 100)
return 1;
return 0;
}
uint8_t dht11_read_byte(void)
{
int i = 0;
uint8_t d = 0;
while(PGin(9));
for(i = 0; i < 8; i++)
{
while(PGin(9) == 0);
delay_us(40);
if(PGin(9) == 1)
{
d |= 1 <<(7-i);
while(PGin(9));
}
}
return d;
}
uint8_t dht11_read_data(char *dht11_data)
{
int i = 0;
int check_sum = 0;
while(dht11_start() == 1);
for(i = 0; i < 5; i++)
{
dht11_data[i] = dht11_read_byte();
}
check_sum = dht11_data[0] + dht11_data[1] + dht11_data[2] + dht11_data[3];
if(check_sum != dht11_data[4])
{
return 1;
}
return 0;
}
int main(void)
{
char dht11_data[5] = {0};
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
init_usart3(9600);
while(1)
{
if(dht11_read_data(dht11_data) == 0)
{
printf("temp=%d.%d\r\n",dht11_data[2],dht11_data[3]);
printf("humi=%d.%d\r\n",dht11_data[0],dht11_data[1]);
}
delay_ms(2000);
}
}