我的毕设是要实现温度的上下限指示,现在我只能做到给一个设定温度大于它则给一个降温指示,小于它则给加热指示. 但是要求是在一个温度范围内显示正常,大于高温则给一个降温指示,小于低温则给加热指示. 希望高手能给予指点一下. 我的部分程序如下/ //////////////////////////////// unsigned char set_tmp=20,set_tmp1=15; if(num_1>set_tmp) wind=1,hot=0; //温度>高温度,则启动风扇 if(num_1<=set_tmp||num_1>=set_tmp1) wind=0,hot=0;//温度合适,则不动作 if(num_1<set_tmp1) hot=1,wind=0; //温度<低温度,则启动加热 ////////////////////////////// #include <reg51.h> #define uchar unsigned char #define uint unsigned int sbit wind=P1^7; sbit hot=P1^6; sbit DQ=P3^4; code unsigned char key_code[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e,0x7f,0x3f}; code unsigned char ksp2[4]={0xf8,0xf4,0xf2,0xf1}; unsigned char th,tl,tt,s[4],set_tmp=20,set_tmp1=15;
//延时Tms void delay(unsigned int t) {unsigned char i; while(t--) {for(i=0;i<125;i++) {;} } }
//读取温度值部分 void rdtemp() {unsigned int num; unsigned char num_1; unsigned char set_tmp1=15; TxReset(); //产生复位脉冲,初始化DS18B20 RxWait(); //等待DS18B20给出应答脉冲 delay(1); //延时 WrByte(0xcc); //skip rom命令 WrByte(0xbe); //read scratchpad命令 tl=RdByte(); th=RdByte(); num=th*256+tl; //th左移8位或上tl num=num>>4; //数据右移4位去掉小数位 s[0]=num%10; //个位 s[1]=(num/10)%10; //十位 num_1=num%256; //得温度数值 if(num_1>set_tmp) wind=1,hot=0; //温度>设定温度,则启动风扇 if(num_1<=set_tmp||num_1>=set_tmp1) wind=0,hot=0,ok=1; //温度=设定温度,则不动作 if(num_1<set_tmp1) hot=1,wind=0; //温度<设定温度,则启动加热 }
//读键写键部分 unsigned char got_key() { unsigned char ks_1,ks_2; ks_1=~P1; ks_1=(ks_1&0x0f);//保留p1.0/p1.1/p1.2/p1.3 delay(1); ks_2=~P1; ks_2=(ks_2&0x0f);//保留p1.0/p1.1/p1.2/p1.3 if(ks_1==ks_2) return(ks_1); else return(0); }
//按键设置 void set_key(unsigned char key) {if(key==1) {set_tmp-=1;} if(key==2) {set_tmp+=1;} s[2]=set_tmp%10; //设定温度个位 s[3]=set_tmp/10%10; //设定温度十位 while(key) key=got_key(); }
//定时器0中断服务子程序 void out() interrupt 1 {unsigned char i; TH0=0xfc; TL0=0x18; tt+=1; i=tt%4; P2=0; if(i==2) P0=key_code[s]; else P0=key_code[s]+0x80; P2=ksp2; }
//主程序 void main() {unsigned char key; unsigned int i; hot=0; wind=0; IE=0x82; TMOD=1; TH0=0xe0; TL0=0x00; TR0=1; //启动定时计数器0 s[2]=set_tmp%10; s[3]=set_tmp/10%10; while(1) {delay(1); TR0=0; //停止定时计数器0 convert(); //启动温度转换 TR0=1; //启动计数器0 for(i=0;i<1000;i++) { delay(1); key=got_key(); if(key) set_key(key); } TR0=0; rdtemp(); TR1=1; } } |