以AT89C52为主控芯片,以DHT11为温湿度传感器,用LCD1602显示数值,当湿度过高时,启动电风扇(用L298N驱动直流电机模拟),当温度过低时,启动电热管(用OVEN器件模拟)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
Proteus8.6
单片机源程序如下:
- #include <reg52.h> /////////////头文件
- sbit IN1 = P2^3;
- sbit IN2 = P2^4;
- sbit ENA = P2^5;
- sbit Res = P2^6; //电热丝控制口
- sbit Key = P2^7; //模拟光电门
- ////////////////////////////////////////////////////////////////////////////
- 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 void LcdWriteCmd(unsigned char cmd);
- extern void LcdWriteDat(unsigned char dat);
- extern bit Start18B20();
- extern bit Get18B20Temp(int *temp);
- extern void InitLcd1602();
- bit DHT_Start();
- bit DHT_ByteRead(unsigned char *dat);
- ///////////////////////////// 数组命名
- int Humity,Tempature,x;
- int Count = 0; //工具计数值
- bit flag1s = 0; //1s定时标志
- unsigned char T0RH = 0; //T0重载值的高字节
- unsigned char T0RL = 0; //T0重载值的低字节
- ///////////////////////////////// 延时子程序
- void delay()
- {
- x=99999;
- while(x--);
- }
- void Key_Scan( void )
- {
- if( Key == 0 )
- {
- delay();
- if( Key == 0 )
- {
- Count++;
- if( Count > 20 )
- {
- Count = 0;
- }
- }
- while(!Key);
- }
- }
- /////////////////////////////////
- void main() ////////////////主程序
- {
- int Tempature = 0;
- int Humidy = 0;
- bit tmp;
- unsigned char Decade = 0;
- unsigned char Digtal = 0;
- unsigned char str[12];
- unsigned char DHT[5];
- ////初始化///////////////////////
- EA = 1; //开总中断
- ConfigTimer0(10); //T0定时10ms
- InitLcd1602(); //初始化液晶
- ////////////////////////////////////
- DHT_Start();
- delay();
- delay();
- delay();
-
- delay();
- ENA = 0;
- IN1 = 1;
- IN2 = 0;
- Res = 0;
- LcdShowStr(0, 0, "Temp:"); //显示到液晶屏上
- LcdShowStr(0, 1, "Humi:"); //显示到液晶屏上
- LcdShowStr(8, 0, "Num:"); //显示到液晶屏上
- while (1)
- {
- if (flag1s) //每秒更新一次温度
- {
- 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(5, 1, str);
-
- str[0] = (DHT[2]/10)%10 + '0'; //十位转为ASCII码
- str[1] = (DHT[2]%10) + '0'; //个位转为ASCII
- str[2] = '\0';
- LcdShowStr(5,0, str);
-
- Tempature= DHT[2];
- Humity=DHT[0];
- if( Tempature >= 24 )
- {
- Res = 0;
- }
- else if ( Tempature < 24 )
- {
- Res = 1;
- }
- /*********************************/
- if( Humity >= 60 )
- {
- ENA = 1;
- }
- else if ( Humity < 60 )
- {
- ENA = 0;
- }
- }
- }
- Key_Scan();
- Decade = Count/10;
- Digtal = Count%10;
- LcdWriteCmd(0x80+12);
- LcdWriteDat(0x30+Decade);
- LcdWriteDat(0x30+Digtal);
- }
- }
- /* 整型数转换为字符串,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'; //添加字符串结束符
-
- return len; //返回字符串长度
- }
- /* 配置并启动T0,ms-T0定时时间 */
- void ConfigTimer0(unsigned int ms)
- {
- unsigned long tmp; //临时变量
-
- tmp = 11059200 / 12; //定时器计数频率
- tmp = (tmp * ms) / 1000; //计算所需的计数值
- tmp = 65536 - tmp; //计算定时器重载值
- tmp = tmp + 12; //补偿中断响应延时造成的误差
- T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0; //清零T0的控制位
- TMOD |= 0x01; //配置T0为模式1
- TH0 = T0RH; //加载T0重载值
- TL0 = T0RL;
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- }
- /* T0中断服务函数,完成1秒定时 */
- void InterruptTimer0() interrupt 1
- {
- static unsigned char tmr1s = 0;
-
- TH0 = T0RH; //重新加载重载值
- TL0 = T0RL;
- tmr1s++;
- if (tmr1s >= 100) //定时1s
- {
- tmr1s = 0;
- flag1s = 1;
- }
-
- }
复制代码
仿真51hei下载地址:
温湿度检测.zip
(208.99 KB, 下载次数: 78)
|