相关智能家居程序
时间:10-02
整理:3721RD
点击:
void service_int0() interrupt 0 using 1
{
if(ring_fluctuation<10)
{
ring_fluctuation++;
}
}
//**************************************************************** //
// t1 定时中断 //
//**************************************************************** //
unsigned char ring_dlsm;
unsigned char ring_time;
unsigned int ring_reset_time;
unsigned char dlms_time;
void timer1 (void) interrupt 3 using 0
{
TH1=0x3c;
TL1=0xb0;
dlms_time++;
if(ring_fluctuation>5)
{
EX0=0;//INT1_OFF;
ring_dlsm++;
if(ring_dlsm==20)//等待一秒
{
ring_dlsm=0;
EX0=1;//INT1_ON;
ring_fluctuation=0;
ring_count++;//震铃次数
if(ring_flag)//有效振铃
{
if(ring_count==6)
{
ring_count=7;
mode=1;
}
}
else//无效振铃
{
if(ring_count>2)
{
ring_reset_time=800;
}
}
}
}
if((ring_count==2)|(ring_count==1))
{
ring_time++;
if(ring_time==200)
{
ring_flag=1;
}
}
if((ring_count>0)&(ring_count<6))
{
LED=INT0;
}
else if(passwordflag)
{
LED=~LED;
}
if(ring_count>0)
{
ring_reset_time++;
if(ring_reset_time==1100)//55s
{
reset();
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=switching;
}
}
}
//**************************************************************** //
// INT1中断服务程序 //
//**************************************************************** //
unsigned char DTMF;
void service_int1() interrupt 2 using 1
{
EX1=0;
DTMF=P1;
DTMF=DTMF>>4;
}
void main(void)
{
TMOD=0x11;
TH1=0x3c;
TL1=0xb0;
TR1=1;
ET1=1;
IT0=1;
EX0=1;
IT1=0;
EX1=1;
reset();
EA=0;
r_flag=RW24XX(password,6,0x0000,0xa1,M2402);//R
if(P2_0==0)
dlms(20);
if(P2_0==0)
{
ring_flag=1;
mode=1;
passwordflag=1;
ring_count=7;
ring_fluctuation=6;
}
EA=1;
while(1)
{
open();
dog=~dog;
switch(mode)
{
case 0:;
break;
case 1:
TEL=1;
LED=0;//
dlms_time=0;
ring_reset_time=0;
while(dlms_time<40)
{
dlms(1);
TELA=~TELA;
}
ring_reset_time=0;
LED=1;//
TELA=0;
mode++;
DTMF=255;
EX1=1;
LED=switching;
break;
case 2://password
if(DTMF!=255)
{
passwordtest[0]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 3://password
if(DTMF!=255)
{
passwordtest[1]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 4://password
if(DTMF!=255)
{
passwordtest[2]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 5://password
if(DTMF!=255)
{
passwordtest[3]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 6://password
if(DTMF!=255)
{
passwordtest[4]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 7://password
if(DTMF!=255)
{
passwordtest[5]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time>2);
if(INT1==1)
{
DTMF=255;
mode++;
//EX1=1;
}
}
}
break;
case 8:
if(passwordflag==0)
{
if((passwordtest[0]==password[0])&(passwordtest[1]==password[1])&(passwordtest[2]==password[2])&(passwordtest[3]==password[3])&(passwordtest[4]==password[4])&(passwordtest[5]==password[5]))
{
mode++;
dlms_time=0;
}
else
{
reset();
}
}
else
{
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
TELA=0;
password[0]=passwordtest[0];
password[1]=passwordtest[1];
password[2]=passwordtest[2];
password[3]=passwordtest[3];
password[4]=passwordtest[4];
password[5]=passwordtest[5];
EA=0;
r_flag=RW24XX(password,6,0x0000,0xa0,M2402);//W
EA=1;
reset();
}
break;
case 9:
while(dlms_time<20)
{
dlms(1);
TELA=~TELA;
}
while(dlms_time<25)
{
}
while(dlms_time<45)
{
dlms(1);
TELA=~TELA;
}
TELA=0;
EX1=1;
mode++;
break;
case 10:
switch(DTMF)
{
case 1:
if(ring_reset_time<1050)
{
dlms_time=0;
switching=0;
LED=0;
while(dlms_time<10)
{
}
while(dlms_time<20)
{
dlms(2);
TELA=~TELA;
}
while(dlms_time<30)
{
}
while(dlms_time<40)
{
dlms(2);
TELA=~TELA;
}
TELA=0;
DTMF=255;
EX1=1;
}
break;
case 2:
if(ring_reset_time<1050)
{
dlms_time=0;
switching=1;
LED=1;
while(dlms_time<10)
{
}
while(dlms_time<20)
{
dlms(4);
TELA=~TELA;
}
while(dlms_time<30)
{
}
while(dlms_time<40)
{
dlms(4);
TELA=~TELA;
}
TELA=0;
DTMF=255;
EX1=1;
}
break;
case 12:
reset();
break;
}
break;
//--------------------------------------------------------------------//
default:;
//--------------------------------------------------------------------//
}
}
}
void reset(void)
{
TEL=0;
TELA=0;
ring_fluctuation=0;
ring_count=0;
ring_flag=0;
ring_dlsm=0;
ring_time=0;
dlms_time=0;
ring_reset_time=0;
key_flag=0;
mode=0;
EX1=1;
DTMF=255;
LED=switching;
passwordflag=0;
passwordtest[0]=255;
passwordtest[1]=255;
passwordtest[2]=255;
passwordtest[3]=255;
passwordtest[4]=255;
passwordtest[5]=255;
}
void open(void)
{
if((key==0)&(key_flag==0)&(passwordflag==0))
{
dlms_time=0;
while(dlms_time<2);
if(key==0)
{
reset();
key_flag=1;
switching=~switching;
LED=switching;
dlms_time=0;
}
}
if((key==1)&(dlms_time>10))
{
key_flag=0;
}
}
void dlms(unsigned int x)
{
unsigned int i;
while (x-->0)
{
dog=~dog;
for (i=0;i<163;i++)
{;}
}
}
//24cxx
//----------------------------------------------START----------------------------------------------//
//enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
//extern bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
// unsigned char ControlByte,enum eepromtype EepromType);
// flag=RW24XX(&x,1,0x0000,0xa0,M24256);//W 24C256
// flag=RW24XX(&x,1,0x0000,0xa1,M24256);//R 24C256
#include <AT89X55.H>
//#pragma ot(6,SIZE)
#include <intrins.h>
#define ERRORCOUNT 10
sbit SDA=P2^7;//对应硬件
sbit SCL=P2^6;//对应硬件
enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
enum eepromtype EepromType;
/***********************************************************************************/
//DataBuff为读写数据输入/输出缓冲区的首址
//ByteQuantity 为要读写数据的字节数量
//Address 为EEPROM的片内地址
//ControlByte 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W),其中R/W=1,
//表示读操作,R/W=0为写操作,A2,A1,A0为EEPROM的页选或片选地址;
//EepromType为枚举变量,需为M2401至M24256中的一种,分别对应24C01至24C256;
//函数返回值为一个位变量,若返回1表示此次操作失效,0表示操作成功;
//ERRORCOUNT为允许最大次数,若出现ERRORCOUNT次操作失效后,则函数中止操作,并返回1
/***********************************************************************************/
extern bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte,enum eepromtype EepromType)
{
void Delay(unsigned char DelayCount);
void IICStart(void);
void IICStop(void);
bit IICRecAck(void);
void IICNoAck(void);
void IICAck(void);
unsigned char IICReceiveByte(void);
void IICSendByte(unsigned char sendbyte);
unsigned char data j,i=ERRORCOUNT;
bit errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
if(EepromType>M2416)
{
IICSendByte((unsigned char)(Address>>8));
if(IICRecAck())
continue;
}
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
if(!(ControlByte&0x01))
{
j=ByteQuantity;
errorflag=0; //********clr errorflag
while(j--)
{
IICSendByte(*DataBuff++);
if(!IICRecAck())
continue;
errorflag=1;
break;
}
if(errorflag==1)
continue;
break;
}
else
{
IICStart();
IICSendByte(ControlByte);
if(IICRecAck())
continue;
while(--ByteQuantity)
{
*DataBuff++=IICReceiveByte();
IICAck();
}
*DataBuff=IICReceiveByte(); //read last byte data
IICNoAck();
errorflag=0;
break;
}
}
IICStop();
if(!(ControlByte&0x01))
{
{
if(ring_fluctuation<10)
{
ring_fluctuation++;
}
}
//**************************************************************** //
// t1 定时中断 //
//**************************************************************** //
unsigned char ring_dlsm;
unsigned char ring_time;
unsigned int ring_reset_time;
unsigned char dlms_time;
void timer1 (void) interrupt 3 using 0
{
TH1=0x3c;
TL1=0xb0;
dlms_time++;
if(ring_fluctuation>5)
{
EX0=0;//INT1_OFF;
ring_dlsm++;
if(ring_dlsm==20)//等待一秒
{
ring_dlsm=0;
EX0=1;//INT1_ON;
ring_fluctuation=0;
ring_count++;//震铃次数
if(ring_flag)//有效振铃
{
if(ring_count==6)
{
ring_count=7;
mode=1;
}
}
else//无效振铃
{
if(ring_count>2)
{
ring_reset_time=800;
}
}
}
}
if((ring_count==2)|(ring_count==1))
{
ring_time++;
if(ring_time==200)
{
ring_flag=1;
}
}
if((ring_count>0)&(ring_count<6))
{
LED=INT0;
}
else if(passwordflag)
{
LED=~LED;
}
if(ring_count>0)
{
ring_reset_time++;
if(ring_reset_time==1100)//55s
{
reset();
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=1;
dlms(100);
LED=0;
dlms(100);
LED=switching;
}
}
}
//**************************************************************** //
// INT1中断服务程序 //
//**************************************************************** //
unsigned char DTMF;
void service_int1() interrupt 2 using 1
{
EX1=0;
DTMF=P1;
DTMF=DTMF>>4;
}
void main(void)
{
TMOD=0x11;
TH1=0x3c;
TL1=0xb0;
TR1=1;
ET1=1;
IT0=1;
EX0=1;
IT1=0;
EX1=1;
reset();
EA=0;
r_flag=RW24XX(password,6,0x0000,0xa1,M2402);//R
if(P2_0==0)
dlms(20);
if(P2_0==0)
{
ring_flag=1;
mode=1;
passwordflag=1;
ring_count=7;
ring_fluctuation=6;
}
EA=1;
while(1)
{
open();
dog=~dog;
switch(mode)
{
case 0:;
break;
case 1:
TEL=1;
LED=0;//
dlms_time=0;
ring_reset_time=0;
while(dlms_time<40)
{
dlms(1);
TELA=~TELA;
}
ring_reset_time=0;
LED=1;//
TELA=0;
mode++;
DTMF=255;
EX1=1;
LED=switching;
break;
case 2://password
if(DTMF!=255)
{
passwordtest[0]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 3://password
if(DTMF!=255)
{
passwordtest[1]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 4://password
if(DTMF!=255)
{
passwordtest[2]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 5://password
if(DTMF!=255)
{
passwordtest[3]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 6://password
if(DTMF!=255)
{
passwordtest[4]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time<2);
if(INT1==1)
{
DTMF=255;
mode++;
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
EX1=1;
TELA=0;
}
}
}
break;
case 7://password
if(DTMF!=255)
{
passwordtest[5]=DTMF;
if(INT1==1)
{
dlms_time=0;
while(dlms_time>2);
if(INT1==1)
{
DTMF=255;
mode++;
//EX1=1;
}
}
}
break;
case 8:
if(passwordflag==0)
{
if((passwordtest[0]==password[0])&(passwordtest[1]==password[1])&(passwordtest[2]==password[2])&(passwordtest[3]==password[3])&(passwordtest[4]==password[4])&(passwordtest[5]==password[5]))
{
mode++;
dlms_time=0;
}
else
{
reset();
}
}
else
{
dlms_time=0;
while(dlms_time<10)
{
dlms(2);
TELA=~TELA;
}
TELA=0;
password[0]=passwordtest[0];
password[1]=passwordtest[1];
password[2]=passwordtest[2];
password[3]=passwordtest[3];
password[4]=passwordtest[4];
password[5]=passwordtest[5];
EA=0;
r_flag=RW24XX(password,6,0x0000,0xa0,M2402);//W
EA=1;
reset();
}
break;
case 9:
while(dlms_time<20)
{
dlms(1);
TELA=~TELA;
}
while(dlms_time<25)
{
}
while(dlms_time<45)
{
dlms(1);
TELA=~TELA;
}
TELA=0;
EX1=1;
mode++;
break;
case 10:
switch(DTMF)
{
case 1:
if(ring_reset_time<1050)
{
dlms_time=0;
switching=0;
LED=0;
while(dlms_time<10)
{
}
while(dlms_time<20)
{
dlms(2);
TELA=~TELA;
}
while(dlms_time<30)
{
}
while(dlms_time<40)
{
dlms(2);
TELA=~TELA;
}
TELA=0;
DTMF=255;
EX1=1;
}
break;
case 2:
if(ring_reset_time<1050)
{
dlms_time=0;
switching=1;
LED=1;
while(dlms_time<10)
{
}
while(dlms_time<20)
{
dlms(4);
TELA=~TELA;
}
while(dlms_time<30)
{
}
while(dlms_time<40)
{
dlms(4);
TELA=~TELA;
}
TELA=0;
DTMF=255;
EX1=1;
}
break;
case 12:
reset();
break;
}
break;
//--------------------------------------------------------------------//
default:;
//--------------------------------------------------------------------//
}
}
}
void reset(void)
{
TEL=0;
TELA=0;
ring_fluctuation=0;
ring_count=0;
ring_flag=0;
ring_dlsm=0;
ring_time=0;
dlms_time=0;
ring_reset_time=0;
key_flag=0;
mode=0;
EX1=1;
DTMF=255;
LED=switching;
passwordflag=0;
passwordtest[0]=255;
passwordtest[1]=255;
passwordtest[2]=255;
passwordtest[3]=255;
passwordtest[4]=255;
passwordtest[5]=255;
}
void open(void)
{
if((key==0)&(key_flag==0)&(passwordflag==0))
{
dlms_time=0;
while(dlms_time<2);
if(key==0)
{
reset();
key_flag=1;
switching=~switching;
LED=switching;
dlms_time=0;
}
}
if((key==1)&(dlms_time>10))
{
key_flag=0;
}
}
void dlms(unsigned int x)
{
unsigned int i;
while (x-->0)
{
dog=~dog;
for (i=0;i<163;i++)
{;}
}
}
//24cxx
//----------------------------------------------START----------------------------------------------//
//enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
//extern bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
// unsigned char ControlByte,enum eepromtype EepromType);
// flag=RW24XX(&x,1,0x0000,0xa0,M24256);//W 24C256
// flag=RW24XX(&x,1,0x0000,0xa1,M24256);//R 24C256
#include <AT89X55.H>
//#pragma ot(6,SIZE)
#include <intrins.h>
#define ERRORCOUNT 10
sbit SDA=P2^7;//对应硬件
sbit SCL=P2^6;//对应硬件
enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
enum eepromtype EepromType;
/***********************************************************************************/
//DataBuff为读写数据输入/输出缓冲区的首址
//ByteQuantity 为要读写数据的字节数量
//Address 为EEPROM的片内地址
//ControlByte 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W),其中R/W=1,
//表示读操作,R/W=0为写操作,A2,A1,A0为EEPROM的页选或片选地址;
//EepromType为枚举变量,需为M2401至M24256中的一种,分别对应24C01至24C256;
//函数返回值为一个位变量,若返回1表示此次操作失效,0表示操作成功;
//ERRORCOUNT为允许最大次数,若出现ERRORCOUNT次操作失效后,则函数中止操作,并返回1
/***********************************************************************************/
extern bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte,enum eepromtype EepromType)
{
void Delay(unsigned char DelayCount);
void IICStart(void);
void IICStop(void);
bit IICRecAck(void);
void IICNoAck(void);
void IICAck(void);
unsigned char IICReceiveByte(void);
void IICSendByte(unsigned char sendbyte);
unsigned char data j,i=ERRORCOUNT;
bit errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
if(EepromType>M2416)
{
IICSendByte((unsigned char)(Address>>8));
if(IICRecAck())
continue;
}
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
if(!(ControlByte&0x01))
{
j=ByteQuantity;
errorflag=0; //********clr errorflag
while(j--)
{
IICSendByte(*DataBuff++);
if(!IICRecAck())
continue;
errorflag=1;
break;
}
if(errorflag==1)
continue;
break;
}
else
{
IICStart();
IICSendByte(ControlByte);
if(IICRecAck())
continue;
while(--ByteQuantity)
{
*DataBuff++=IICReceiveByte();
IICAck();
}
*DataBuff=IICReceiveByte(); //read last byte data
IICNoAck();
errorflag=0;
break;
}
}
IICStop();
if(!(ControlByte&0x01))
{