datasheet

智能家居控制系统C程序

2018-01-12 20:51:05来源: eefocus 关键字:智能家居  控制系统

#include  

bit r_flag; 

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); 


sbit key   = P2^0; 
bit key_flag; 

sbit dog   = P0^7; 
sbit LED   = P0^0; 
sbit TELA   = P3^6; 
sbit TEL  = P3^7; 
sbit RING  = P3^2; 
sbit switching =P0^4; 


unsigned char mode; 
bit passwordflag; 
unsigned char password[6]; 
unsigned char passwordtest[6]; 

void dlms(unsigned int x); 
void reset(void); 
void open(void); 

//****************************************************************  // 
//               INT1中断服务程序                                   // 
//****************************************************************  // 
unsigned char ring_fluctuation; 
unsigned char ring_count; 
bit ring_flag; 
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  
//#pragma  ot(6,SIZE) 
#include  
#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)) 
  { 
  Delay(255); 
  Delay(255); 
  Delay(255); 
  Delay(255); 
  } 
return(errorflag); 



/*****************以下是对IIC总线的操作子程序***/ 
/*****************启动总线**********************/ 
void IICStart(void) 

SCL=0;                  // 
SDA=1; 
SCL=1; 
_nop_(); 
_nop_(); 
_nop_(); 
SDA=0; 
_nop_(); 
_nop_(); 
_nop_(); 
_nop_(); 
SCL=0; 
SDA=1;                  // 


/*****************停止IIC总线****************/ 
void IICStop(void) 

SCL=0; 
SDA=0; 
SCL=1; 
_nop_(); 
_nop_(); 
_nop_(); 
SDA=1; 
_nop_(); 
_nop_(); 
_nop_(); 
SCL=0; 


/**************检查应答位*******************/ 
bit IICRecAck(void) 

SCL=0; 
SDA=1; 
SCL=1; 
_nop_(); 
_nop_(); 
_nop_(); 
_nop_(); 
CY=SDA;                 //因为返回值总是放在CY中的 
SCL=0; 
return(CY); 


/***************对IIC总线产生应答*******************/ 
void IICACK(void) 

SDA=0; 
SCL=1; 
_nop_(); 
_nop_(); 
_nop_(); 
_nop_(); 
SCL=0; 
_nop_(); 
SDA=1; 


/*****************不对IIC总线产生应答***************/ 
void IICNoAck(void) 

SDA=1; 
SCL=1; 
_nop_(); 
_nop_(); 
_nop_(); 
_nop_(); 
SCL=0; 


/*******************向IIC总线写数据*********************/ 
void IICSendByte(unsigned char sendbyte) 

unsigned char data j=8; 
for(;j>0;j--) 
   { 
   SCL=0; 
   sendbyte<<=1;        //无论C51怎样实现这个操作,始终会使CY=sendbyte^7; 
   SDA=CY; 
   SCL=1; 
   } 
SCL=0; 


/**********************从IIC总线上读数据子程序**********/ 
unsigned char IICReceiveByte(void) 

register receivebyte,i=8; 
SCL=0; 
while(i--) 
   { 
   SCL=1; 
   receivebyte=(receivebyte<<1)|SDA; 
   SCL=0; 
   } 
return(receivebyte); 


/***************一个简单延时程序************************/ 
void Delay(unsigned char DelayCount) 

while(DelayCount--); 

关键字:智能家居  控制系统

编辑:什么鱼 引用地址:http://www.eeworld.com.cn/mcu/article_2018011237326.html
本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。

上一篇:智能家居控制系统VB上的代码
下一篇:最后一页

关注eeworld公众号 快捷获取更多信息
关注eeworld公众号
快捷获取更多信息
关注eeworld服务号 享受更多官方福利
关注eeworld服务号
享受更多官方福利
推荐阅读
全部
智能家居
控制系统

小广播

独家专题更多

东芝在线展会——芯科技智社会创未来
东芝在线展会——芯科技智社会创未来
2017东芝PCIM在线展会
2017东芝PCIM在线展会
TI车载信息娱乐系统的音视频解决方案
TI车载信息娱乐系统的音视频解决方案
汇总了TI汽车信息娱乐系统方案、优质音频解决方案、汽车娱乐系统和仪表盘参考设计相关的文档、视频等资源

何立民专栏

单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号 电信业务审批[2006]字第258号函 京公海网安备110108001534 Copyright © 2005-2017 EEWORLD.com.cn, Inc. All rights reserved