LCD显示正常应该是是温度Wd:00 H00 L00,湿度Sd:00% H00% L00% ,但是仿真图显示出来的是不对的;另外就是按键函数那块,按键不起温湿度选择加减,而且对应的LED报警也是不对的;最后就是DHT11温湿度传感也可能有问题。全部代码如下,希望有大佬可以解决一下。
单片机源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #include<intrins.h>
- sbit beep = P1^0;
- uchar a_a;
- bit flag_300ms ;
- uchar key_can;
- sbit wh_led = P1^4;
- sbit wl_led = P1^3;
- sbit sh_led = P1^5;
- sbit sl_led = P1^6;
- uchar flag_en = 1;
- uchar menu_1;
- uint t_high = 30,t_low = 10;
- uint s_high = 30,s_low = 10;
- void delay_1ms(uint q)
- {
- uint i,j;
- for(i=0;i<q;i++)
- for(j=0;j<120;j++);
- }
- sbit dht11=P1^1;
- uchar table_dht11[5]={1,2,3,4,5};
- uchar code table_num[]="0123456789abcdefg";
- sbit rs=P2^0;
- sbit rw=P2^1;
- sbit e =P2^2;
- void delay_uint(uint q)
- {
- while(q--);
- }
- void write_com(uchar com)
- {
- e=0;
- rs=0;
- rw=0;
- P0=com;
- delay_uint(25);
- e=1;
- delay_uint(100);
- e=0;
- }
- void write_data(uchar dat)
- {
- e=0;
- rs=1;
- rw=0;
- P0=dat;
- delay_uint(25);
- e=1; //éÏéyÑØÖ′DD
- delay_uint(100);
- e=0;
- }
- void write_zifu(uchar hang,uchar add,uchar date)
- {
- if(hang==1)
- write_com(0x80+add);
- else
- write_com(0x80+0x40+add);
- write_data(date);
- }
- void write_sfm2(uchar hang,uchar add,uint date)
- {
- if(hang==1)
- write_com(0x80+add);
- else
- write_com(0x80+0x40+add);
- write_data(0x30+date/10%10);
- write_data(0x30+date%10);
- }
- void write_string(uchar hang,uchar add,uchar *p)
- {
- if(hang==1)
- write_com(0x80+add);
- else
- write_com(0x80+0x40+add);
- while(1)
- {
- if(*p == '\0') break;
- write_data(*p);
- p++;
- }
- }
- void init_1602() //LCD16023õê¼»ˉoˉêy
- {
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- delay_uint(1000);
- write_string(1,0,"Wd:00 H00 L00 ");
- write_string(2,0,"Sd:00% H00% L00% ");
- write_zifu(1,5,0xdf);
- write_zifu(1,10,0xdf);
- write_zifu(1,15,0xdf);
- write_sfm2(1,3,table_dht11[2]);
- write_sfm2(2,3,table_dht11[0]);
- write_sfm2(1,7,t_high);
- write_sfm2(2,7,s_high);
- write_sfm2(1,10,t_low);
- write_sfm2(2,10,s_low);
- }
- void time_init()
- {
- EA = 1;
- TMOD = 0X01;
- ET0 = 1;
- TR0 = 1;
- }
- uchar key_can; //°′¼ü
- void key()
- {
- static uchar key_new;
- key_can = 20;
- P3 |= 0xf0; //¸ßËÄλ
- if((P3 & 0xf0) != 0xf0)
- {
- delay_1ms(1);
- if(((P3 & 0xf0) != 0xf0) && (key_new == 1))
- {
- key_new = 0;
- switch(P3 & 0xf0)
- {
- case 0xd0: key_can = 1; break;
- case 0xb0: key_can = 2; break;
- case 0x70: key_can = 3; break;
- }
- }
- }
- else
- key_new = 1;
- }
- void key_with()
- {
- if(key_can == 1)
- {
- menu_1 ++;
- if(menu_1 > 2)
- {
- menu_1 = 0;
- init_1602();
- }
- }
- if(menu_1 == 1)
- {
- if(key_can == 2)
- {
- t_high ++ ;
- if(t_high > 99)
- t_high = 99;
- }
- if(key_can == 3)
- {
- t_high -- ;
- if(t_high <= t_low)
- t_high = t_low + 1;
- }
- write_sfm2(1,8,t_high);
- write_com(0x80+8);
- write_com(0x0f);
- }
- if(menu_1 == 2)
- {
- if(key_can == 2)
- {
- t_low ++ ;
- if(t_low >= t_high)
- t_low = t_high - 1;
- }
- if(key_can == 3)
- {
- t_low -- ;
- if(t_low <= 1)
- t_low = 1;
- }
- write_sfm2(1,13,t_low);
- write_com(0x80+13);
- write_com(0x0f);
- }
- if(menu_1 == 3)
- {
- if(key_can == 2)
- {
- s_high ++ ;
- if(s_high > 99)
- s_high = 99;
- }
- if(key_can == 3)
- {
- s_high -- ;
- if(s_high <= s_low)
- s_high = s_low + 1;
- }
- write_sfm2(2,8,s_high);
- write_com(0x80+0x40+8);
- write_com(0x0f);
- }
- if(menu_1 == 4)
- {
- if(key_can == 2)
- {
- s_low ++ ;
- if(s_low >= s_high)
- s_low = s_high - 1;
- }
- if(key_can == 3)
- {
- s_low -- ;
- if(s_low <= 1)
- s_low = 1;
- }
- write_sfm2(2,13,s_low);
- write_com(0x80+0x40+13);
- write_com(0x0f);
- }
- }
- void dst11()
- {
- uchar i,j;
- dht11 = 0;
- delay_1ms(5);
- dht11 = 1;
- delay_uint(4);
- if(dht11 == 0)
- {
- while(dht11 == 0);
- while(dht11 == 1);
- for(i=0;i<5;i++)
- {
- for(j=0;j<8;j++)
- {
- table_dht11[i] <<= 1;
- while(dht11 == 1);
- delay_uint(4);
- if(dht11 == 1)
- {
- table_dht11[i] |= 0x01;
- while(dht11 == 1);
- }
- }
- }
- }
- dht11 = 1;
- }
- void clock_h_l()
- {
- static uchar value,value1,value2,value3,value4;
- if((table_dht11[2] >= t_high))
- {
- value1 ++;
- if(value1 > 2)
- {
- wh_led = 0;
- }
- }
- else
- {
- value1 = 0;
- wh_led = 1;
- }
- if((table_dht11[2] <= t_low))
- {
- value2 ++;
- if(value2 > 2)
- {
- wl_led = 0;
- }
- }
- else
- {
- value2 = 0;
- wl_led = 1;
- }
- if((table_dht11[0] >= s_high))
- {
- value3 ++;
- if(value3 > 2)
- {
- sh_led = 0;
- }
- }else
- {
- value3 = 0;
- sh_led = 1;
- }
- if((table_dht11[0] <= s_low))
- {
- value4 ++;
- if(value4 > 2)
- {
- sl_led = 0;
- }
- }else
- {
- value4 = 0;
- sl_led = 1;
- }
- if((wh_led == 0) || (wl_led == 0) || (sh_led == 0) || (sl_led == 0))
- {
- value ++;
- if(value >= 2)
- if(flag_en == 1)
- beep = ~beep;
- }
- else
- {
- beep = 1;
- value = 0;
- flag_en = 1;
- }
- }
- void main()
- {
- beep = 0;
- delay_1ms(150);
- P0 = P1 = P2 = P3 = 0xff;
- time_init();
- init_1602();
- dst11();
- while(1)
- {
- if(flag_300ms == 1)
- {
- flag_300ms = 0;
- dst11();
- write_sfm2(2,3,table_dht11[0]);
- write_sfm2(1,3,table_dht11[2]);
- clock_h_l();
- }
- key();
- if(key_can < 10)
- {
- key_with();
- if(key_can == 3)
- {
- flag_en = 0;
- }
- }
- delay_1ms(1);
- }
- }
- void time0_int() interrupt 1
- {
- static uchar value;
- TH0 = 0x3c;
- TL0 = 0xb0;
- value ++;
- if(value % 6 == 0)
- {
- flag_300ms = 1;
- value = 0;
- }
- }
复制代码
|