标题:
附上stm32f103控制dht11温湿度传感器的代码
[打印本页]
作者:
e电动小马达e
时间:
2018-12-27 09:08
标题:
附上stm32f103控制dht11温湿度传感器的代码
实习内容,有些细节没做好
单片机源程序如下:
#include <stdio.h>
#include "uart.h"
#include "tim.h"
#include "SysTick.h"
#include "TFT18.h"
#include "dht11.h"
#include "GUI.h"
#define UART_RX_LEN 8
uint8_t USART2_Buffer[UART_RX_LEN]= {0};
//uint8_t Data_Receive[UART_RX_LEN]= {0};
uint8_t Sendbuf[UART_RX_LEN]={0};
int USART2_Rx=0;
double temperature=0; //空气温度
double humidity=0; //空气湿度
//uint8_t illumination; //光照强度
//uint8_t soil; //土壤湿度
uint8_t Time_3s=0;
uint8_t Time_2s=0;
uint8_t ch;
char buffer[5];
char str[8];
static void SYS_init(void);
static void NVIC_Configuration(void);
static void lcd(void);
static void kaiji(void);
int main()
{
SYS_init();
kaiji();
while(1){
// u8 buffer[5];
// if(Time_2s==1){
// Time_2s=0;
// if (dht11_read_data(buffer)==0){
// humidity = buffer[0] + buffer[1] / 10.0; //空气湿度
// temperature = buffer[2] + buffer[3]/10.0; //空气温度
//// humidity=1.2;
//// temperature=3.5;
// }
// //在屏幕显示数据
// lcd();
///****************数据处理*********************/
///*
// *回复报文格式 Data_Receive[0] Data_Receive[1] Data_Receive[2] Data_Receive[3] Data_Receive[3]
// * (1为空气温度,2为空气湿度,3为光照强度,4为土壤湿度) 数据十位 数据个位 数据十分位 数据百分位
// *
// */
// Sendbuf[0]=2; //标志位2位空气湿度
// Sendbuf[1]=buffer[0]/10; //湿度的十位
// Sendbuf[2]=buffer[0]%10; //湿度的个位
// Sendbuf[4]=buffer[1]/10; //湿度的十分位
// Sendbuf[5]=buffer[1]%10; //湿度的百分位
// }
}
}
/*
函数功能:资源初始化
*/
static void SYS_init(void)
{
uart2_init(115200);
NVIC_Configuration();
/*tim2 10ms 中断定时*/
Tim2_Config(10000,71);
Lcd_Init();
}
/*
函数功能:USART2中断控制
*/
void NVIC_Configuration(void)
{
//配置中断控制器
NVIC_InitTypeDef NVIC_InitStructure;
// /* Enable and set USART Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void kaiji(void)
{
Lcd_Clear(GRAY0);
Gui_DrawFont_GBK16(35,50,BLUE,GRAY0,"开机中...");
}
void lcd(void)
{
Lcd_Clear(GRAY0);
Gui_DrawFont_GBK16(18,30,BLACK,GRAY0,"智能农业大棚");
Gui_DrawFont_GBK16(10,50,BLUE,GRAY0,"空气温湿度检测");
Gui_DrawFont_GBK16(10,70,BLUE,GRAY0,"空气温度:");
sprintf(str,"%f",temperature);
Gui_DrawFont_GBK16(80,70,BLUE,GRAY0,str);
Gui_DrawFont_GBK16(10,90,BLUE,GRAY0,"空气湿度:");
sprintf(str,"%f",humidity);
Gui_DrawFont_GBK16(80,90,BLUE,GRAY0,str);
}
/*
接收中断处理函数
*/
//串口5接收中断
void USART2_IRQHandler()
{
// u32 temp = 0;
// u16 i = 0;
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //中断产生
{
Gui_DrawFont_Num32(100,50,BLACK,GRAY0,5);
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志
USART2_Buffer[USART2_Rx] = USART_ReceiveData(USART2); //接收串口2数据到buff缓冲区
USART2_Rx++;
if(USART2_Buffer[USART2_Rx-1] == 0x0a || USART2_Rx == UART_RX_LEN) //如果接收到尾标识是换行符(或者等于最大接受数就清空重新接收)
{
//收到网关以‘9’开头的命令报文就将检测的数据组成的报文Sendbuf回复网关
if(USART2_Buffer[0] == '9') //检测到头标识是我们需要的
{
delay_ms(20);
// UART_Send_Message(USART2,Sendbuf);
USART2_Rx=0;
}
else
{
USART2_Rx=0; //不是我们需要的数据或者达到最大接收数则开始重新接收
}
}
}
}
复制代码
所有资料51hei提供下载:
空气湿度.7z
(166.54 KB, 下载次数: 116)
2018-12-28 02:49 上传
点击文件名下载附件
dth11驱动
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1