#include<STC15F2K60S2.H>
#include"ds1302.h"
#include"iic.h"
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
unsigned char dat_rd2;
unsigned char kaiguan=0;
unsigned char shidu=50; //初始湿度
unsigned char nowshidu; //当前湿度
unsigned char code table[]={0xc0,0xf9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0x7f};
unsigned char write[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char read[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char Time[]={0x16,0x30,0x08,0x02,0x16,0x02,0x21};
void delayms(unsigned char x)
{
unsigned char i;
while(x--)
{
for(i=0;i<113;i++);
}
}
void select (unsigned char n)
{
switch(n)
{
case 4:
P2=(P2&0x1f)|0x80;
break;
case 5:
P2=(P2&0x1f)|0xa0;
break;
case 6:
P2=(P2&0x1f)|0xc0;
break;
case 7:
P2=(P2&0x1f)|0xe0;
break;
}
}
void guanbi()
{
select(5);
P0=0x00;
select(4);
P0=0xff;
}
void DS1302_write()
{
unsigned char i;
Ds1302_Single_Byte_Write(0x8e,0x00);
for(i=0;i<7;i++)
{
Ds1302_Single_Byte_Write(write[i],Time[i]);
}
Ds1302_Single_Byte_Write(0x8e,0x80);
}
void DS1302_read()
{
unsigned char i;
for(i=0;i<7;i++)
{
Time[i]=Ds1302_Single_Byte_Read(read[i]);
}
}
void read_rd2()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat_rd2=IIC_RecByte();
IIC_WaitAck();
IIC_Ack(0);
IIC_Stop();
}
void shuma(unsigned char pos,unsigned char date)
{
select(6);
P0=0x01<<pos;
select(7);
P0=date;
}
void dis ()
{
nowshidu=dat_rd2*20; //显示当前湿度
shuma(0,table[Time[2]/16]);
delayms(2);
shuma(1,table[Time[2]%16]);
delayms(2);
shuma(2,table[16]);
delayms(2);
shuma(3,table[Time[1]/16]);
delayms(2);
shuma(4,table[Time[1]%16]);
delayms(2);
shuma(5,0xff);
delayms(2);
shuma(6,table[nowshidu/10]);
delayms(2);
shuma(7,table[nowshidu%10]);
delayms(2);
}
void shezi ()
{
shuma(0,table[16]); //进入湿度设置界面
delayms(2);
shuma(1,table[16]);
delayms(2);
shuma(2,0xff);
delayms(2);
shuma(3,0xff);
delayms(2);
shuma(4,0xff);
delayms(2);
shuma(5,0xff);
delayms(2);
shuma(6,table[shidu/10]);
delayms(2);
shuma(7,table[shidu%10]);
delayms(2);
}
void offall ()
{
select(6);
P0=0xff;
select(7);
P0=0xff;
}
void INT()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
}
unsigned char count=0;
void serviceINT() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if(count==20)
{
nowshidu++;
count=0;
}
if(nowshidu==99)
{
TR0=~TR0;
}
}
unsigned char stat=0;
void scan ()
{
if(S7==0)
{
delayms(10);
if(S7==0)
{
if(stat==0)
{
stat=1; //自动模式
select(4);
P0=0xfe;
}
else if(stat==1)
{
stat=0; //手动模式
select(4);
P0=0xfd;
}
}
while(S7==0)
{
dis();
}
}
if(S6==0)
{
delayms(10);
if(S6==0)
{
if(stat==0)
{
if(nowshidu>shidu)
{
select(5);
P0=0x40;
}
else if(nowshidu<shidu)
{
select(5);
P0=0x00;
}
}
else if(stat==1)
{
offall ();
while(kaiguan==0)
{
shezi ();
if(S6==0)
{
delayms(10);
if(S6==0)
{
kaiguan=1;
}
}
if(S5==0||S4==0)
{
delayms(10);
if(S5==0||S4==0)
{
kaiguan==2;
}
}
}
kaiguan=0;
}
}
}
if(S5==0)
{
delayms(10);
if(S5==0)
{
if(stat==0)
{
select(5);
P0=0x10;
TR0=1;
}
else if(stat==1)
{
shidu=shidu++;
}
}
}
if(S4==0)
{
delayms(10);
if(S4==0)
{
if(stat==0)
{
select(5);
P0=0x00;
TR0=0;
}
else if(stat==1)
{
shidu=shidu--;
}
}
}
}
void main()
{
guanbi();
INT();
DS1302_write();
while(1)
{
DS1302_read();
read_rd2();
dis ();
scan ();
}
}
|