各位大神看下以下的功能与程序,本人试了下,发现程序不能用,但不知道怎么改,希望各位大神能帮帮忙,非常感谢,毕设急需,可以有报酬,谢谢了!阈值设置分为两个模块,分别是湿度最高值设置模式、温度最高值设置模式。例如湿度最高值设置,按一次K6湿度最高值加一,按一次K7湿度最高值减一,1602液晶显示将实时显示出设置值。其他值设置类似。利用发光二极管的点亮来实现报警,当单片机P1.5、P1.6引脚输出一个低电平时发光二极管亮,实现报警。
为实现温湿度阈值的连续性。考虑了温湿度阈值的存储,在掉电后,下一次使用时能回到原来设定的阈值。24C02的时钟线SCL与单片机的P1.0相连,数据线SDA与单片机的P1.1相连。
A1.1主程序//实现功能,设定温度上限和湿度上限,超过上限值时报警。报警现象,对应二极管亮,蜂鸣器响//#include <reg51.h>#include"1602.h"#include"dht.h"#include"2402.h" //定义变量sbitLed_qushi=P1^6; //潮湿报警灯sbitLed_jiangwen=P1^5; //超温报警灯sbit Key_TH1 =P1^2; //温度最高值设置按键sbit Key_TH2 =P3^3; //温度最高值设置按键sbit Key_HH1 =P3^4; //湿度最高值设置按键sbit Key_HH2 =P3^5; //湿度最高值设置按键sbit beep=P2^0; //蜂鸣器volatile bitFlagStartRH = 0; //开始温湿度转换标志volatile bitFlagKeyPress = 0; //有键按下extern U8 U8FLAG,k; //定义温湿度传感器用外部变量extern U8 U8count,U8temp;extern U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;extern U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;extern U8 U8comdata;extern U8 count, count_r;U16 temp;S16 temperature,humidity;S16 idata TH,HH; //温度上限和湿度上限char * pSave;U8 keyvalue,keyTH1, keyTH2, keyHH1, keyHH2;U16 RHCounter;void Data_Init() //数据初始化{ RHCounter = 0; keyvalue = 0; Led_qushi = 1; Led_jiangwen = 1; TH = 40; HH = 85; keyTH1 = 1; keyTH2 = 1; keyHH1 = 1; keyHH2 = 1; beep=1; } void Timer0_Init() //定时器0初始化{ ET0 = 1; //允许定时器0中断 TMOD = 1; //定时器工作方式选择 TL0 = 0x06; //定时器赋予低位初值 TH0 = 0xf8; //定时器赋予高位初值 TR0 = 1; } //启动定时器void Timer0_ISR(void) interrupt 1 using 0 //定时器0中断{ TL0 = 0x06; //定时器赋予低位初值 TH0 = 0xf8; //定时器赋予高位初值 RHCounter ++; //每2秒钟启动一次温湿度转换 if (RHCounter >= 1000) { FlagStartRH = 1; RHCounter = 0; }}voidSave_Setting() //存入设定值、{ pSave = (char *)&TH; //地址低位对应低8位,高位对应高8位 wrteeprom(0, *pSave); //存温度上限值TH低8位 DELAY(500); //延时 pSave ++; wrteeprom(1, *pSave); //存温度上限值TH高8位 DELAY(500); pSave = (char *)&HH; wrteeprom(2, *pSave); //存湿度上限值RH低8位 DELAY(500); pSave ++; wrteeprom(3, *pSave); //存湿度上限值RH高8位 DELAY(500); }voidLoad_Setting() //载入设定值、{ pSave = (char *)&TH; *pSave++ = rdeeprom(0); *pSave = rdeeprom(1); pSave = (char *)&HH; *pSave++ = rdeeprom(2); *pSave = rdeeprom(3); if ((TH>99)||(TH<0)) TH = 40; if ((HH>99)||(HH<0)) HH = 85; }voidKeyProcess(uint num){ switch (num) { case 1: if (TH<99) TH++; L1602_char(1, 15, TH/10+48); L1602_char(1, 16, TH%10+48); break; case 2: if (TH>1) TH--; L1602_char(1, 15, TH/10+48); L1602_char(1, 16, TH%10+48); break; case 3: if (HH<99) HH++; L1602_char(2, 15, HH/10+48); L1602_char(2, 16, HH%10+48); break; case 4: if (HH>1) HH--; L1602_char(2, 15, HH/10+48); L1602_char(2, 16, HH%10+48); break; default: break; } Save_Setting();}/*********主函数**********/void main(){ U16 i, j, testnum; EA = 0; Timer0_Init(); //定时器0初始化 Data_Init(); EA = 1; L1602_init(); L1602_string(1,1," Welcome toT&H "); L1602_string(2,1," ControlSystem! "); for (i=0;i<1000;i++) //延时 for (j=0;j<1000;j++) {;} L1602_string(1,1," "); //清屏 L1602_string(2,1," "); L1602_string(1,1,"Tem: C TH:"); L1602_string(2,1,"Hum: % HH:"); Load_Setting(); //载入温度上限和湿度上限设定值 L1602_char(1, 15, TH/10+48); L1602_char(1, 16, TH%10+48); L1602_char(2, 15, HH/10+48); L1602_char(2, 16, HH%10+48); while(1) { if (FlagStartRH == 1) //温湿度转换标志检查 { TR0= 0; testnum = RH(); FlagStartRH= 0; TR0 = 1; humidity = U8RH_data_H; //读出温湿度,只取整数部分 temperature = U8T_data_H; L1602_int(1,5,temperature); //显示温湿度 L1602_int(2,5,humidity); } //温湿度控制 if (temperature > TH) //判断超温 { Led_jiangwen= 0; //二极管亮,超温报警 beep=0; //蜂鸣器响,超温报警 DELAY(500); } //延时 else { Led_jiangwen = 1; beep=1; DELAY(500); } if (humidity > HH) //判断潮湿 { Led_qushi = 0; //二极管亮,超湿报警 beep=0; //蜂鸣器响,超温报警 DELAY(500); } else { Led_qushi= 1; beep=1; DELAY(500); }//键盘查询,在弹起时响应 if((Key_TH1)&&(keyTH1==0)) {FlagKeyPress = 1; keyvalue = 1;} else if((Key_TH2)&&(keyTH2==0)){FlagKeyPress=1;keyvalue= 2;} else if((Key_HH1)&&(keyHH1==0)){FlagKeyPress=1;keyvalue=3;} else if((Key_HH2)&&(keyHH2==0)){FlagKeyPress=1;keyvalue=4;} if (FlagKeyPress == 1) { KeyProcess(keyvalue); FlagKeyPress = 0; } if (!Key_TH1) keyTH1 = 0; else keyTH1 = 1; if (!Key_TH2) keyTH2 = 0; else keyTH2 = 1; if (!Key_HH1) keyHH1 = 0; else keyHH1 = 1; if (!Key_HH2) keyHH2 = 0; else keyHH2 = 1; } }A1.2 24C02程序 #include"2402.h" voidDELAY(unsigned int t){ while(t!=0) t--; }voidIICStart(void){ SCL=0; DELAY(1); SDA=1; SCL=1; DELAY(1); SDA=0; DELAY(1); SCL=0; }void IICStop(void){ SDA=0;SCL=1; DELAY(1); SDA=1; DELAY(1); SCL=0; }void SEND0(void){ SDA=0; SCL=1; DELAY(1); SCL=0; }void SEND1(void){ SDA=1; DELAY(1); SCL=1; DELAY(1); SCL=0; }bitCheck_Ack(void){ unsigned char errtime=250; DELAY(1); SCL=1; DELAY(1); CY=SDA; while(CY) { errtime--; CY=SDA; if(!errtime) { IICStop(); return 1; }} DELAY(1); SCL=0; return 0;}voidWrite_byte(unsigned char dat) { unsigned char i; for(i=0;i<8;i++) { if((dat<<i)&0x80) SEND1(); else SEND0(); }}unsigned charRead_byte(void){ unsigned char i,temp=0; for(i=0;i<8;i++) { SDA=1; SCL=1; DELAY(1); if(SDA==1) { temp=temp<<1; temp=temp|0x01; } else temp=temp<<1; SCL=0; } returntemp; }unsigned charrdeeprom(unsigned char addr){ unsigned char temp=0; bit flag=0; IICStart(); Write_byte(0xa0); Check_Ack(); Write_byte(addr); Check_Ack(); IICStart(); Write_byte(0xa1); Check_Ack(); temp=Read_byte(); SEND1(); IICStop(); return temp; } voidwrteeprom(unsigned char addr,unsigned char dat){ IICStart(); Write_byte(0xa0); Check_Ack(); Write_byte(addr); Check_Ack(); Write_byte(dat); Check_Ack(); IICStop(); } #ifndef _2402_H_#define _2402_H_#include<reg52.h>sbit SCL = P1^0; //模拟I2C 总线的引脚定义sbit SDA = P1^1; //定义2402在I2C总线协议中从机地址voidDELAY(unsigned int t); //延时函数voidIICStart(void); //I2C开始函数voidIICStop(void); //I2C结束函数voidAck(void); //I2C应答函数voidNOAck(void); //I2C应答非函数bitCheck_Ack(void); //I2C应答检测函数voidSEND0(void); //I2C发送0函数voidSEND1(void); //I2C发送1函数 void Write_byte(unsignedchar dat); //I2C发送字节函数unsigned charRead_byte(void); //I2C读取字节函数unsigned charrdeeprom(unsigned char addr); //读AT2402函数voidwrteeprom(unsigned char addr,unsigned char dat); //写AT2402函数#endif //_2402_H_A1.3 DHT11传感器程序#include"dht.h"U8 U8FLAG,k;U8 U8count,U8temp;U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;U8 U8comdata;U8 count, count_r=0;void Delay1(U16 j){ U8 i; for(;j>0;j--) { for(i=0;i<27;i++); }}void Delay_10us(void){ U8 i; i--; i--;i--; i--; i--; i--; }void COM(void){ U8 i; for(i=0;i<8;i++) { U8FLAG=2; while((!DATA)&&U8FLAG++); Delay_10us(); Delay_10us(); Delay_10us(); U8temp=0; if(DATA)U8temp=1; U8FLAG=2; while((DATA)&&U8FLAG++); //超时则跳出for循环 if(U8FLAG==1)break; //判断数据位是0还是1.如果高电平高过预定0高电平值则数据位为 1 U8comdata<<=1; U8comdata|=U8temp; } //0 }//温湿度读取子程序 -以下变量均为全局变量//温度高8位== U8T_data_H-温度低8位== U8T_data_L//湿度高8位== U8RH_data_H-湿度低8位== U8RH_data_L//校验 8位 == U8checkdata-调用相关子程序如下U8 RH(void){ DATA=0; //主机拉低18ms Delay1(180); //原来为5 DATA=1; Delay_10us(); //总线由上拉电阻拉高 主机延时20us Delay_10us(); Delay_10us(); Delay_10us(); DATA=1; //主机设为输入 判断从机响应信号 //判断从机是否有低电平响应信号,不响应跳出,响应则向下运行 if(!DATA) //T ! { U8FLAG=2;//判断从机是否发出 80us 的低电平响应信号是否结束 while((!DATA)&&U8FLAG++); U8FLAG=2;//判断从机是否发出 80us 的高电平,发出则进入数据接收状态 while((DATA)&&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; DATA=1; //数据校验 U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp); if(U8temp==U8checkdata_temp) { U8RH_data_H=U8RH_data_H_temp; U8RH_data_L=U8RH_data_L_temp; U8T_data_H=U8T_data_H_temp; U8T_data_L=U8T_data_L_temp; U8checkdata=U8checkdata_temp; } return 1; } else //传感器不响应 { return 0; }}A1.4 1602程序/************该程序实现了对液晶1602的控制**************/#include"1602.h"#include"math.h"void delay() //功能.延时,延时时间大概为140US。{ int i,j; for(i=0; i<=10; i++) for(j=0; j<=2; j++); }ucharConvert(uchar In_Date){ return In_Date; }void enable(uchardel) //1602命令函数,输入的命令值{ P0 = Convert(del); RS = 0; RW= 0; E = 0; delay(); E = 1; delay(); }void write(uchardel) //1602写数据函数{ P0 = Convert(del); RS = 1; RW= 0; E = 0; delay(); E = 1; delay(); }voidL1602_init(void) //1602初始化{ enable(0x01); enable(0x38); enable(0x0c); enable(0x06); enable(0xd0); }voidL1602_char(uchar hang,uchar lie,char sign){ uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); write(sign); }voidL1602_string(uchar hang,uchar lie,uchar *p){ uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); while(1) { if(*p== ' ') break; write(*p); p++; }}voidL1602_int(uchar hang, uchar lie, int num){ uint temp; //显示整型的温湿度数据.共占用4位. uint gewei,shiwei,baiwei,sign; if (num >= 0) { sign = 0; } else { sign = 1; } temp =abs(num); baiwei = temp / 100; temp = temp - baiwei*100; shiwei = temp / 10; gewei = temp - shiwei*10; num = abs(num); if (num>=100) { if (sign == 1) //负数 { L1602_char(hang, lie,'-'); } L1602_char(hang, lie+1, baiwei+48); L1602_char(hang, lie+2, shiwei+48); L1602_char(hang, lie+3, gewei+48); } else if (num>=10) { if(sign == 1) { L1602_char(hang, lie+1,'-'); } L1602_char(hang, lie+2, shiwei+48); L1602_char(hang, lie+3, gewei+48); } else { if (sign == 1) { L1602_char(hang, lie+2,'-'); } L1602_char(hang, lie+3, gewei+48); }} #include<reg52.h>#define ucharunsigned char#define uint unsigned intsbit E=P2^5; //1602使能引脚sbit RW=P2^6; //1602读写引脚 sbit RS=P2^7; //1602数据/命令选择引脚void delay();ucharConvert(uchar );void enable(uchar);void write(uchar);voidL1602_init(void);voidL1602_char(uchar ,uchar ,char );voidL1602_string(uchar ,uchar ,uchar *);void L1602_int(uchar,uchar, int);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>