2019-07-18 08:55发布
jjy1039 发表于 2018-3-30 15:14 程序调试了吗,先调试程序吧,仿真的硬件一般不会有什么问题
勇哥-lzu 发表于 2018-3-30 18:10 程序跑飞了吧 贴上代码看一下
最多设置5个标签!
不知道程序哪里有问题- -
- 勇哥-lzu 发表于 2018-3-30 18:10
- 程序跑飞了吧 贴上代码
- 好的
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sfr ISP_DATA = 0xe2;
- sfr ISP_ADDRH = 0xe3;
- sfr ISP_ADDRL = 0xe4;
- sfr ISP_CMD = 0xe5;
- sfr ISP_TRIG = 0xe6;
- sfr ISP_CONTR = 0xe7;
- sbit LcdRs_P = P1^2;
- sbit LcdRw_P = P1^3;
- sbit LcdEn_P = P1^4;
- sbit Buzzer_P = P1^5;
- sbit DHT11_P = P1^6;
- sbit DHT11_1_P = P1^7;
- sbit KeySet_P = P3^2;
- sbit KeyDown_P = P3^3;
- sbit KeyUp_P = P3^4;
- sbit LedTH_P = P2^0;
- sbit LedTL_P = P2^1;
- sbit LedHH_P = P2^2;
- sbit LedHL_P = P2^3;
- sbit W_P = P2^4;
- sbit W_P_1 = P2^5;
- uchar temp;
- uchar humi;
- uchar temp_1;
- uchar humi_1;
- uchar AlarmTL;
- uchar AlarmTH;
- uchar AlarmHL;
- uchar AlarmHH;
- void ISP_Disable()
- {
- ISP_CONTR = 0;
- ISP_ADDRH = 0;
- ISP_ADDRL = 0;
- }
- unsigned char EEPROM_Read(unsigned int add)
- {
- ISP_DATA = 0x00;
- ISP_CONTR = 0x83;
- ISP_CMD = 0x01;
- ISP_ADDRH = (unsigned char)(add>>8);
- ISP_ADDRL = (unsigned char)(add&0xff);
- ISP_TRIG = 0x46;
- ISP_TRIG = 0xB9;
- _nop_();
- ISP_Disable();
- return (ISP_DATA);
- }
- void EEPROM_Write(unsigned int add,unsigned char ch)
- {
- ISP_CONTR = 0x83;
- ISP_CMD = 0x02;
- ISP_ADDRH = (unsigned char)(add>>8);
- ISP_ADDRL = (unsigned char)(add&0xff);
- ISP_DATA = ch;
- ISP_TRIG = 0x46;
- ISP_TRIG = 0xB9;
- _nop_();
- ISP_Disable();
- }
- void Sector_Erase(unsigned int add)
- {
- ISP_CONTR = 0x83;
- ISP_CMD = 0x03;
- ISP_ADDRH = (unsigned char)(add>>8);
- ISP_ADDRL = (unsigned char)(add&0xff);
- ISP_TRIG = 0x46;
- ISP_TRIG = 0xB9;
- _nop_();
- ISP_Disable();
- }
- void DelayMs(uint time)
- {
- uint i,j;
- for(i=0;i<time;i++)
- for(j=0;j<112;j++);
- }
- void LcdWriteCmd(uchar cmd)
- {
- LcdRs_P = 0;
- LcdRw_P = 0;
- LcdEn_P = 0;
- P0=cmd;
- DelayMs(2);
- LcdEn_P = 1;
- DelayMs(2);
- LcdEn_P = 0;
- }
- void LcdWriteData(uchar dat)
- {
- LcdRs_P = 1;
- LcdRw_P = 0;
- LcdEn_P = 0;
- P0=dat;
- DelayMs(2);
- LcdEn_P = 1;
- DelayMs(2);
- LcdEn_P = 0;
- }
- void LcdInit()
- {
- LcdWriteCmd(0x38);
- LcdWriteCmd(0x0C);
- LcdWriteCmd(0x06);
- LcdWriteCmd(0x01);
- }
- void LcdGotoXY(uchar line,uchar column)
- {
-
- if(line==0)
- LcdWriteCmd(0x80+column);
-
- if(line==1)
- LcdWriteCmd(0x80+0x40+column);
- }
- void LcdPrintStr(uchar *str)
- {
- while(*str!=' ')
- LcdWriteData(*str++);
- }
- void LcdPrintNum(uchar num)
- {
- LcdWriteData(num/10+48);
- LcdWriteData(num%10+48);
- }
- void LcdShowInit()
- {
- LcdGotoXY(0,0);
- LcdPrintStr(" DHT11 System ");
- LcdGotoXY(1,0);
- LcdPrintStr("T: C H: %RH");
- LcdGotoXY(1,4);
- LcdWriteData(0xdf);
- }
- void Delay10us()
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- uchar DhtReadByte(void)
- {
- bit bit_i;
- uchar j;
- uchar dat=0;
- for(j=0;j<8;j++)
- {
- while(!DHT11_P);
- Delay10us();
- Delay10us();
- Delay10us();
- if(DHT11_P==1)
- {
- bit_i=1;
- while(DHT11_P);
- }
- else
- {
- bit_i=0;
- }
- dat<<=1;
- dat|=bit_i;
- }
- return(dat);
- }
- void ReadDhtData()
- {
- uchar HumiHig;
- uchar HumiLow;
- uchar TemHig;
- uchar TemLow;
- uchar check;
-
- DHT11_P=0;
- DelayMs(20);
- DHT11_P=1;
- Delay10us();
- Delay10us();
- Delay10us();
- while(!DHT11_P);
- while(DHT11_P);
- HumiHig = DhtReadByte();
- HumiLow = DhtReadByte();
- TemHig = DhtReadByte();
- TemLow = DhtReadByte();
- check = DhtReadByte();
- DHT11_P=1;
- if(check==HumiHig + HumiLow + TemHig + TemLow)
- {
- temp=TemHig;
- humi=HumiHig;
- }
- }
- uchar Dht_1ReadByte(void)
- {
- bit bit_i;
- uchar j;
- uchar dat_1=0;
- for(j=0;j<8;j++)
- {
- while(!DHT11_1_P);
- Delay10us();
- Delay10us();
- Delay10us();
- if(DHT11_1_P==1)
- {
- bit_i=1;
- while(DHT11_1_P);
- }
- else
- {
- bit_i=0;
- }
- dat_1<<=1;
- dat_1|=bit_i;
- }
- return(dat_1);
- }
- void ReadDht_1Data()
- {
- uchar HumiHig_1;
- uchar HumiLow_1;
- uchar TemHig_1;
- uchar TemLow_1;
- uchar check_1;
-
- DHT11_1_P=0;
- DelayMs(20);
- DHT11_1_P=1;
- Delay10us();
- Delay10us();
- Delay10us();
- while(!DHT11_1_P);
- while(DHT11_1_P);
- HumiHig_1 = Dht_1ReadByte();
- HumiLow_1 = Dht_1ReadByte();
- TemHig_1 = Dht_1ReadByte();
- TemLow_1 = Dht_1ReadByte();
- check_1 = Dht_1ReadByte();
- DHT11_1_P=1;
- if(check_1==HumiHig_1 + HumiLow_1 + TemHig_1 + TemLow_1)
- {
- temp_1=TemHig_1;
- humi_1=HumiHig_1;
- }
- }
- void AlarmJudge(void)
- {
- uchar i;
- if(temp>AlarmTH)
- {
- if(temp>temp_1)
- {
- W_P_1 = 0;
- LedTH_P=1;
- LedTL_P=1;
- }
- else
- {
- LedTH_P=0;
- LedTL_P=1;
- W_P_1 = 1;
- }
- }
- else if(temp<AlarmTL)
- {
- if(temp<temp_1)
- {
- W_P_1 = 0;
- LedTL_P = 1;
- LedTH_P = 1;
- }
- else
- {
- LedTL_P= 0;
- LedTH_P= 1;
- W_P_1 = 1;
- }
- }
- else
- {
- LedTH_P= 1;
- LedTL_P= 1;
- W_P_1= 1;
- }
- if(humi>AlarmHH)
- {
- if(humi>humi_1)
- {
- W_P = 0;
- LedHH_P= 1;
- LedHL_P= 1;
- }
- else
- {
- W_P = 1;
- LedHH_P= 0;
- LedHL_P= 1;
- }
- }
- else if(humi<AlarmHL)
- {
- if(humi<humi_1)
- {
- W_P = 0;
- LedHL_P= 1;
- LedHH_P= 1;
- }
- else
- {
- W_P = 1;
- LedHL_P= 0;
- LedHH_P= 1;
- }
- }
- else
- {
- LedHH_P=1;
- LedHL_P=1;
- W_P = 1;
- }
- if((LedHH_P==0)||(LedHL_P==0)||(LedTH_P==0)||(LedTL_P==0)||(W_P==0)||(W_P_1==0))
- {
- for(i=0;i<3;i++)
- {
- Buzzer_P=0;
- DelayMs(100);
- Buzzer_P=1;
- DelayMs(100);
- }
- }
- }
- void KeyScanf()
- {
- if(KeySet_P==0)
- {
-
- LcdWriteCmd(0x01);
- LcdGotoXY(0,0);
- LcdPrintStr("Temp: - ");
- LcdGotoXY(1,0);
- LcdPrintStr("Humi: - ");
-
- LcdGotoXY(0,6);
- LcdPrintNum(AlarmTL);
- LcdGotoXY(0,9);
- LcdPrintNum(AlarmTH);
- LcdGotoXY(1,6);
- LcdPrintNum(AlarmHL);
- LcdGotoXY(1,9);
- LcdPrintNum(AlarmHH);
- LcdGotoXY(0,7);
- LcdWriteCmd(0x0F);
-
- DelayMs(10);
- while(!KeySet_P);
- DelayMs(10);
- while(KeySet_P)
- {
- if(KeyDown_P==0)
- {
- if(AlarmTL>0)
- AlarmTL--;
- LcdGotoXY(0,6);
- LcdPrintNum(AlarmTL);
- LcdGotoXY(0,7);
- DelayMs(350);
- }
- if(KeyUp_P==0)
- {
- if(AlarmTL<99)
- AlarmTL++;
- LcdGotoXY(0,6);
- LcdPrintNum(AlarmTL);
- LcdGotoXY(0,7);
- DelayMs(350);
- }
- }
- LcdGotoXY(0,10);
- DelayMs(10);
- while(!KeySet_P);
- DelayMs(10);
-
- while(KeySet_P)
- {
- if(KeyDown_P==0)
- {
- if(AlarmTH>0)
- AlarmTH--;
- LcdGotoXY(0,9);
- LcdPrintNum(AlarmTH);
- LcdGotoXY(0,10);
- DelayMs(350);
- }
- if(KeyUp_P==0)
- {
- if(AlarmTH<99)
- AlarmTH++;
- LcdGotoXY(0,9);
- LcdPrintNum(AlarmTH);
- LcdGotoXY(0,10);
- DelayMs(350);
- }
- }
- LcdGotoXY(1,7);
- DelayMs(10);
- while(!KeySet_P);
- DelayMs(10);
-
-
- while(KeySet_P)
- {
- if(KeyDown_P==0)
- {
- if(AlarmHL>0)
- AlarmHL--;
- LcdGotoXY(1,6);
- LcdPrintNum(AlarmHL);
- LcdGotoXY(1,7);
- DelayMs(350);
- }
- if(KeyUp_P==0)
- {
- if(AlarmHL<99)
- AlarmHL++;
- LcdGotoXY(1,6);
- LcdPrintNum(AlarmHL);
- LcdGotoXY(1,7);
- DelayMs(350);
- }
- }
- LcdGotoXY(1,10);
- DelayMs(10);
- while(!KeySet_P);
- DelayMs(10);
-
- while(KeySet_P)
- {
- if(KeyDown_P==0)
- {
- if(AlarmHH>0)
- AlarmHH--;
- LcdGotoXY(1,9);
- LcdPrintNum(AlarmHH);
- LcdGotoXY(1,10);
- DelayMs(350);
- }
- if(KeyUp_P==0)
- {
- if(AlarmHH<99)
- AlarmHH++;
- LcdGotoXY(1,9);
- LcdPrintNum(AlarmHH);
- LcdGotoXY(1,10);
- DelayMs(350);
- }
- }
- LcdWriteCmd(0x0C);
- LcdShowInit();
- DelayMs(10);
- while(!KeySet_P);
- DelayMs(10);
- Sector_Erase(0x2000);
- EEPROM_Write(0x2000,AlarmTL);
- EEPROM_Write(0x2001,AlarmTH);
- EEPROM_Write(0x2002,AlarmHL);
- EEPROM_Write(0x2003,AlarmHH);
- }
- }
- void main()
- {
- uchar i;
- LcdInit();
- LcdShowInit();
- AlarmTL=EEPROM_Read(0x2000);
- AlarmTH=EEPROM_Read(0x2001);
- AlarmHL=EEPROM_Read(0x2002);
- AlarmHH=EEPROM_Read(0x2003);
- if((AlarmTL==0)||(AlarmTL>100))
- AlarmTL=20;
- if((AlarmTH==0)||(AlarmTH>100))
- AlarmTH=35;
- if((AlarmHL==0)||(AlarmHL>100))
- AlarmHL=40;
- if((AlarmHH==0)||(AlarmHH>100))
- AlarmHH=55;
-
-
- while(1)
- {
- ReadDhtData(); // 读DHT11(内)数据
- DelayMs(100);
- ReadDht_1Data(); // 读DHT11(外)数据
- LcdGotoXY(1,2);
- LcdPrintNum(temp);
- LcdGotoXY(1,11);
- LcdPrintNum(humi);
-
- AlarmJudge(); // 报警
- for(i=0;i<25;i++)
- {
- KeyScanf();
- DelayMs(50);
- }
- }
- }
复制代码一周热门 更多>