蓝桥杯第五届模拟智能灌溉系统(HGS)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<STC15F2K60S2.H>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST=P1^3;
sbit SCL=P2^0;
sbit SD=P2^1;
uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
uchar one,two,three,four,five,six,seven,eight;
uchar code ds[]={0,30,8,1,1,1,17};
uchar time[7];
uchar humi,hold=50;
bit handle=0,set=0,inc,dec,warn=1,irri=0;
void Delay1ms();
void Delay5ms();
void allinit();
void keyscan();
void display(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six,uchar seven,uchar eight);
void dsinit();
void dsget();
uchar ADRead(uchar add);
void main()
{
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;
}
else if(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;
}
}
else if(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;}
}
else if((humi<hold)&&(warn==1))
{
if(irri==1)
{P2=0XA0; P0=0X50;}
else
{P2=0XA0; P0=0X40;}
}
else if(humi>hold)
{
if(irri==1)
{P2=0XA0; P0=0X10;}
else
{P2=0XA0; P0=0X00;}
}
}
display(one,two,three,four,five,six,seven,eight);
}
}
void keyscan()
{
if(P30==0)
{
Delay5ms();
if(P30==0)
{
handle=~handle;
}
while(!P30);
}
else if(P31==0)
{
Delay5ms();
if(P31==0)
{
if(handle==0)set=~set;
if(handle==1)warn=~warn;
}
while(!P31);
}
else if(P32==0)
{
Delay5ms();
if(P32==0)
{
if((handle==0)&&(set==1))inc=1;
if(handle==1)irri=1;
}
while(!P32);
}
else if(P33==0)
{
Delay5ms();
if(P33==0)
{
if((handle==0)&&(set==1))dec=1;
if(handle==1)irri=0;
}
while(!P33);
}
}
void iicdelay(uchar m)
{
do{_nop_();}
while(m--);
}
void iicstart()
{
SD=1;
_nop_();
SCL=1;
_nop_();
SD=0;
_nop_();
SCL=0;
_nop_();
}
void iicstop()
{
SD=0;
_nop_();
SCL=1;
_nop_();
SD=1;
_nop_();
}
uchar ack()
{
SCL=1;
iicdelay(5);
if(SD==1)
{
SCL=0;
iicstop();
return 0;
}
else
{
SCL=0;
return 1;
}
}
void iicwritebyte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCL=0;
SD=dat&0X80;
SCL=1;
dat<<=1;
}
SCL=0;
}
uchar iicreadbyte()
{
uchar i,dat;
for(i=0;i<8;i++)
{
SCL=1;
iicdelay(5);
dat<<=1;
if(SD)
{
dat|=0X01;
}
SCL=0;
}
return dat;
}
uchar ADRead(uchar add)
{
uchar date;
iicstart();
iicwritebyte(0X90);
ack();
iicwritebyte(add);
ack();
iicstop();
iicst
art();
iicwritebyte(0X91);
ack();
date=iicreadbyte();
ack();
iicstop();
date=0.39*date;
return date;
}
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte((dat/10<<4)|(dat%10));
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,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;
return temp;
}
void dsinit()
{
uchar i,add=0X80;
Write_Ds1302(0X8E,0X00);
for(i=0;i<7;i++)
{
Write_Ds1302(add,ds[i]);
add=add+2;
}
Write_Ds1302(0X8E,0X80);
}
void dsget()
{
uchar i,add=0X81;
Write_Ds1302(0X8E,0X00);
for(i=0;i<7;i++)
{
time[i]=Read_Ds1302 ( add );
add=add+2;
}
Write_Ds1302(0X8E,0X80);
}
void display(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six,uchar seven,uchar eight)
{
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();
}
void allinit()
{
P2=0XA0; P0=0X00;
P2=0X80; P0=0XFF;
P2=0XC0; P0=0XFF;
P2=0XE0; P0=0XFF;
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}