我這個程序燒到單片機里,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");
}
} |