给大伙分享一个最近的学习成果:基于51单片机的温湿度仿真 程序加proteus 8.0的版本 1602液晶屏显示
dht11温湿度系统仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h> /////////////头文件
- ////////////////////////////////////////////////////////////////////////////
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
- void ConfigTimer0(unsigned int ms);
- unsigned char IntToString(unsigned char *str, int dat);
- extern bit Start18B20();
- extern bit Get18B20Temp(int *temp);
- extern void InitLcd1602();
- bit DHT_Start();
- void aj (void);
- bit DHT_ByteRead(unsigned char *dat);
- /////////////////////////////////////////////////////////////////////
- sbit K1=P1^4; ///引脚声明
- sbit K2=P1^5;
- sbit K3=P1^6;
- sbit K4=P1^7;
- sbit M1=P1^0;
- sbit M2=P1^1;
- sbit M3=P1^2;
- sbit M4=P1^3;
- sbit D1=P2^7;
- sbit D2=P2^6;
- sbit KD1=P2^3;
- sbit KD2=P2^4;
- /////////////////
- ///////////////////////////// 数组命名
- int SD,WD,x,xx,SDC,WDC,PWM1,PWM2;
- int KK1,KK2;
- int wdg=40,wdd=20,sdg=80,sdd=60;
- bit SZ,JB;
- bit flag1s = 0; //1s定时标志
- unsigned char T0RH = 0; //T0重载值的高字节
- unsigned char T0RL = 0; //T0重载值的低字节
- ///////////////////////////////// 延时子程序
- void delay ()
- {
- x=99999;
- while(x--);
- }
- /////////////////////////////////
- void main() ////////////////主程序
- {
- bit tmp;
- unsigned char str[12];
- unsigned char DHT[5];
- ////初始化///////////////////////
- EA = 1; //开总中断
- ConfigTimer0(10); //T0定时10ms
- InitLcd1602(); //初始化液晶
- M1=0; M1=0;M2=0;M3=0;D1=0;D2=0;
- ////////////////////////////////////
- DHT_Start();
- delay();
- delay();
- delay();
- delay();
- while (1)
- {
- LcdShowStr(0, 0, "WD"); //显示到液晶屏上
- LcdShowStr(0, 1, "SD"); //显示到液晶屏上
- LcdShowStr(5, 0, "H"); //显示到液晶屏上
- LcdShowStr(5, 1, "H"); //显示到液晶屏上
-
- LcdShowStr(9, 0, "L"); //显示到液晶屏上
- LcdShowStr(9, 1, "L"); //显示到液晶屏上
- if (flag1s) //每秒更新一次温度
- {
-
-
- str[0] = (wdg/10)%10 + '0'; //十位转为ASCII码
- str[1] = (wdg%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(6, 0, str);
- str[0] = (wdd/10)%10 + '0'; //十位转为ASCII码
- str[1] = (wdd%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(10,0, str);
- str[0] = (sdg/10)%10 + '0'; //十位转为ASCII码
- str[1] = (sdg%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(6, 1, str);
- str[0] = (sdd/10)%10 + '0'; //十位转为ASCII码
- str[1] = (sdd%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(10, 1, str);
- DHT_Start();
- tmp=DHT_ByteRead(&DHT);
- if(tmp==1)
- {
- str[0] = (DHT[0]/10)%10 + '0'; //十位转为ASCII码
- str[1] = (DHT[0]%10) + '0'; //个位转为ASCII
- LcdShowStr(2, 1, str);
- str[0] = (DHT[2]/10)%10 + '0'; //十位转为ASCII码
- str[1] = (DHT[2]%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(2,0, str);
- WD= DHT[2];
- SD=DHT[0];
- }
- }
- ////////////////////超热警报
- if(wdg<=WD)
- {
- WDC=WD-wdg+2;
- M1=1;
- }
- else M1=0;
- if(wdd>=WD)
- {
- WDC=wdd-WD+2;
- M2=1;
- }
- else M2=0;
- if(sdg<=SD)
- {
- SDC=SD-sdg+2;
- M3=1;
- }
- else M3=0;
- if(sdd>=SD)
- {
- SDC=sdd-SD+2;
- M4=1;
- }
- else M4=0;
- if(wdg<=WD||wdd>=WD)
- {
- PWM1++;
- if(PWM1<WDC)
- D1=1;
- if(PWM1>=WDC)
- D1=0;
- if(PWM1==10)
- PWM1=0;
- }
- if(wdg>WD&&wdd<WD&&KK1==0)
- D1=0;
- if(sdg<=SD||sdd>=SD)
- {
- PWM2++;
- if(PWM2<SDC)
- D2=1;
- if(PWM2>=SDC)
- D2=0;
- if(PWM2==10)
- PWM2=0;
- }
- if(sdg>SD&&sdd<SD&&KK2==0)
- D2=0;
- /////////////////////////////按键设置
- aj();
- //////////////////////////////////////////////
- }
- }
- void aj (void)
- {
- if(KD1==0)
- {
- delay();
- KK1++;
- if(KK1==1)
- D1=1;
- if(KK1>=2)
- {
- D1=0;
- KK1=0;
- }
- }
- if(KD2==0)
- {
- delay();
- KK2++;
- if(KK2==1)
- D2=1;
- if(KK2>=2)
- {
- D2=0;
- KK2=0;
- }
- }
- if(K1==0)
- {
- xx++;
- delay();
- if(xx==1)
- LcdShowStr(12, 0, "WDH"); //显示到液晶屏上
- if(xx==2)
- LcdShowStr(12, 0, "WDL"); //显示到液晶屏上
- if(xx==3)
- LcdShowStr(12, 0, "SDH"); //显示到液晶屏上
- if(xx==4)
- LcdShowStr(12, 0, "SDL"); //显示到液晶屏上
- if(xx>=5)
- xx=0;
- }
- if(K2==0)
- {
- if(xx==1)
- {
- LcdShowStr(12, 1, "WD+"); //显示到液晶屏上
- wdg++;
- }
- if(xx==2)
- {
- LcdShowStr(12, 1, "WD+"); //显示到液晶屏上
- wdd++;
- }
- if(xx==3)
- {
- LcdShowStr(12, 1, "SD+"); //显示到液晶屏上
- sdg++;
- }
- if(xx==4)
- {
- LcdShowStr(12, 1, "SD+"); //显示到液晶屏上
- sdd++;
- }
- delay();
- }
- if(K3==0)
- {
- if(xx==1)
- {
- LcdShowStr(12, 1, "WD-"); //显示到液晶屏上
- wdg--;
- }
- if(xx==2)
- {
- LcdShowStr(12, 1, "WD-"); //显示到液晶屏上
- wdd--;
- }
- if(xx==3)
- {
- LcdShowStr(12, 1, "SD-"); //显示到液晶屏上
- sdg--;
- }
- if(xx==4)
- {
- LcdShowStr(12, 1, "SD-"); //显示到液晶屏上
- sdd--;
- }
- delay();
- }
- if(K4==0)
- {
- delay();
- xx=0;
- InitLcd1602(); //初始化液晶
- }
- }
- /* 整型数转换为字符串,str-字符串指针,dat-待转换数,返回值-字符串长度 */
- unsigned char IntToString(unsigned char *str, int dat)
- {
- signed char i = 0;
- unsigned char len = 0;
- unsigned char buf[6];
-
- if (dat < 0) //如果为负数,首先取绝对值,并在指针上添加负号
- {
- dat = -dat;
- *str++ = '-';
- len++;
- }
- do { //先转换为低位在前的十进制数组
- buf[i++] = dat % 10;
- dat /= 10;
- } while (dat > 0);
- len += i; //i最后的值就是有效字符的个数
- while (i-- > 0) //将数组值转换为ASCII码反向拷贝到接收指针上
- {
- *str++ = buf[i] + '0';
- }
- *str = '\0'; //添加字符串结束符
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
温湿度检测.zip
(198.88 KB, 下载次数: 2525)
|