您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > 蓝桥杯第五届模拟智能灌溉系统(HGS)
#include<STC15F2K60S2.H>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedintsbitSCK=P1^7;sbitSDA=P2^3;sbitRST=P1^3;sbitSCL=P2^0;sbitSD=P2^1;ucharcodetab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};ucharone,two,three,four,five,six,seven,eight;ucharcodeds[]={0,30,8,1,1,1,17};uchartime[7];ucharhumi,hold=50;bithandle=0,set=0,inc,dec,warn=1,irri=0;voidDelay1ms();voidDelay5ms();voidallinit();voidkeyscan();voiddisplay(ucharone,uchartwo,ucharthree,ucharfour,ucharfive,ucharsix,ucharseven,uchareight);voiddsinit();voiddsget();ucharADRead(ucharadd);voidmain(){allinit();dsinit();while(1){keyscan();humi=ADRead(0X03);dsget();if(handle==0){P2=0X80;P0=0XFE;if(set==0){one=time[2]/10;two=time[2]%10;three=10;four=time[1]/10;five=time[1]%10;six=11;seven=humi/10;eight=humi%10;}elseif(set==1){if(inc==1){inc=0;hold=hold+1;}if(dec==1){dec=0;hold=hold-1;}one=10;two=10;three=11;four=11;five=11;six=11;seven=hold/10;eight=hold%10;}}elseif(handle==1){P2=0X80;P0=0XFD;one=time[2]/10;two=time[2]%10;three=10;four=time[1]/10;five=time[1]%10;six=11;seven=humi/10;eight=humi%10;if((humi<hold)&&(warn==0)){if(irri==1){P2=0XA0;P0=0X10;}else{P2=0XA0;P0=0X00;}}elseif((humi<hold)&&(warn==1)){if(irri==1){P2=0XA0;P0=0X50;}else{P2=0XA0;P0=0X40;}}elseif(humi>hold){if(irri==1){P2=0XA0;P0=0X10;}else{P2=0XA0;P0=0X00;}}}display(one,two,three,four,five,six,seven,eight);}}voidkeyscan(){if(P30==0){Delay5ms();if(P30==0){handle=~handle;}while(!P30);}elseif(P31==0){Delay5ms();if(P31==0){if(handle==0)set=~set;if(handle==1)warn=~warn;}while(!P31);}elseif(P32==0){Delay5ms();if(P32==0){if((handle==0)&&(set==1))inc=1;if(handle==1)irri=1;}while(!P32);}elseif(P33==0){Delay5ms();if(P33==0){if((handle==0)&&(set==1))dec=1;if(handle==1)irri=0;}while(!P33);}}voidiicdelay(ucharm){do{_nop_();}while(m--);}voidiicstart(){SD=1;_nop_();SCL=1;_nop_();SD=0;_nop_();SCL=0;_nop_();}voidiicstop(){SD=0;_nop_();SCL=1;_nop_();SD=1;_nop_();}ucharack(){SCL=1;iicdelay(5);if(SD==1){SCL=0;iicstop();return0;}else{SCL=0;return1;}}voidiicwritebyte(uchardat){uchari;for(i=0;i<8;i++){SCL=0;SD=dat&0X80;SCL=1;dat<<=1;}SCL=0;}uchariicreadbyte(){uchari,dat;for(i=0;i<8;i++){SCL=1;iicdelay(5);dat<<=1;if(SD){dat|=0X01;}SCL=0;}returndat;}ucharADRead(ucharadd){uchardate;iicstart();iicwritebyte(0X90);ack();iicwritebyte(add);ack();iicstop();iicstart();iicwritebyte(0X91);ack();date=iicreadbyte();ack();iicstop();date=0.39*date;returndate;}voidWrite_Ds1302_Byte(unsignedchartemp){unsignedchari;for(i=0;i<8;i++){SCK=0;SDA=temp&0x01;temp>>=1;SCK=1;}}voidWrite_Ds1302(unsignedcharaddress,unsignedchardat){RST=0;_nop_();SCK=0;_nop_();RST=1;_nop_();Write_Ds1302_Byte(address);Write_Ds1302_Byte((dat/10<<4)|(dat%10));RST=0;}unsignedcharRead_Ds1302(unsignedcharaddress){unsignedchari,temp=0X00,dat1,dat2;RST=0;_nop_();SCK=0;_nop_();RST=1;_nop_();Write_Ds1302_Byte(address);for(i=0;i<8;i++){SCK=0;temp>>=1;if(SDA)temp|=0x80;SCK=1;}RST=0;_nop_();RST=0;SCK=0;_nop_();SCK=1;_nop_();SDA=0;_nop_();SDA=1;_nop_();dat1=temp/16;dat2=temp%16;temp=dat1*10+dat2;returntemp;}voiddsinit(){uchari,add=0X80;Write_Ds1302(0X8E,0X00);for(i=0;i<7;i++){Write_Ds1302(add,ds[i]);add=add+2;}Write_Ds1302(0X8E,0X80);}voiddsget(){uchari,add=0X81;Write_Ds1302(0X8E,0X00);for(i=0;i<7;i++){time[i]=Read_Ds1302(add);add=add+2;}Write_Ds1302(0X8E,0X80);}voiddisplay(ucharone,uchartwo,ucharthree,ucharfour,ucharfive,ucharsix,ucharseven,uchareight){P2=0XC0;P0=0X01;P2=0XE0;P0=tab[one];Delay1ms();P2=0XC0;P0=0X02;P2=0XE0;P0=tab[two];Delay1ms();P2=0XC0;P0=0X04;P2=0XE0;P0=tab[three];Delay1ms();P2=0XC0;P0=0X08;P2=0XE0;P0=tab[four];Delay1ms();P2=0XC0;P0=0X10;P2=0XE0;P0=tab[five];Delay1ms();P2=0XC0;P0=0X20;P2=0XE0;P0=tab[six];Delay1ms();P2=0XC0;P0=0X40;P2=0XE0;P0=tab[seven];Delay1ms();P2=0XC0;P0=0X80;P2=0XE0;P0=tab[eight];Delay1ms();}voidallinit(){P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;}voidDelay1ms()//@11.0592MHz{unsignedchari,j;_nop_();_nop_();_nop_();i=11;j=190;do{while(--j);}while(--i);}voidDelay5ms()//@11.0592MHz{unsignedchari,j;i=54;j=199;do{while(--j);}while(--i);}
本文标题:蓝桥杯第五届模拟智能灌溉系统(HGS)
链接地址:https://www.777doc.com/doc-2399871 .html