基于51单片机SHT11温湿度传感器检测程序.doc
基于51单片机的DHT11温湿度系统(已调试成功,无需置疑)
/*系统名:基于51单片机的DHT11温湿度系统系统使用说明:通过无线蓝牙进行数据传输,上位机需通过蓝牙调试软件向系统发送命令获取信息,若要获取当前温度、湿度,则发送“?Z”即可。
系统将会自动回复当前温度和湿度。
注意事项:蓝牙需设置成主从模式(即通过AT命令设置蓝牙名和密码相同,再设置成主从模式)蓝牙灯同步闪烁代表蓝牙连接成功。
*/#include "reg51.h"# include <intrins.h>typedef unsigned char BYTE;typedef unsigned int WORD;#define uint unsigned int#define uchar unsigned char/************辅助全局变量块*****************/char t,buf[6],buf_cnt;bit sec,flag,send_flag;/*sec:秒信号;flag:串行口接收到命令标志;send_flag:回复数据标志*//************辅助全局变量块*****************//*************DHT11变量块*******************/sbit io=P3^2; //DHT11数据端接单片机的P3^2口typedef bit BOOL;//此声明一个布尔型变量即真或假uchar data_byte;uchar RH,RL,TH,TL; //TH保存温度,RH保存湿度/*************DHT11变量块*******************///*******************************延时函数*******************************************void delay(uchar ms){uchar i;while(ms--)for(i=0;i<100;i++);}void delay1()//一个for循环大概需要8个多机器周期 一个机器周期为1us 晶振为12MHz 也就是说本函数延时8us多 此延时函数必须德稍微精确一点{uchar i;for(i=0;i<1;i++);}void delay1ms(char t){int i,j;for(i=0;i<t;i++)for(j=0;j<1000;j++);}//***************************延时函数*****************************************//**************************DHT11模块***************************************** void start()//开始信号{io=1;delay1();io=0;delay(25);// 主机把总线拉低必须大于18ms 保证DHT11能检测到起始信号io=1; //发送开始信号结束后 拉高电平延时20-40usdelay1();//以下三个延时函数差不多为24us 符合要求delay1();delay1();}uchar receive_byte()//接收一个字节//{uchar i,temp;for(i=0;i<8;i++)//接收8bit的数据{while(!io);//等待50us的低电平开始信号结束delay1();//开始信号结束之后 延时26us-28us 以下三个延时函数delay1();delay1();temp=0;//时间为26us-28us 表示接收的为数据'0'if(io==1)temp=1; //如果26us-28us之后 还为高电平 则表示接收的数据为'1'while(io);//等待数据信号高电平 '0'为26us-28us '1'为70usdata_byte<<=1;//接收的数据为高位在前 右移data_byte|=temp;}return data_byte;}void receive()//接收数据{uchar T_H,T_L,R_H,R_L,check,num_check,i;start();//开始信号//io=1; //主机设为输入 判断从机 DHT11 响应信号if(!io)//判断从机是否有低电平响应信号//{while(!io);//判断从机发出 80us 的低电平响应信号是否结束//while(io);//判断从机发出 80us 的高电平是否结束 如结束则主机进入数据接收状态R_H=receive_byte();//湿度高位R_L=receive_byte();//湿度低位T_H=receive_byte();//温度高位T_L=receive_byte();//温度低位check=receive_byte();//校验位io=0; //当最后一bit数据接完毕后 从机拉低电平50us//for(i=0;i<7;i++)//差不多50us的延时delay1();io=1;//总线由上拉电阻拉高 进入空闲状态num_check=R_H+R_L+T_H+T_L;if(num_check==check)//判断读到的四个数据之和是否与校验位相同{RH=R_H;RL=R_L;TH=T_H;TL=T_L;check=num_check;}}}//***************************DHT11模块**********************************//**************************中断服务函数块*******************************void ComSer() interrupt 4 //串行口中断服务函数(串口数据接收){if(RI){t=SBUF;buf[buf_cnt++]=t;RI=0;if(t=='Z'){flag=1;buf_cnt=0;}}}//********************************中断服务函数********************************* //************************串行口字符(字符串)发送块************************** void putchar(unsigned char n){SBUF=n;while(!TI);TI=0;}void puts(unsigned char *q){while(*q)putchar(*q++);}void Enter() //换行函数{putchar(0x0d);putchar(0x0a);}void DispNum(unsigned char n){unsigned char t[8]={0};t[0]=n/10+'0';t[1]=n%10+'0';t[2]=0;puts(t);Enter();}//************************串行口字符(字符串)发送块*****************//********************************串行口请求判断块********************void Dill(){if(buf[0]=='?'){delay1ms(50);send_flag=1;}}//********************************串行口请求判断块**************************** //********************************初始化函数块***************************void init_com() //寄存器初始化函数{SCON=0x50; //串行口工作方式0,多机通信,允许接收IE|=0X90; //定时器T1和总中断允许PCON=0X80; //SMOD为1TMOD|=0X21; //T1工作在方式2,T0工作在方式1TH1=250; //T1高八位赋值TR1=1; //T1打开TR0=1; //T0关闭}//********************************初始化函数块*********************void main(){init_com();while(1){receive(); //读取温湿度if(send_flag)//接收到询问{send_flag=0;puts("Wendu:");DispNum(TH);puts("Shidu:");DispNum(RH);Enter();}if(flag){flag=0;ES=0;Dill();ES=1;}}}。
基于51单片机SHT11温湿度传感器检测程序
基于51单片机SHT11温湿度传感器检测程序(含电路图)下面是原理图:下面是SHT11与MCU连接的典型电路:下面是源代码:#include <reg52.h>#include <intrins.h>/******************************************************** 宏定义********************************************************/ #define uint unsigned int#define uchar unsigned char#define noACK 0#define ACK 1#define STATUS_REG_W 0x06#define STATUS_REG_R 0x07#define MEASURE_TEMP 0x03#define MEASURE_HUMI 0x05#define RESET 0x1eenum {TEMP,HUMI};typedef union //定义共用同类型{unsigned int i;float f;} value;/******************************************************** 位定义********************************************************/ sbit lcdrs=P2^0;sbit lcdrw=P2^1;sbit lcden=P2^2;sbit SCK = P1^0;sbit DATA = P1^1;/******************************************************** 变量定义********************************************************/ uchar table2[]="SHT11 温湿度检测";uchar table3[]="温度为:℃";uchar table4[]="湿度为:";uchar table5[]=".";uchar wendu[6];uchar shidu[6];/******************************************************** 1ms延时函数********************************************************/ void delay(int z){int x,y;for(x=z;x>0;x--)for(y=125;y>0;y--);}/******************************************************** 50us延时函数********************************************************/ void delay_50us(uint t){uint j;for(;t>0;t--)for(j=19;j>0;j--);}/******************************************************** 50ms延时函数********************************************************/ void delay_50ms(uint t){uint j;for(;t>0;t--)for(j=6245;j>0;j--);}/******************************************************** 12864液晶写指令********************************************************/ void write_12864com(uchar ){lcdrs=0;delay_50us(1);P0=;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶写数据********************************************************/ void write_dat(uchar dat){lcdrs=1;lcdrw=0;delay_50us(1);P0=dat;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶初始化********************************************************/ void init12864lcd(void){delay_50ms(2);write_12864com(0x30);delay_50us(4);write_12864com(0x30);delay_50us(4);write_12864com(0x0f);delay_50us(4);write_12864com(0x01);delay_50us(240);write_12864com(0x06);delay_50us(10);write_12864com(0x0c);delay_50us(10);}/********************************************************12864液晶显示函数********************************************************/ void display1(void){uchar i;write_12864com(0x80);for(i=0;i<18;i++){write_dat(table2[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display2(void){uchar i;write_12864com(0x90);for(i=0;i<18;i++){write_dat(table3[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display3(void){uchar i;write_12864com(0x88);for(i=0;i<8;i++){write_dat(table4[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/void displaywendu(void) {uchar i;write_12864com(0x94); for(i=0;i<3;i++){write_dat(wendu[i]); delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]); delay_50us(1);}for(i=4;i<5;i++){write_dat(wendu[i]); delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void displayshidu(void){uchar i;write_12864com(0x8C);for(i=0;i<3;i++){write_dat(shidu[i]);delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i<5;i++){write_dat(shidu[i]);delay_50us(1);}}/******************************************************** SHT11写字节程序********************************************************/ char s_write_byte(unsigned char value){unsigned char i,error=0;for (i=0x80;i>0;i>>=1) //高位为1,循环右移{if (i&value) DATA=1; //和要发送的数相与,结果为发送的位else DATA=0;SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;}DATA=1; //释放数据线SCK=1;error=DATA; //检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0;DATA=1;return error; //error=1 通讯错误}/******************************************************** SHT11读字节程序********************************************************/ char s_read_byte(unsigned char ack){unsigned char i,val=0;DATA=1; //释放数据线for(i=0x80;i>0;i>>=1) //高位为1,循环右移{SCK=1;if(DATA) val=(val|i); //读一位数据线的值SCK=0;}DATA=!ack; //如果是校验,读取完后结束通讯 ;SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;_nop_();_nop_();_nop_();DATA=1; //释放数据线return val;}/******************************************************** SHT11启动传输********************************************************/ void s_transstart(void){DATA=1; SCK=0; //准备_nop_();SCK=1;_nop_();DATA=0;_nop_();SCK=0;_nop_();_nop_();_nop_();SCK=1;_nop_();DATA=1;_nop_();SCK=0;}/********************************************************SHT11连接复位********************************************************/void s_connectionreset(void){unsigned char i;DATA=1; SCK=0; //准备for(i=0;i<9;i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位{SCK=1;SCK=0;}s_transstart(); //启动传输}/********************************************************SHT11温湿度检测********************************************************/char s_measure(unsigned char *p_value, unsigned char *p_checksum,unsigned charmode){unsigned error=0;unsigned int i;s_transstart(); //启动传输switch(mode) //选择发送命令{case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //测量温度case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //测量湿度default : break;}for (i=0;i<65535;i++) if(DATA==0) break; //等待测量结束if(DATA) error+=1; // 如果长时间数据线没有拉低,说明测量错误*(p_value) =s_read_byte(ACK); //读第一个字节,高字节(MSB)*(p_value+1)=s_read_byte(ACK); //读第二个字节,低字节(LSB)*p_checksum =s_read_byte(noACK); //read CRC校验码return error; // error=1 通讯错误}/********************************************************SHT11温湿度值标度变换及温度补偿********************************************************/void calc_sth10(float *p_humidity ,float *p_temperature){const float C1=-4.0; // 12位湿度精度修正公式const float C2=+0.0405; // 12位湿度精度修正公式const float C3=-0.0000028; // 12位湿度精度修正公式const float T1=+0.01; // 14位温度精度5V条件修正公式const float T2=+0.00008; // 14位温度精度5V条件修正公式float rh=*p_humidity; // rh: 12位湿度float t=*p_temperature; // t: 14位温度float rh_lin; // rh_lin: 湿度linear值float rh_true; // rh_true: 湿度ture值float t_C; // t_C : 温度℃t_C=t*0.01 - 40; //补偿温度rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿if(rh_true>100)rh_true=100; //湿度最大修正if(rh_true<0.1)rh_true=0.1; //湿度最小修正*p_temperature=t_C; //返回温度结果*p_humidity=rh_true; //返回湿度结果}/********************************************************主函数********************************************************/void main(void){unsigned int temp,humi;value humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度unsigned char error; //用于检验是否出现错误unsigned char checksum; //CRCinit12864lcd();display1();display2();display3();s_connectionreset(); //启动连接复位while(1){error=0; //初始化error=0,即没有错误error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //温度测量error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //湿度测量if(error!=0) s_connectionreset(); ////如果发生错误,系统复位else{humi_val.f=(float)humi_val.i; //转换为浮点数temp_val.f=(float)temp_val.i; //转换为浮点数calc_sth10(&humi_val.f,&temp_val.f); //修正相对湿度及温度temp=temp_val.f*10;humi=humi_val.f*10;wendu[0]=temp/1000+'0'; //温度百位wendu[1]=temp%1000/100+'0'; //温度十位wendu[2]=temp%100/10+'0'; //温度个位wendu[3]=0x2E; //小数点wendu[4]=temp%10+'0'; //温度小数点后第一位displaywendu();shidu[0]=humi/1000+'0'; //湿度百位shidu[1]=humi%1000/100+'0'; //湿度十位shidu[2]=humi%100/10+'0'; //湿度个位shidu[3]=0x2E; //小数点shidu[4]=humi%10+'0'; //湿度小数点后第一位displayshidu();}delay(800); //等待足够长的时间,以现行下一次转换}}相关手册资料及源码下载地址:基于51单片机SHT11温湿度传感器检测程序相关资料。
51单片机Nokia5110的温湿度传感器DHT11温湿度显示的程序
//返回到DHT_start处
}
k=0;
Dis_DHT_Data();
}
PUBLIC.C如下
#include<reg52.h>
#include "PUBLIC.h"
#define uchar unsigned char
#define uint unsigned int
void delayus(uchar a,uchar b,uchar c)
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",13*/
};
/*******************************************
Nokia5110液晶写一个字节的函数
入口参数dt,command
功能写数据或写命令1为数据,0为命令
}
}
DHT11.c如下
#include <reg52.h>
#include "PUBLIC.h"
#define uchar unsigned char
#define uint unsigned int
unsigned char DHT_Data[5];
//将温湿度的值写在LCD上
void Dis_DHT_Data(void)
delayus(2,1,9);
DHT_Data[i]<<=1;
DHT_Data[i]|=DHT;
while(DHT==1);
}
//校检温湿度的值
if((k<10)&&(DHT_Data[4]!=(DHT_Data[0]+DHT_Data[1]+DHT_Data[2]+DHT_Data[3])))
基于51单片机温湿度采集控制系统.doc
基于51单片机温湿度采集控制系统.摘要温湿度控制系统早已经成为当今社会研究的热门领域之一,同时温湿度也是现今工农业生产生活中必须要首先考虑的重要因素之一。
因此现今社会生产生活中就迫切需要一种价格便宜、容易操作而且精确度高的温湿度控制系统。
本系统设计就是利用AT89C52单片机对温湿度参量进行检测进而实现对温湿度参量的控制。
本系统设计通过使用AT89C52单片机、DHT11传感器模块、LCD1602液晶显示屏模块以及二极管闪烁报警模块。
简单的系统设计就可以基本上满足本系统设计的系统设计要求。
DHT11数字温湿度传感器负责把采集到的温湿度信号传送给AT89C52单片机。
温湿度经过AT89C52单片机处理,然后AT89C52准确地把温湿度信号发送到LCD1602液晶显示屏模块,温湿度数据就可以准确地显示到LCD1602液晶屏上面,同时我们可以分别对温度和湿度设置上下限,越限二极管闪烁报警。
关键词:AT89C52单片机,DHT11传感器模块,1602液晶显示屏模块,二极管闪烁报警模块word教育资料ABSTRACTTemperature and humidity control system has already become one of the most popular areas of today's social research, one of the factors of temperature and humidity is also today's industrial and agricultural production life must first consider important. Therefore the social production and living an urgent need for a cheap, easy operation and high precision temperatureand humidity control system. The system design is the use of the AT89C52 microcontroller temperature and humidity parameters for testing so as to realize the control of temperature and humidity parameters.This design by using AT89C52 microcontroller, the DHT11 sensor module, LCD1602 LCD module diode flashing alarm module. Simple design can be substantially meet the design requirements of the design. DHT11 digital temperature and humidity sensor is responsible for the collected temperature and humidity signal is transmitted to the AT89C52 microcontroller. Temperature and humidity after AT89C52 single-chip processing, and then AT89C52 accurat-省略部分-umi_L_shi]);//显示设置湿度十位write_cmd(0xc7); write_dat(tab_lcd_num[set_humi_L_ge]);//显示设置湿度个位} }//按键功能结束///////////////////////////////////////////// key_flag++; if(key_flag==4)//按键标志 6 key_flag=0; write_cmd(0x01);//清屏disp_start();//显示字符}}////////////比较数据,开启或关闭继电器/////////////////////////////////////void bijiao(){ if(U8T_data_Hset_temp_H)//如果温度大于设置温度上限,打开超出温度上限温度继电器和温度LED, temp_out=0;//P1.1 else temp_out=1; //否则关闭超出温度上限温度继电器和温度LED if(U8T_data_Hset_humi_H)//如果湿度大于设置湿度上限,打开超出湿度上限继电器和湿度LED, humi_out=0;//P1.2 else humi_out=1; //否则关闭超出湿度上限继电器和湿度LED if(U8RH_data_H) 达到当天最大量API KEY 超过次数限制。
基于51单片机的温湿度DHT11采集(word文档良心出品)
一.设计目的温湿度控制系统早己经成为当今社会研究的热门领域之一,同时温湿度也是现今工农业生产生活中必须要首先考虑的崑要因素之一。
因此现今社会生产生活中就迫切需要一种价格便宜、容易操作而且精确度高的温湿度控制系统。
本系统设计就是利用AT89C52单片机对温湿度参最进行•检测进而实现对温湿度参量的控制。
本系统设计通过使用AT89C52单片机、DHT11传感器模块、LCD 1602 液晶显示屏模块以及LED闪烁和蜂鸣器报警模块。
简单的系统设计就可以基本上满足本系统设计的系统设计耍求。
DHTU数字温湿度传感器负责把采集到的温湿度信号传送给AT89C52单片机。
温湿度经过AT89C52单•片机处理,然后AT89C52准确地把温湿度信号发送到LCD1602液晶显示屏模块, 温湿度数据就可以准确地显示到LCD1602液晶屏上面,同时我们可以分别对温度和湿度设置上下限,越限LED闪烁和蜂鸣器报警模块。
二.设计要求和方案选择・LCD1602能够实时、准确的显示AT89C52采样温度值和湿度值,显示设定报警的温度和风扇的状态。
・DHT11采集温度及湿度值,AT89C52能够将DHT11输送过來的采集信息,准确的判断标准值与设定值之间的差异,及吋的启动报警装置(LED闪烁和蜂鸣器)进行报警。
•根据采集温湿度的数据进行判断,控制继电器进行风扇的开闭传感器选择方案•方案一:利用两个传感器分别检测温度和湿度值,温度检测模块选用DS18B20温度传恋器(+-0.5°C)o湿度检测模块选用HS1101电容式湿度传感器(+-5%RH) o这两款传感器都是我们大学实践电子设计中非常常见和常用的传感器。
•方案二:检测温湿度数值选用DHT11传感器作为系统设计的温湿度检测模块。
DHT11传感器能够同时对温度和湿度数值进行检测,而且其精确度能过完全满足本课程设计的精度要求。
湿度、温度测量误差:+-5%> +-2度•经过上述分析,方案一虽然精度更精确。
基于51单片机的温湿度检测控制系统
摘要本次设计是采用MSC-51系列单片机中的AT89S51和DHT11构成的低成本的温湿度的检测控制系统。
单片机AT89S51是一款低消耗、高性能的CMOS8位单片机,由于它强大的功能和低价位,因此在很多领域都是用它。
DHT11温湿度传感器是一款含有已校准数字输出的温湿度复合传感器,传感器包括一个电阻式感湿原件和一个NTC测温元件,该产品具有品质卓越、超快响应、抗干扰能力强、性价比极高等优点。
设计主要包括硬件电路的设计和系统软件的设计。
硬件电路主要包括单片机、温湿度传感器、显示模块、报警器以及控制设备等5部分。
其中由DHT11温湿度传感器及1602字符型液晶模块构成系统显示模块;测温湿度控制电路由温湿度传感器和预设温度值比较报警电路组成;用户根据需要预先输入预设值,当实际测量的温湿度不符合预设的温湿度标准时,发出报警信号(蜂鸣器蜂鸣),启动相应控制。
软件部分包括了主程序、显示子程序、测温湿度子程序。
关键词:AT89S51;DHT11;温湿度传感器AbstractMicrocontroller AT89S51 is a low consumption, high performance CMOS8 bit microcontroller.Because of its powerful features and low price, so it is used in many areas.DHT11 temperature and humidity sensor is a temperature and humidity combined sensor contains a calibrated digital output, the sensor consists of a resistor in the original sense of wet and a NTC temperature measurement devices.The product has many advantage,such as excellent quality, fast response, strong anti-jamming capability . This design is fromed by the AT89S51 in MSC-51 Series and DHT11 constitute which is a low-cost temperature and humidity measurement and control system. The design includes the design of hardware circuit design and system software.The hardware has Five modules.They are a microcontroller, temperature and humidity sensors, display module, alarm and control equipment. The 1602-character LCD module constitute the system display module.The temperature and humidity control circuit by the temperature and humidity sensors and preset temperature alarm circuit.According to the need of pre-enter the default value, when the actual measurement of the temperature humidity does not conform the preset temperature and humidity standards, send the alarm signal (buzzer will beep), and start the corresponding control.The software part includes the main program, the display routines, temperature and humidity subroutine.Key words:Temperature and humidity measurement;Temperature and humidity control;AT89S51 ;DHT11目录前言 (1)1.1本文研究的背景及意义 (1)1.2研究现状 (1)1.3本文研究的主要内容 (1)第2章设计任务分析及方案论证 (4)2.1设计过程及其工艺要求设计 (4)2.2设计总体方案及其论证 (4)2.3器件选定 (5)2.4AT89S51单片机 (11)2.5中断系统 (15)2.6复位电路 (16)2.7时钟电路 (17)2.8显示部分 (18)2.9本章小结 (26)第3章硬件设计 (27)3.1主控制电路和测温时控制电路 (27)3.2主要模块的电路 (28)3.3硬件实施控制 (33)3.4设备运行 (35)3.5控制设备: (36)3.6本章小结 (38)第4章软件设计 (39)4.1系统流程图 (39)4.2按键流程图 (41)4.3P ROTUES运行结果 (42)4.4本章小结 (43)结论 (44)参考文献 (45)附录 (47)前言1.1本文研究的背景及意义粮库已经被广泛的运用,是存储粮食的一个重要方式。
51单片机+DHT11温控程序-可设置温度值
51单片机+DHT11温控程序-可设置温度值基于51 单片机的温湿度测量控制程序,使用DHT11 数字传感器,用1602液晶屏显示,按键定义如下:sbit key_1=P0;//确定按键sbit key_2=P0 ; //按键加sbit key_3=P0;//按键减本程序中用到的头文件12c5a.h 下载:51hei/f/12c5a.rar#include “12c5a.H”#include“intrins.h”unsigned char code num[]={“0123456789”};//显示字符存储unsigned char code start_char[]={“Loading....”};unsigned char code t_char[]={“NOW TEMP IS”};unsigned char *pnum=num;unsigned int bbb,ccc,temp,temp_high,temp_low;#define ADC_POWER 0x80 //ADC power control bit#define ADC_FLAG 0x10 //ADC completeflag#define ADC_START 0x08 //ADC start control bit#define ADC_SPEEDLL 0x00 //540 clocks#define ADC_SPEEDL 0x20 //360 clocks#define ADC_SPEEDH 0x40 //180 clocks#define ADC_SPEEDHH 0x60 //90 clockssbit RS=P3;//数据命令选择端sbit RW=P3 ; //读写控制端sbit E=P3;//使能信号控制端//sbit key_0=P0; //设置按键sbit key_1=P0;//确定按键sbitkey_2=P0 ; //按键加sbit key_3=P0;//按键减sbit P04=P0;sb it P05=P0 ;sbitP06=P0;void key_temphigh();void key_templow();void key_scan();void delay_1602(unsigned int a);void init_1602(void);void WR_data_1602(unsigned char adata,unsigned char i);void process(unsigned int shuju);void Delay(unsigned char n){ int x; while (n--) { x = 500; while (x--); }}void init_ADC(void){P1ASF=0xFF;ADC_RES = 0;ADC_RESL = 0;ADC_CONTR = 0x88;Delay(2);}unsigned int GetResult(void){unsigned char m,n;unsigned int aaa; init_ADC();。
基于51系列单片机仓库温湿度的实时监测系统设计
毕业设计有关说明一、温度传感器的选择、湿度传感器的选择、系统总体设计1. 温度传感器的选择采用AD590,它的测温范围在-55℃~+150℃之间,而且精度高。
M档在测温范围内非线形误差为±0.3℃。
AD590可以承受44V正向电压和20V反向电压,因而器件反接也不会损坏,使用可靠。
它只需直流电源就能工作,而且,无需进行线性校正,所以使用也非常方便,借口也很简单。
作为电流输出型传感器的一个特点是,和电压输出型相比,它有很强的抗外界干扰能力。
2.湿度传感器的选择采用HS1100/HS1101湿度传感器。
HS1100/HS1101电容传感器,在电路构成中等效于一个电容器件,其电容量随着所测空气湿度的增大而增大。
不需校准的完全互换性,高可靠性和长期稳定性,快速响应时间,专利设计的固态聚合物结构,由顶端接触(HS1100)和侧面接触(HS1101)两种封装产品,适用于线性电压输出和频率输出两种电路,适宜于制造流水线上的自动插件和自动装配过程等。
相对湿度在1%---100%RH范围内;电容量由16pF变到200pF,其误差不大于±2%RH;响应时间小于5S;温度系数为0.04 pF/℃。
可见精度是较高的。
3.总体设计系统整体框图二、系统联调的有关说明1. AD590应用电路AD590应用电路2. 主程序流程图时间过的很快,一晃大学几年的生活已接近了尾声在目,当初还是刚进大学的懵懂少年现在也长大了学到了很多,也懂得了很多。
随着毕业日子的到,毕业设计也接近了尾声。
经过两个月的奋战我的毕业设计终于完成了。
在没有做毕业设计以前觉得毕业设计只是对这几年来所学知识的单纯总结,但是通过这次做毕业设计发现自己的看法有点太片面。
毕业设计不仅是对前面所学知识的一种检验,而且也是对自己能力的一种提高。
通过这次毕业设计使我明白了自己原来知识还比较欠缺。
自己要学习的东西还太多,以前老是觉得自己什么东西都会,什么东西都懂,有点眼高手低。
基于51单片机的粮仓温湿度检测
工 程 技 术DOI:10.16661/ki.1672-3791.2018.26.041基于51单片机的粮仓温湿度检测曾鹏 陈其军 段浩楠 高雁凤 许素安 陈锡爱(中国计量大学机电工程学院 浙江杭州 310018)摘 要:随着社会科学技术发展,越来越多的产业开始实现自动化。
近年来,互联网实时监控日渐普及,得到了广泛应用,本文设计了一套基于51单片机的粮仓温湿度检测系统。
该粮仓管理系统根据用户所期待的温湿度对环境(粮仓)进行检测,将数据通过2.4G无线网络上传并记录在数据库,并在超出阈值时报警。
关键词:粮仓管理 51单片机 温湿度检测 无线通讯 串口技术中图分类号:TP27 文献标识码:A 文章编号:1672-3791(2018)09(b)-0041-03Abstract: For the large range of temperature f luctuations and pushing tube speed and temperature control matching problems, the heating system and hydraulic in the intermediate frequency bender are analyzed, transfer function of heating system and hydraulic system are established. A double closed loop controller is developed and variable integral PID method is adopted in hydraulic system control strategy. The results show that base on a double closed loop and variable integral PID control method ensures accurately control to the temperature and high-level quality to the elbows.Key Words: Double closed loop; Variable integral PID control; Medium-frequency heating; Pipe bender粮库是粮食仓库的简称,是存储粮食的重要方式,由粮食部门统一管理,担负着国家粮食储备、地方粮食储备、粮食流通的主渠道作用,其主要任务是完成粮食的接受、保管和调运输送等粮食流通诸环节。
SHT11温湿度传感器在单片机中的应用
郑州航空工业管理学院《单片机原理与应用》课程设计说明书09 级电气工程及其自动化专业 72 班级题目I2C总线数字温湿度传感器SHT11及其在单片机系统的应用姓名李兴田学号090607215指导教师王义琴职称讲师二О一二年五月28 日一、SHT11温湿度传感器的基本原理SHT11的湿度检测运用电容式结构,并采用具有不同保护的“微型结构”检测电极系统与聚合物覆盖层来组成传感器芯片的电容,除保持电容式湿敏器件的原有特性外,还可抵御来自外界的影响。
由于它将温度传感器与湿度传感器结合在一起而构成了一个单一的个体,因而测量精度较高且可精确得出露点,同时不会产生由于温度与湿度传感器之间随温度梯度变化引起的误差。
CMOSensTM技术不仅将温湿度传感器结合在一起,而且还将信号放大器、模/数转换器、校准数据存储器、标准I2C总线等电路全部集成在一个芯片内。
SHT11传感器的内部结构框图如图2所示。
SHT11的每一个传感器都是在极为精确的湿度室中校准的。
SHT11传感器的校准系数预先存在OTP内存中。
经校准的相对湿度和温度传感器与一个14位的A/D转换器相连,可将转换后的数字温湿度值送给二线I2C总线器件,从而将数字信号转换为符合I2C总线协议的串行数字信号。
由于将传感器与电路部分结合在一起,因此,该传感器具有比其它类型的湿度传感器优越得多的性能。
首先是传感器信号强度的增加增强了传感器的抗干扰性能,保证了传感器的长期稳定性,而A/D转换的同时完成,则降低了传感器对干扰噪声的敏感程度。
其次在传感器芯片内装载的校准数据保证了每一只湿度传感器都具有相同的功能,即具有100%的互换性。
最后,传感器可直接通过I2C总线与任何类型的微处理器、微控制器系统连接,从而减少了接口电路的硬件成本,简化了接口方式。
二、设计方案3.4 系统设计流程图设计SHT11温湿度传感器在单片机中的应用的流程图如下:三、硬件设计图5 单片机与SHT11接口电路及温湿度显示电路(1)湿度值输出SHT11可通过I2C总线直接输出数字量湿度值,其相对湿度数字输出特性曲线如图3所示。
基于51单片机的温湿度检测控制系统
摘要本次设计是采用MSC-51系列单片机中的AT89S51和DHT11构成的低成本的温湿度的检测控制系统。
单片机AT89S51是一款低消耗、高性能的CMOS8位单片机,由于它强大的功能和低价位,因此在很多领域都是用它。
DHT11温湿度传感器是一款含有已校准数字输出的温湿度复合传感器,传感器包括一个电阻式感湿原件和一个NTC测温元件,该产品具有品质卓越、超快响应、抗干扰能力强、性价比极高等优点。
设计主要包括硬件电路的设计和系统软件的设计。
硬件电路主要包括单片机、温湿度传感器、显示模块、报警器以及控制设备等5部分。
其中由DHT11温湿度传感器及1602字符型液晶模块构成系统显示模块;测温湿度控制电路由温湿度传感器和预设温度值比较报警电路组成;用户根据需要预先输入预设值,当实际测量的温湿度不符合预设的温湿度标准时,发出报警信号(蜂鸣器蜂鸣),启动相应控制。
软件部分包括了主程序、显示子程序、测温湿度子程序。
关键词:AT89S51;DHT11;温湿度传感器AbstractMicrocontroller AT89S51 is a low consumption, high performance CMOS8 bit microcontroller.Because of its powerful features and low price, so it is used in many areas.DHT11 temperature and humidity sensor is a temperature and humidity combined sensor contains a calibrated digital output, the sensor consists of a resistor in the original sense of wet and a NTC temperature measurement devices.The product has many advantage,such as excellent quality, fast response, strong anti-jamming capability . This design is fromed by the AT89S51 in MSC-51 Series and DHT11 constitute which is a low-cost temperature and humidity measurement and control system. The design includes the design of hardware circuit design and system software.The hardware has Five modules.They are a microcontroller, temperature and humidity sensors, display module, alarm and control equipment. The 1602-character LCD module constitute the system display module.The temperature and humidity control circuit by the temperature and humidity sensors and preset temperature alarm circuit.According to the need of pre-enter the default value, when the actual measurement of the temperature humidity does not conform the preset temperature and humidity standards, send the alarm signal (buzzer will beep), and start the corresponding control.The software part includes the main program, the display routines, temperature and humidity subroutine.Key words:Temperature and humidity measurement;Temperature and humidity control;AT89S51 ;DHT11目录前言 (1)1.1本文研究的背景及意义 (1)1.2研究现状 (1)1.3本文研究的主要内容 (1)第2章设计任务分析及方案论证 (4)2.1设计过程及其工艺要求设计 (4)2.2设计总体方案及其论证 (4)2.3器件选定 (5)2.4AT89S51单片机 (11)2.5中断系统 (15)2.6复位电路 (16)2.7时钟电路 (17)2.8显示部分 (18)2.9本章小结 (26)第3章硬件设计 (27)3.1主控制电路和测温时控制电路 (27)3.2主要模块的电路 (28)3.3硬件实施控制 (33)3.4设备运行 (35)3.5控制设备: (36)3.6本章小结 (38)第4章软件设计 (39)4.1系统流程图 (39)。
毕业论文-基于51单片机的温湿度检测控制系统
毕业论文-基于51单片机的温湿度检测控制系统摘要本次设计是采用MSC-51系列单片机中的AT89S51和DHT11构成的低成本的温湿度的检测控制系统。
单片机AT89S51是一款低消耗、高性能的CMOS8位单片机,由于它强大的功能和低价位,因此在很多领域都是用它。
DHT11温湿度传感器是一款含有已校准数字输出的温湿度复合传感器,传感器包括一个电阻式感湿原件和一个NTC测温元件,该产品具有品质卓越、超快响应、抗干扰能力强、性价比极高等优点。
设计主要包括硬件电路的设计和系统软件的设计。
硬件电路主要包括单片机、温湿度传感器、显示模块、报警器以及控制设备等5部分。
其中由DHT11温湿度传感器及1602字符型液晶模块构成系统显示模块;测温湿度控制电路由温湿度传感器和预设温度值比较报警电路组成;用户根据需要预先输入预设值,当实际测量的温湿度不符合预设的温湿度标准时,发出报警信号(蜂鸣器蜂鸣),启动相应控制。
软件部分包括了主程序、显示子程序、测温湿度子程序。
关键词:AT89S51;DHT11;温湿度传感器AbstractMicrocontroller AT89S51 is a low consumption, high performance CMOS8 bit microcontroller.Because of its powerful features and low price, so it is used in many areas.DHT11 temperature and humidity sensor is a temperature and humidity combined sensor contains a calibrated digital output, the sensor consists of a resistor in the original sense of wet and a NTC temperature measurement devices.The product has many advantage,such as excellent quality, fast response, strong anti-jamming capability . This design is fromed by the AT89S51 in MSC-51 Series and DHT11 constitute which is a low-cost temperature and humidity measurement and control system. The design includes the design of hardware circuit design and system software.The hardware has Five modules.They are a microcontroller, temperature and humidity sensors, display module, alarm and control equipment. The 1602-character LCD module constitute the system display module.The temperature and humidity control circuit by the temperature and humidity sensors and preset temperature alarm circuit.According to the need of pre-enter the default value, when the actual measurement of the temperature humidity does not conform the preset temperature and humidity standards, send the alarm signal (buzzer will beep), and start the corresponding control.The software part includes the main program, the display routines, temperature and humidity subroutine.Key words:Temperature and humidity measurement;Temperature and humidity control;AT89S51 ;DHT11前言1.1本文研究的背景及意义粮库已经被广泛的运用,是存储粮食的一个重要方式。
基于51单片机的温湿度测量参考程序
#include<reg52.h>#include<math.h>#include<INTRINS.H>#define uint unsigned int#define uchar unsigned charsbit seg1=P2^0;sbit seg2=P2^1;sbit seg3=P2^2;sbit DQ=P1^3;//ds18b20 端口sfr dataled=0x80;//显示数据端口uchar code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,};uchar code we[]={0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,}; uchar huanchong[]={0,0,0,0,0,0,0,0,};uchar dispcount,T0count,time,temp[8];sbit led=P1^2;bit flag;uint temp1;uchar flag_get,count,num,minute,second;unsigned long x;void delay1(uchar MS);unsigned int ReadTemperature(void);void Init_DS18B20(void);unsigned char ReadOneChar(void);void WriteOneChar(unsigned char dat);void delay(uint z){while(z--);}void main(void){uchar TempH,TempL;uchar i;EA=1;TMOD=0x15;TH0=0;TL0=0;TH1=(65536-4000)/256;TL1=(65536-4000)%256;TR1=1;TR0=1;ET0=1;ET1=1;//P2=0x00;count=0;while(1){P2=we[7];P0=duan[huanchong[0]];delay(250);P2=we[6];P0=duan[huanchong[1]];delay(250);P2=we[5];P0=duan[huanchong[2]];delay(250);P2=we[4];P0=duan[huanchong[3]];delay(250);P2=we[3];P0=duan[12];delay(250);P2=we[2];P0=duan[TempL];delay(250);P2=we[1];P0=duan[(TempH%100)%10]|0x80;delay(250);P2=we[0];P0=duan[(TempH%100)/10];delay(250);if(flag_get==1) //定时读取当前温度{temp1=ReadTemperature();if(temp1&0x8000){P2=we[0];P0=0x40;//负号标志temp1=~temp1; // 取反加1temp1 +=1;}elseP2=we[0];P0=0;TempH=temp1>>4;TempL=temp1&0x0F;TempL=TempL*6/10;//小数近似处理flag_get=0;}if(flag==1){flag=0;x=T0count*65536+TH0*256+TL0;for(i=0;i<4;i++){temp[i]=0;}i=0;while(x/10){temp[i]=x%10;x=x/10;i++;}temp[i]=x;for(i=0;i<4;i++){huanchong[i]=temp[i];}time=0;T0count=0;TH0=0;TL0=0;TR0=1;}}}void t0(void)interrupt 1 using 0{T0count++;}void t1(void)interrupt 3 using 0{TH1=(65536-4000)/256;TL1=(65536-4000)%256;time++;num++;if(time==250){led=~led;TR0=0;time=0;flag=1;}if (num==50){num=0;flag_get=1;//标志位有效second++;if(second>=60){second=0;minute++;}}}void Init_DS18B20(void){unsigned char x=0;DQ = 1; //DQ复位delay(8); //稍做延时DQ = 0; //单片机将DQ拉低delay(80); //精确延时大于480usDQ = 1; //拉高总线delay(10);x=DQ; //稍做延时后如果x=0则初始化成功x=1则初始化失败delay(5);}/******************************************************************/ /* 读一个字节*/ /******************************************************************/ unsigned char ReadOneChar(void){unsigned char i=0;unsigned char dat = 0;for (i=8;i>0;i--){DQ = 0; // 给脉冲信号dat>>=1;DQ = 1; // 给脉冲信号if(DQ)dat|=0x80;delay(5);}return(dat);}/******************************************************************/ /* 写一个字节*/ /******************************************************************/ void WriteOneChar(unsigned char dat){unsigned char i=0;for (i=8; i>0; i--){DQ = 0;DQ = dat&0x01;delay(5);DQ = 1;dat>>=1;}delay(5);}/******************************************************************//* 读取温度*//******************************************************************/ unsigned int ReadTemperature(void){unsigned char a=0;unsigned int b=0;unsigned int t=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换delay(200);Init_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度a=ReadOneChar(); //低位b=ReadOneChar(); //高位b<<=8;t=a+b;return(t);}。
基于51单片机的DHT11温湿度监测+液晶LCD1602显示程序源代码
基于51单片机的DHT11温湿度监测+液晶LCD1602显示程序源代码/***************DHT11温湿度监测+液晶LCD1602显示程序源代码******************单片机型号:STC15W4K56S4,内部晶振:22.1184M。
功能:DHT11温湿度监测+液晶LCD1602显示。
操作说明:通过温湿度传感器DHT11监测温湿度数值,并将温湿度数值显示在液晶LCD1602上。
**************************************************************************/#include "stc15.h" //包含头文件stc15.h#include <intrins.h> //包含头文件intrins.h#define Busy 0x80 //LCD忙sbit LCD_D0 = P0^0; //LCD_D0对应P0.0sbit LCD_D1 = P0^1; //LCD_D1对应P0.1sbit LCD_D2 = P0^2; //LCD_D2对应P0.2sbit LCD_D3 = P0^3; //LCD_D3对应P0.3sbit LCD_D4 = P0^4; //LCD_D4对应P0.4sbit LCD_D5 = P0^5; //LCD_D5对应P0.5sbit LCD_D6 = P0^6; //LCD_D6对应P0.6sbit LCD_D7 = P0^7; //LCD_D7对应P0.7sbit LCD_RS = P1^0; //LCD_RS对应P1.0sbit LCD_RW = P1^1; //LCD_RW对应P1.1sbit LCD_EN = P3^4; //LCD_EN对应P3.4sbit DHT11_PIN = P4^0; //DHT11管脚对应P4.0void delay(unsigned int t); //delay延时函数void delay_us(unsigned int t); //delay_us延时函数void delay_ms(unsigned int t); //delay_ms延时函数void Delay5Ms(void); //5Ms延时函数void GPIO_1602_Configuration(void); //LCD1602液晶IO口初始化void WriteDataLCD(unsigned char WDLCD); //LCD写数据函数void WriteCommandLCD(unsigned char WCLCD,BuysC); //LCD写命令函数unsigned char ReadDataLCD(void); //LCD读数据函数unsigned char ReadStatusLCD(void); //LCD读状态函数void LCDInit(void); //LCD初始化void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData);//LCD显示一个字符void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData); //LCD显示一个字符串void DHT11_Init(void); //初始化DHT11void DHT11_Delay(unsigned int j); //延时函数,用于DHT11 void DHT11_Delay_10us(void); //延时函数,用于DHT11 void COM(void);void RH(unsigned char *temp,unsigned char *humi);void DHT11_Display(void);unsigned char code welcome[] = {"DHT 11"}; //LCD显示内容DHT 11 unsigned char code Dht11[] = {"T: H: "}; //LCD显示内容T: H: unsigned char code Space[] = {" "};//LCD显示内容空白unsigned char U8FLAG,k;unsigned char U8count,U8temp;unsigned char U8T_data_H_temp,U8T_data_L_temp;unsigned char U8RH_data_H_temp,U8RH_data_L_temp;unsigned char U8checkdata_temp;unsigned char U8comdata;unsigned char temperature;unsigned char humidity;unsigned char disbuff_T[4]={0,0,0,0};unsigned char disbuff_H[4]={0,0,0,0};void delay(unsigned int t) //delay延时函数{while(t--);}void delay_us(unsigned int t) //delay_us延时函数{unsigned char i;while(t--){i = 3;while(i--) delay(1);}}void delay_ms(unsigned int t) //delay_ms延时函数{while(t--){delay_us(t);}}void Delay5Ms(void) //5ms延时函数{unsigned int TempCyc = 3552;while(TempCyc--);}void GPIO_1602_Configuration(void) //LCD1602液晶IO口初始化{P0M1 = P3M1&0x00;P0M0 = P3M0&0x00;P1M1 = P3M1&0xfc;P1M0 = P3M0&0xfc;P3M1 = P4M1&0xef;P3M0 = P4M0&0xef;}unsigned char ReadStatusLCD(void) //测试LCD忙碌状态{LCD_D7 = 1; //LCD的D7置1LCD_RS = 0; //LCD管脚RS设置成低电平LCD_RW = 1; //LCD管脚RW设置成高电平LCD_EN = 0; //LCD管脚E设置成低电平LCD_EN = 0; //LCD管脚E设置成低电平LCD_EN = 1; //LCD管脚E设置成高电平while(LCD_D7); //检测忙信号return(Busy); //表示当前忙}void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC为0时忽略忙检测{if(BuysC) ReadStatusLCD(); //根据需要检测忙LCD_EN = 0; //LCD管脚E设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_RS = 0; //LCD管脚RS设置成低电平LCD_RW = 0; //LCD管脚RW设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时P0 = WCLCD; //将数据送入P0口,即写入指令或地址 _nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 1; //E置高电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 0;//当E由高电平跳变成低电平时,液晶模块开始执行命令}void WriteDataLCD(unsigned char WDLCD) //LCD写数据函数{ReadStatusLCD(); //读取LCD状态LCD_EN = 0; //LCD管脚E设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_RS = 1; //LCD管脚RS设置成高电平LCD_RW = 0; //LCD管脚RW设置成低电平P0 = WDLCD; //将数据送入P0口_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 1; //E置高电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 0;//当E由高电平跳变成低电平时,液晶模块开始执行命令}void LCDInit(void) //LCD初始化{WriteCommandLCD(0x38,0); //三次显示模式设置,不检测忙信号Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);WriteCommandLCD(0x08,1); //关闭显示WriteCommandLCD(0x01,1); //显示清屏WriteCommandLCD(0x06,1); //显示光标移动设置WriteCommandLCD(0x0C,1); //显示开及光标设置}void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData){Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;X |= 0x80; //算出指令码WriteCommandLCD(X,0); //这里不检测忙信号,发送地址码WriteDataLCD(DData); //发送数据}void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData) {unsigned char ListLength;ListLength = 0;Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1while (DData[ListLength]>=0x20) //若到达字串尾则退出{if (X <= 0xF) //X坐标应小于0xF{DisplayOneChar(X, Y, DData[ListLength]);//显示单个字符ListLength++;X++;}}}void DHT11_Init(void) //初始化DHT11 {P4M1 = P4M1&0xfe;P4M0 = P4M0&0xfe;}void DHT11_Delay(unsigned int j){unsigned char i;for(;j>0;j--){for(i=0;i<250;i++);}}void DHT11_Delay_10us(void){unsigned char i;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;}void COM(void){unsigned char i;for(i=0;i<8;i++){U8FLAG=2;while((!DHT11_PIN)&&U8FLAG++);DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us();U8temp=0;if(DHT11_PIN)U8temp=1;U8FLAG=2;while((DHT11_PIN)&&U8FLAG++); //超时则跳出for循环if(U8FLAG==1)break;//判断数据位是0还是1,如果高电平高过预定0高电平值则数据位为 1U8comdata<<=1;U8comdata|=U8temp;}}void RH(unsigned char *temp,unsigned char *humi){DHT11_PIN = 0;DHT11_Delay(180);DHT11_PIN = 1; //总线由上拉电阻拉高主机延时20us DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us(); //主机设为输入判断从机响应信号DHT11_PIN = 1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!DHT11_PIN){U8FLAG=2;//判断从机是否发出 80us 的低电平响应信号是否结束while((!DHT11_PIN)&&U8FLAG++);U8FLAG=2;//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态while((DHT11_PIN)&&U8FLAG++); //数据接收状态COM();U8RH_data_H_temp=U8comdata;COM();U8RH_data_L_temp=U8comdata;COM();U8T_data_H_temp=U8comdata;COM();U8T_data_L_temp=U8comdata;COM();U8checkdata_temp=U8comdata;DHT11_PIN=1; //数据校验U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp); if(U8temp==U8checkdata_temp){*temp = U8T_data_H_temp;*humi = U8RH_data_H_temp;}}}void DHT11_Display(void){RH(&temperature,&humidity);disbuff_T[2]=temperature/100+0x30;disbuff_T[1]=temperature/10%10+0x30;disbuff_T[0]=temperature%10+0x30;disbuff_H[2]=humidity/100+0x30;disbuff_H[1]=humidity/10%10+0x30;disbuff_H[0]=humidity%10+0x30;DisplayOneChar(2,1,disbuff_T[2]);delay_ms(10); //延时DisplayOneChar(3,1,disbuff_T[1]);delay_ms(10); //延时DisplayOneChar(4,1,disbuff_T[0]);delay_ms(10); //延时DisplayOneChar(8,1,disbuff_H[2]);delay_ms(10); //延时DisplayOneChar(9,1,disbuff_H[1]);delay_ms(10); //延时DisplayOneChar(10,1,disbuff_H[0]);delay_ms(10); //延时}void main(void){GPIO_1602_Configuration(); //LCD1602液晶IO口初始化delay_ms(10); //延时LCDInit(); //LCD1602初始化delay_ms(10); //延时DHT11_Init(); //初始化DHT11DisplayListChar(5,0,welcome); //LCD1602显示Hello My Friends delay_ms(10); //延时while(1){DisplayListChar(0,1,Space); //LCD1602显示P: K1delay_ms(10); //延时DisplayListChar(0,1,Dht11); //LCD1602显示delay_ms(10); //延时DHT11_Display();delay_ms(200); //延时}}程序源代码是编译通过的DHT11温湿度监测模块接口电路图该程序的实际运行效果。
71、零基础51单片机教程 DHT11温湿度检测模块
K12-L298P电机驱动板
R3-L293D电机驱动模块
慧净microbit扩展板 micro:bit转接板
慧净micro:bit智能小车驱动板
microbit机器人2WD驱动板
驱动板正面
慧净树莓派扩展板 SMP-ad8951
树莓派SMP-ad8951来自慧净树莓派智能小车驱动板SMP-298Pcar
实验效果
谢谢
DHT11简介
• DHT11是一款有已校准数字信号输出的温湿度传感器。 其精 度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃。
概述
• DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器, 它应用专用的数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性和 卓越的长期稳定性。传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一 个高性能8位单片机相连接。因此该产品具有品质卓越、超快响应、抗干扰能力强、 性价比极高等优点。每个DHT11传感器都在极为精确的湿度校验室中进行校准。校 准系数以程序的形式存在OTP内存中,传感器内部在检测信号的处理过程中要调用 这些校准系数。单线制串行接口,使系统集成变得简易快捷。超小的体积、极低的 功耗,使其成为该类应用中,在苛刻应用场合的最佳选择。产品为4针单排引脚封 装,连接方便。
模块:S ---接扩展板S--开发板IO口引脚 模块:V--- 接扩展板V--开发板VCC针脚 模块:G--- 接扩展板G--开发板GND针脚
模块接口 3P线接头
3P线接头
扩展板接口
51单片机最小系统
AVR单片机最小系统
STM32单片机最小系统
慧净ARduino主板--扩展板
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于51单片机SHT11温湿度传感器检测程序(含电路图)•下面是原理图:下面是SHT11与MCU连接的典型电路:下面是源代码:#include <reg52.h>#include <intrins.h>/******************************************************** 宏定义********************************************************/ #define uint unsigned int#define uchar unsigned char#define noACK 0#define ACK 1#define STATUS_REG_W 0x06#define STATUS_REG_R 0x07#define MEASURE_TEMP 0x03#define MEASURE_HUMI 0x05#define RESET 0x1eenum {TEMP,HUMI};typedef union //定义共用同类型{unsigned int i;float f;} value;/******************************************************** 位定义********************************************************/ sbit lcdrs=P2^0;sbit lcdrw=P2^1;sbit lcden=P2^2;sbit SCK = P1^0;sbit DATA = P1^1;/******************************************************** 变量定义********************************************************/ uchar table2[]="SHT11 温湿度检测";uchar table3[]="温度为:℃";uchar table4[]="湿度为:";uchar table5[]=".";uchar wendu[6];uchar shidu[6];/******************************************************** 1ms延时函数********************************************************/ void delay(int z){int x,y;for(x=z;x>0;x--)for(y=125;y>0;y--);}/******************************************************** 50us延时函数********************************************************/ void delay_50us(uint t){uint j;for(;t>0;t--)for(j=19;j>0;j--);}/******************************************************** 50ms延时函数********************************************************/ void delay_50ms(uint t){uint j;for(;t>0;t--)for(j=6245;j>0;j--);}/******************************************************** 12864液晶写指令********************************************************/ void write_12864com(uchar com){lcdrs=0;delay_50us(1);P0=com;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶写数据********************************************************/ void write_dat(uchar dat){lcdrs=1;lcdrw=0;delay_50us(1);P0=dat;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶初始化********************************************************/ void init12864lcd(void){delay_50ms(2);write_12864com(0x30);delay_50us(4);write_12864com(0x30);delay_50us(4);write_12864com(0x0f);delay_50us(4);write_12864com(0x01);delay_50us(240);write_12864com(0x06);delay_50us(10);write_12864com(0x0c);delay_50us(10);}/******************************************************** 12864液晶显示函数********************************************************/ void display1(void){uchar i;write_12864com(0x80);for(i=0;i<18;i++){write_dat(table2[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display2(void){uchar i;write_12864com(0x90);for(i=0;i<18;i++){write_dat(table3[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display3(void){uchar i;write_12864com(0x88);for(i=0;i<8;i++){write_dat(table4[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void displaywendu(void){uchar i;write_12864com(0x94);for(i=0;i<3;i++){write_dat(wendu[i]);delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i<5;i++){write_dat(wendu[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void displayshidu(void){uchar i;write_12864com(0x8C);for(i=0;i<3;i++){write_dat(shidu[i]);delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i<5;i++){write_dat(shidu[i]);delay_50us(1);}}/******************************************************** SHT11写字节程序********************************************************/ char s_write_byte(unsigned char value){unsigned char i,error=0;for (i=0x80;i>0;i>>=1) //高位为1,循环右移{if (i&value) DATA=1; //和要发送的数相与,结果为发送的位 else DATA=0;SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;}DATA=1; //释放数据线SCK=1;error=DATA; //检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0;DATA=1;return error; //error=1 通讯错误}/********************************************************SHT11读字节程序********************************************************/ char s_read_byte(unsigned char ack){unsigned char i,val=0;DATA=1; //释放数据线for(i=0x80;i>0;i>>=1) //高位为1,循环右移{SCK=1;if(DATA) val=(val|i); //读一位数据线的值SCK=0;}DATA=!ack; //如果是校验,读取完后结束通讯 ; SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;_nop_();_nop_();_nop_();DATA=1; //释放数据线return val;}/********************************************************SHT11启动传输********************************************************/ void s_transstart(void){DATA=1; SCK=0; //准备_nop_();SCK=1;_nop_();DATA=0;_nop_();SCK=0;_nop_();_nop_();_nop_();SCK=1;_nop_();DATA=1;_nop_();SCK=0;}/********************************************************SHT11连接复位********************************************************/ void s_connectionreset(void){unsigned char i;DATA=1; SCK=0; //准备for(i=0;i<9;i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位{SCK=1;SCK=0;}s_transstart(); //启动传输}/********************************************************SHT11温湿度检测********************************************************/char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned charmode){unsigned error=0;unsigned int i;s_transstart(); //启动传输switch(mode) //选择发送命令{case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //测量温度case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //测量湿度default : break;}for (i=0;i<65535;i++) if(DATA==0) break; //等待测量结束if(DATA) error+=1; // 如果长时间数据线没有拉低,说明测量错误*(p_value) =s_read_byte(ACK); //读第一个字节,高字节 (MSB)*(p_value+1)=s_read_byte(ACK); //读第二个字节,低字节 (LSB)*p_checksum =s_read_byte(noACK); //read CRC校验码return error; // error=1 通讯错误}/********************************************************SHT11温湿度值标度变换及温度补偿********************************************************/void calc_sth10(float *p_humidity ,float *p_temperature){const float C1=-4.0; // 12位湿度精度修正公式const float C2=+0.0405; // 12位湿度精度修正公式const float C3=-0.0000028; // 12位湿度精度修正公式const float T1=+0.01; // 14位温度精度 5V条件修正公式const float T2=+0.00008; // 14位温度精度 5V条件修正公式float rh=*p_humidity; // rh: 12位湿度float t=*p_temperature; // t: 14位温度float rh_lin; // rh_lin: 湿度 linear值float rh_true; // rh_true: 湿度 ture值float t_C; // t_C : 温度℃t_C=t*0.01 - 40; //补偿温度rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿if(rh_true>100)rh_true=100; //湿度最大修正if(rh_true<0.1)rh_true=0.1; //湿度最小修正*p_temperature=t_C; //返回温度结果*p_humidity=rh_true; //返回湿度结果}/********************************************************主函数********************************************************/void main(void){unsigned int temp,humi;value humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度 unsigned char error; //用于检验是否出现错误unsigned char checksum; //CRCinit12864lcd();display1();display2();display3();s_connectionreset(); //启动连接复位while(1){error=0; //初始化error=0,即没有错误error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //温度测量error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //湿度测量if(error!=0) s_connectionreset(); ////如果发生错误,系统复位else{humi_val.f=(float)humi_val.i; //转换为浮点数temp_val.f=(float)temp_val.i; //转换为浮点数calc_sth10(&humi_val.f,&temp_val.f); //修正相对湿度及温度temp=temp_val.f*10;humi=humi_val.f*10;wendu[0]=temp/1000+'0'; //温度百位wendu[1]=temp%1000/100+'0'; //温度十位wendu[2]=temp%100/10+'0'; //温度个位wendu[3]=0x2E; //小数点wendu[4]=temp%10+'0'; //温度小数点后第一位displaywendu();shidu[0]=humi/1000+'0'; //湿度百位shidu[1]=humi%1000/100+'0'; //湿度十位shidu[2]=humi%100/10+'0'; //湿度个位shidu[3]=0x2E; //小数点shidu[4]=humi%10+'0'; //湿度小数点后第一位displayshidu();}delay(800); //等待足够长的时间,以现行下一次转换}}相关手册资料及源码下载地址:基于51单片机SHT11温湿度传感器检测程序相关资料。