中科深谷stm32f4温湿度模块检测
#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_init(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_NOPULL;
GPIO_Init(GPIOG, &GPIO_InitStructure);
PGout(9)=1;
}
int32_t dht11_read(uint8_t *pbuf)
{
uint32_t t=0;
int32_t i=0,j=0;
uint8_t d=0;
uint8_t *p=pbuf;
uint32_t check_sum=0;
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_NOPULL;
GPIO_Init(GPIOG, &GPIO_InitStructure);
PGout(9)=0;
delay_ms(20);
PGout(9)=1;
delay_us(30);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOG, &GPIO_InitStructure);
t=0;
while(PGin(9))
{
t++;
delay_us(1);
if(t>=4000)
{
return -1;
}
}
t=0;
while(PGin(9)==0)
{
t++;
delay_us(1);
if(t>=4000)
{
return -2;
}
}
t=0;
while(PGin(9))
{
t++;
delay_us(1);
if(t>=4000)
{
return -3;
}
}
for(j=0;j<5;j++)
{
d=0;
for(i=7;i>=0;i--)
{
t=0;
while(PGin(9)==0)
{
t++;
delay_us(1);
if(t>=4000)
{
return -4;
}
}
delay_us(40);
if(PGin(9))
{
d|=1<<i;
t=0;
while(PGin(9))
{
t++;
delay_us(1);
if(t>=1000)
{
return -5;
}
}
}
}
p[j]=d;
}
check_sum = (p[0]+p[1]+p[2]+p[3])&0xFF;
if(p[4]!=check_sum)
{
return -6;
}
return 0;
}
int main(void)
{
int32_t rt=0;
uint8_t buf[5]={0};
init_usart3(9600);
dht11_init();
while(1)
{
rt=dht11_read(buf);
if(rt==0)
{
printf("T:%d.%d,H:%d.%d\r\n",buf[2],buf[3],buf[0],buf[1]);
}else
{
printf("dht11 error code %d\r\n",rt);
}
delay_ms(2000);
}
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3,USART_IT_RXNE)==SET)
{
d=USART_ReceiveData(USART3);
USART_SendData(USART3,d);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) != SET);
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
}
}