我这个程序烧到单片机里,LCD显示一翻页第一页的湿度就自己跳为零,然后就报警,第二页的内容可以正常显示。
void main()
{
BUT=1;
uart_init();//初始化串口
lcd1602_init();
time0_init();
while(DHT11_Init()) //检测DHT11是否存在
{
lcd1602_show_string(0,0,"Error");
}
DHT11_Read_Data(&temp,&humi);
// lcd1602_show_string(0,0,"Temp: C");
// lcd1602_show_string(0,1,"Humi: %RH");
// lcd1602_show_string(0,0,"Wating......");
// delay_ms(2500);
while(1)
{
i++;
DHT11_Read_Data(&temp,&humi);
valuedata1=200-Adc0832(0);
delay_ms(100);
valuedata2=Adc0832(1);
key=key_scan(0);
if(key==KEY1_PRESS)//检测按键K1是否按下,按下进入设置阈值状态
{
LED2=0;
while(1) //温度上限设置
{
key=key_scan(0);
if(key==KEY2_PRESS)
{
temphigh++;
}
if(key==KEY3_PRESS)
{
temphigh--;
}
if(key==KEY1_PRESS)
{
break;
}
tempgoal_bufhigh[0]=temphigh/10+0x30;
tempgoal_bufhigh[1]=temphigh%10+0x30;
lcd1602_show_string(10,0,tempgoal_bufhigh);
}
while(1) //温度下限设置
{
key=key_scan(0);
if(key==KEY2_PRESS)
{
templow++;
}
if(key==KEY3_PRESS)
{
templow--;
}
if(key==KEY1_PRESS)
{
break;
}
tempgoal_buflow[0]=templow/10+0x30;
tempgoal_buflow[1]=templow%10+0x30;
lcd1602_show_string(13,0,tempgoal_buflow);
}
while(1) //湿度上限设置
{
key=key_scan(0);
if(key==KEY2_PRESS)
{
humihigh++;
}
if(key==KEY3_PRESS)
{
humihigh--;
}
if(key==KEY1_PRESS)
{
break;
}
humigoal_bufhigh[0]=humihigh/10+0x30;
humigoal_bufhigh[1]=humihigh%10+0x30;
lcd1602_show_string(10,1,humigoal_bufhigh);
}
while(1) //湿度下限设置
{
key=key_scan(0);
if(key==KEY2_PRESS)
{
humilow++;
}
if(key==KEY3_PRESS)
{
humilow--;
}
if(key==KEY1_PRESS)
{
break;
}
humigoal_buflow[0]=humilow/10+0x30;
humigoal_buflow[1]=humilow%10+0x30;
lcd1602_show_string(13,1,humigoal_buflow);
}
LED2=1;
}
if(keydata==0)
{
lcd1602_show_string(0,0,"Temp: C");
lcd1602_show_string(0,1,"Humi: %");
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;
temp_buf[2]='\0';
lcd1602_show_string(6,0,temp_buf);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
humi_buf[2]='\0';
lcd1602_show_string(6,1,humi_buf);
tempgoal_bufhigh[0]=temphigh/10+0x30;
tempgoal_bufhigh[1]=temphigh%10+0x30;
tempgoal_bufhigh[2]='\0';
tempgoal_buflow[0]=templow/10+0x30;
tempgoal_buflow[1]=templow%10+0x30;
tempgoal_buflow[2]='\0';
humigoal_bufhigh[0]=humihigh/10+0x30;
humigoal_bufhigh[1]=humihigh%10+0x30;
humigoal_bufhigh[2]='\0';
humigoal_buflow[0]=humilow/10+0x30;
humigoal_buflow[1]=humilow%10+0x30;
humigoal_buflow[2]='\0';
lcd1602_show_string(10,0,tempgoal_bufhigh);
lcd1602_show_string(13,0,tempgoal_buflow);
lcd1602_show_string(10,1,humigoal_bufhigh);
lcd1602_show_string(13,1,humigoal_buflow);
delay_ms(50);
}
if(keydata==1)
{
DHT11_Read_Data(&temp,&humi);
lcd1602_show_string(0,0,"GZ: LUX");
lcd1602_show_string(0,1,"YW: MMP");
CO_buf[0]=valuedata1/100+0x30;
CO_buf[1]=valuedata1%100/10+0x30;
CO_buf[2]=valuedata1%10+0x30;
CO_buf[3]='\0';
lcd1602_show_string(3,0,CO_buf);
CH_buf[0]=valuedata2/100+0x30;
CH_buf[1]=valuedata2%100/10+0x30;
CH_buf[2]=valuedata2%10+0x30;
CH_buf[3]='\0';
lcd1602_show_string(3,1,CH_buf);
delay_ms(50);
}
if(temp>=temphigh||temp<=templow||humi>=humilow||humi<humihigh||valuedata2>=10)
{
BEEP=0;
LED1=0;
}
else
{
BEEP=1;
LED1=1;
}
if(temp>=temphigh)
{
IN1=0;
}
else
{
IN1=1;
}
if(temp<=templow)
{
IN2=0;
}
else
{
IN2=1;
}
if(humi<=humilow)
{
IN3=0;
}
else
{
IN3=1;
}
if(humi>=humihigh)
{
IN4=0;
}
else
{
IN4=1;
}
if(valuedata2>=10)
{
IN5=0;
}
else
{
IN5=1;
}
delay_ms(1);
}
}
蓝牙程序:
void time0() interrupt 1 //定时器0中断函数
{
TH0=0XDC; //给定时器赋初值,定时10ms
TL0=0X00;
gmsec++;//10ms加1次
if(BUT==0)
{
delay_ms(500);
lcd1602_clear();
keydata=!keydata;
}
if(gmsec%500==0)//定时5秒
{
SendString("Temp:");
SendString(temp_buf);
SendString("C");
SendString("\r\n");
delay_ms(20);
SendString("Humi:");
SendString(humi_buf);
SendString("%");
SendString("\r\n");
delay_ms(20);
SendString("GZ:");
SendString("144");
SendString("LUX");
SendString("\r\n");
delay_ms(20);
SendString("YW:");
SendString(CH_buf);
SendString("MMP");
SendString("\r\n");
}
} |