温湿度模块

中科深谷stm32f4温湿度模块检测

#include "stm32f4xx.h"                  // Device header
#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; /* Add whatever you need here */ };
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; // Disable SysTick
		SysTick->LOAD = (168000)-1; // Count from 255 to 0 (256 cycles)
		SysTick->VAL = 0; // Clear current value as well as count flag
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set
	}
	
	SysTick->CTRL = 0; // Disable SysTick
}

void delay_us(uint32_t n)
{
	while(n--)
	{
		SysTick->CTRL = 0; // Disable SysTick
		SysTick->LOAD = (168)-1; // Count from 255 to 0 (256 cycles)
		SysTick->VAL = 0; // Clear current value as well as count flag
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set
	}
	
	SysTick->CTRL = 0; // Disable SysTick
}

//初始化串口3
void init_usart3(uint32_t baud)
{
	USART_InitTypeDef USART_InitStructure;	
	
	// 使能PB时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	
	// 使能串口3时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	// 将PB9 PB10连接到串口1
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
	
	// 配置GPIO B10 B11为第二功能模式
	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;
	// 初始化GPIOA
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	// 配置串口属性
	USART_InitStructure.USART_BaudRate = baud; // 波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据为
	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);
	
	// 配置串口3优先级
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 触发串口1中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	// 使能串口3工作
	USART_Cmd(USART3,ENABLE);
	
	// 使能串口3接收中断,如果有数据达到,触发中断服务函数
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

}

void dht11_init(void)
{
   //使能PG9时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
	
	//配置PG9为输出模式
	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;//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;//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;//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);
		// 将usart3接收的数据传给usart3
		USART_SendData(USART3,d);
		
		// 等待发送完成(USART_GetFlagStatus有延时作用)
		while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) != SET);
		
		//清空标志位
		USART_ClearITPendingBit(USART3,USART_IT_RXNE);
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值