嵌入式开发实例


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

#include"REG932.H"

#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80

#define TIME0 40
#define TIME1 30



sbit LMPWR = P0^0;
sbit LAPWR = P1^7;
sbit LFIRE = P2^3;
sbit LFAULT = P1^6;
sbit LFALL = P3^1;
sbit LLIFT = P3^0;

sbit RM = P2^5;
sbit RB = P2^4;

sbit RFAULT = P0^6;
sbit RFIRE = P0^7;
sbit RFALL = P0^1;
sbit RLIFT = P0^3;
sbit    BUZZ = P1^0;
sbit U4051 = P1^4;

sbit BtnLine1  = P1^1;
sbit BtnLine2  = P1^2;
sbit BtnLine3  = P1^3;

sbit MPWRCHECK = P0^5;

sbit RECHAR = P2^6;
sbit EMERGE = P2^7;

unsigned char FIREALARM;
unsigned char DIRECTION;
unsigned char TROUBLE;


unsigned int FSTEP;
unsigned int LSTEP;
unsigned int POSITION[6];
unsigned char MpowerT1;
unsigned char MpowerT2;
unsigned char AllFallT;
unsigned char HalfFallT;
unsigned int LNSTEP;
unsigned char FRQ;
unsigned char FRQ1;
unsigned char FRQ2;
unsigned char FRQ3;

int STYPE;                                                    
bit TONE;
bit NeedSound;
unsigned int MiddleWaitTime ;
unsigned int WaitTime;
bit COMPELCHARGE;
unsigned long COMPELCHARGETIME;
unsigned int TimeAdd;
unsigned int TimeAdd1;
unsigned int LightFlash;

/* 按键类型 */
enum ButtonStatus { BtnUp,
BtnDown,
BtnStop,
BtnReset,
BtnDetect,
BtnMute,
BtnNone};

enum ButtonStatus GetButtonStatus (void);

void DelayTime( unsigned int t );
void SystemInit(void);
void Reset(void);
void Keyboard(void);
void SelectMode(void);
void InOutput(void);
void Running(void);
void E2PROMSave( unsigned char Addr, unsigned int i);
void E2PROMRead( unsigned int i[4]);
bit  GetU4051Value(unsigned char c);
void Down(void);
void SelectSound(void);
void Up(void);
void CalcStep(void);
void Learn(void);
void LearnSave(void);
bit AfterLearnSound1;
bit AfterLearnSound2;

void timer0 (void) interrupt 1 using 1
{
if (!(TROUBLE & BIT5))
{
switch (STYPE)
{
case 0:break ;

case 1:
{
TH0 = 0xfe;
TL0 = FRQ;
BUZZ = !BUZZ;
};break;
case 2:
{
TH0 = 0xfe;
TL0 = 0xff;
if( TONE )
BUZZ = !BUZZ;
};break;
     case 3:
{
TH0 = 0xfe;
TL0 = FRQ;
if( TONE )
BUZZ = !BUZZ;
};break;
     case 4:
{
TH0 = 0xfe;
TL0 = FRQ;
if( !TONE )
BUZZ = !BUZZ;
};break;
case 5:
{
   if ( TONE )
if ((TROUBLE & 0x03)||(NeedSound))
{
TH0 = 0xfe;
TL0 = 0xff;
if( TONE )
BUZZ = !BUZZ;
}
};break;
case 6:
{
TH0 = 0xfe;
TL0 = 0xff;
if( TONE )
BUZZ = !BUZZ;
};break;
case 7:
{
TH0 = 0xfe;
TL0 = 0xff;
BUZZ = !BUZZ;
};break;
case 8:
{
TH0 = 0xfe;
TL0 = 0xff;
BUZZ = !BUZZ;
};break;
}
}
}

void timer1 (void) interrupt 3 using 1
{
unsigned long t;

TH1 = -(9600/256);
TL1 = -(9600%256);

if (!(TROUBLE & BIT5))
{
switch (STYPE)
{
case 0:break;

case 1:
if( TONE )
{
FRQ++;
if( FRQ== 0xff )
TONE = 0;
}
else
{
FRQ--;
if( FRQ== 0 )
TONE = 1;
};break;
case 2:
if( TONE )                      
{
FRQ++;
if( FRQ== 0x32 )
TONE = 0;
}
else
{
FRQ++;
if( FRQ== 0xc8 )
{
TONE = 1;
FRQ = 0;
}
};break;
        case 3:
if( TONE )                        
{
FRQ++;
if( FRQ== 0x3e )
TONE = 0;
}
else
{
FRQ--;
if( FRQ== 0 )
TONE = 1;
};break;
case 4:
if( TONE )                      
{
FRQ++;
if( FRQ== 0x3e )
TONE = 0;
}
else
{
FRQ--;
if( FRQ== 0 )
TONE = 1;
};break;
case 5:
if ((NeedSound)||(TROUBLE & 0x03))
if( TONE )                    
{
FRQ++;
if( FRQ== 0x32 )
{  
TONE = 0;
FRQ=0;
FRQ3=0;
                                    }
}
else
{

if( FRQ < 0xC8 )
{
FRQ++;
FRQ3=0;
}
else
{  
    FRQ3++;
    if (FRQ3==0xC8 )
{
TONE = 1;
FRQ = 0;
FRQ3 = 0;
}


}

};break;
case 6:
if( TONE )                      
{
FRQ++;
if( FRQ== 0x19 )
TONE = 0;
}
else
{
FRQ++;
if( FRQ== 0x64 )
{
TONE = 1;
FRQ = 0;
}
};break;
case 7:
{
if (FRQ1 != 0xff)
{
FRQ1++;
AfterLearnSound1 = 1;
}
else
{
STYPE = 2;
}
};break;
case 8:
{
if (FRQ2 != 0xff)
{
FRQ2++;
AfterLearnSound2 = 1;
}
else
STYPE = 2;
};break;
}
}

if (!(TROUBLE & BIT0))
if (FIREALARM & BIT7)
Learn(); /* 学习 */
else
{
if (DIRECTION & BIT0) /* 下降 */
{
RLIFT = 0;
if (WaitTime < TIME1)
WaitTime++;

if (WaitTime == TIME1)
if (RLIFT == 0)
Down();
}
else
{
RFALL = 0;
EMERGE = 0;
}
if (DIRECTION & BIT1) /* 上升 */
{
RFALL = 0;
if (WaitTime < TIME1)
WaitTime++;

if (WaitTime == TIME1)
if (RFALL == 0)
Up();
}
   else
RLIFT = 0;
}

if (COMPELCHARGE == 1)
COMPELCHARGETIME++;

}

void main()
{
SystemInit(); /* 系统初始化 */
Reset(); /* 复位方法*/

while(1)
{
Keyboard();
InOutput();
Running();

}
}

void SelectMode(void)
{
if ( GetU4051Value(0) ) /* 学习 */
{
FIREALARM |=BIT7;
POSITION[0] = 0;
POSITION[1] = 0;
POSITION[2] = 0;
POSITION[3] = 0;
POSITION[4] = 0;
POSITION[5] = 0;
}
else /* 运行 */
{
E2PROMRead( POSITION );
if( POSITION[0] == 0 )
{
POSITION[0] = 0x03e8;
POSITION[1] = 0x03e8;
POSITION[2] = 0x03e8;
POSITION[3] = 0x03e8;
POSITION[4] = 0x03e8;
POSITION[5] = 0x03e8;
}
}

FIREALARM |= BIT3;


NeedSound = GetU4051Value(2);

}

void Keyboard(void)
{

if( GetButtonStatus()>=BtnUp&&GetButtonStatus()<=BtnDetect ) /* 如果已经有按键按下时取消静音状态 */
TROUBLE &= ~BIT5;

if (( GetButtonStatus()==BtnUp )&&!(TROUBLE & 0x03)) /* 上升键 */
DIRECTION |= BIT5;

if( GetButtonStatus()==BtnStop ) /* 停止键 */
{
if (FIREALARM & BIT0)
{
if( !( TROUBLE & 0x03 ))
{
if ((0<FSTEP < POSITION[0])&&(DIRECTION & BIT0))
{
DIRECTION |= BIT4;
MiddleWaitTime = 0;
}

if ((FSTEP > POSITION[0]))
DIRECTION |= BIT5;         
}
else
{
DIRECTION |= BIT4;
TimeAdd1 = 0;
}

}
    else
DIRECTION |= BIT6;
    }

if ( GetButtonStatus()==BtnDown ) /* 下降键 */
{
DIRECTION |= BIT7;
WaitTime = 0;
}

if( GetButtonStatus()==BtnMute ) /* 静音键 */
TROUBLE |= BIT5;

if( GetButtonStatus()==BtnReset ) /* 复位键 */
{
Reset();
COMPELCHARGE = 0;
}

if( GetButtonStatus()==BtnDetect ) /* 自检 */
{
LMPWR = 0;
LAPWR = 0;
LFIRE = 1;
LFAULT = 0;
LFALL = 0;
LLIFT = 0;
           STYPE = 1; /* 响火警声 */

DelayTime(1000); /* 延时 */

LMPWR = 1;
LAPWR = 1;
LFIRE = 0;
LFAULT = 1;
LFALL = 1;

STYPE = 0; /* 消音 */
LLIFT = 1;
}

}

void InOutput(void)
{
unsigned char KeyR;

if ( !GetU4051Value(4) || ((LSTEP >= POSITION[3]+POSITION[4]+300)&&(LSTEP!=0)) ) /* 到达上限位 ,防翻卷 */

if ( !GetU4051Value(4)) /* 到达上限位 */
{
if ( FIREALARM & BIT0 )
{
    if (GetU4051Value(5))
{
RLIFT = 0;
LLIFT = 1;
DIRECTION &= ~BIT1;
DelayTime(350);
DIRECTION |= BIT0; /* 开始下降 */
MiddleWaitTime = 0;
}
}
else
{
if( !( DIRECTION & BIT0 ) )
{
DIRECTION &= ~BIT1; /* 停止在上限位 */
DIRECTION |= BIT2;
}
}

if (!(DIRECTION & 0x03))
FSTEP = 0; /* 下降步数清零 */

}
else
DIRECTION &= ~BIT2; /* 未到上限位,清除上限位标志 */



if ( !GetU4051Value(5)) /* 到达下限位 */
{
RB = 1;
EMERGE = 0; /* 停止速放 */
}
   else
RB = 0;


if ( !GetU4051Value(5) ||  ((FSTEP >= (POSITION[0]+POSITION[2]+300))&&(FSTEP!=0))&&(!(TROUBLE & 0x03)) )

if ( !GetU4051Value(5)) /* 到达下限位 */
{
if( !( DIRECTION & BIT1 ) ) /* 非上升状态 */
{
DIRECTION |= BIT3; /* 设置到达下限位标志 */
DIRECTION &= ~BIT0; /* 清除下降标志 */
}

if (!(DIRECTION & 0x03)) /* 主电无故障 */
LSTEP = 0;

RM = 0;


          
EMERGE =0;     


}
else /* 未到达下限位 */
{
DIRECTION &= ~BIT3; /* 清除下限位标志 */

}
if ((FSTEP == POSITION[0])&&(FSTEP!= 0)||(LSTEP == POSITION[3])&&(LSTEP!= 0)) /* 到达中位 */
RM = 1;
else if ((FSTEP == POSITION[5])&&(TROUBLE&0x03)) /* 速降到中位 */
RM = 1;
else
RM = 0;


KeyR = 0;
DelayTime( TIME0 );
if ( !MPWRCHECK ) /* 主电丢失 */
{
DelayTime( TIME0 );
if( !MPWRCHECK )
KeyR = 1;
}

if( KeyR ) /* 主电丢失 */
MpowerT1++;
else
MpowerT1 = 0;
if( MpowerT1 > 5 )
{
if ((!(TROUBLE & BIT1))&&(TROUBLE & BIT5))
TROUBLE &= ~BIT5;

TROUBLE |= BIT1;
MpowerT1 = TIME1;
}
else
TROUBLE &= ~BIT1;


if( !GetU4051Value(3) ) /* 主电相序错 */
MpowerT2++;
else
MpowerT2 = 0;

if( (MpowerT2 > 5 ))
{
if ((!(TROUBLE & BIT0))&&(TROUBLE & BIT5))
TROUBLE &= ~BIT5;

TROUBLE |= BIT0;
MpowerT2 = TIME1;
}
else
TROUBLE &= ~BIT0; /* 清除主电故障标志 */

if( TROUBLE & 0x03 )
{
FIREALARM &= ~BIT4;

if (COMPELCHARGE == 0)
RECHAR = 1;
}
else
{
LMPWR = 0;   /* 主电灯亮 */

if( !( FIREALARM & BIT4 ) )
{
if( ( CMP1 & BIT1 ) || !( CMP2 & BIT1 ) )
{
if (COMPELCHARGE == 0)
RECHAR = 1;  
}
if( ! ( CMP2 & BIT1 ) )
{
COMPELCHARGE = 1;
COMPELCHARGETIME  = 0;
}
}
}


if( CMP1 & BIT1 ) /* 备电未丢失 */
{
TROUBLE &= ~BIT2;
LAPWR = 0;  
}
else
{
if ((!(TROUBLE & BIT2))&&(TROUBLE & BIT5))
TROUBLE &= ~BIT5;

TROUBLE |= BIT2;
}




if( TROUBLE & 0x1f ) /* 电源故障 */
{
LFAULT = 0;
RFAULT = 1;
}
else
{
LFAULT = 1;
RFAULT = 0;
}

if( !( FIREALARM&BIT1 ) ) /* 如果没有全降信号 */
{
if( !GetU4051Value(7) )
AllFallT++;
else
AllFallT = 0;

if( AllFallT > 5 ) /* 如果检测到全降信号 */
{
FIREALARM &= ~BIT2;
FIREALARM |= 0x03;
DIRECTION |= BIT0;
LFIRE = 1;
TROUBLE &= ~BIT5;
}
}

if (!(FIREALARM & BIT1)) /* 如果没有接收到全降信号 */
if( !( FIREALARM&BIT2 ) ) /* 如果没有半降信号 */
{
if( !GetU4051Value(6) )
HalfFallT++;
else
HalfFallT = 0;

if( HalfFallT > 5 )
{
FIREALARM |= BIT0;
FIREALARM &= ~BIT1;
FIREALARM |= BIT2;
DIRECTION |= BIT0;
LFIRE = 1;
TROUBLE &= ~BIT5;
}
}

}

void Running(void)
{
SelectSound(); /* 设置声音状态 */

LearnSave(); /* 如果是学习状态,保存学习信息 */

if (TROUBLE & 0x03) /* 主电故障,主电灯闪烁 */
{
LightFlash++;

if (LightFlash < 5)
LMPWR = 0;
else
LMPWR = 1;

if (LightFlash == 10)
LightFlash = 0;

LFAULT = 0;
     
}


if (TROUBLE & BIT2) /* 备电故障,备电灯闪烁  */
{
LightFlash++;
if (LightFlash < 5)
LAPWR = 0;
else
LAPWR = 1;

if (LightFlash == 10)
LightFlash = 0;

LFAULT = 0;

}

if (!(FIREALARM & BIT7)  ) /* 非学习状态时,实时计算步数 */
CalcStep();

if (!(DIRECTION & 0x03)) /* 停止状态,上、下行灯灭 */
{
LFALL = 1;
LLIFT = 1;
}

if (DIRECTION & BIT2) /* 到达上限位 */
{
RLIFT = 0;
LLIFT = 1;
}

if (DIRECTION & BIT3) /* 到达下限位 */
{
RFALL = 0;
LFALL = 1;
}

if (FIREALARM & BIT0) /* 如果火警 */
LFIRE = 1;
else
LFIRE = 0;

if (DIRECTION & BIT5) /* 已经按下上升键 */
{
WaitTime = 0;
if (!(DIRECTION & BIT2))
if (FIREALARM & BIT0)
{
DIRECTION &= ~BIT0;
RFALL = 0;
DelayTime(50);
DIRECTION |= BIT1;
}
else
if (!(DIRECTION & 0x03)) /* 没有火警,已经停止 */
{
DIRECTION &= ~BIT0;
RFALL = 0;
DelayTime(50);
DIRECTION |= BIT1;
}

DIRECTION &= ~BIT5; /* 响应后就清除上升键标志位 */
}

if( DIRECTION & BIT6 ) /* 按下停止键 */
if (!((FIREALARM & BIT0)&&(DIRECTION & BIT0)))
{
DIRECTION &= ~0x03;
DIRECTION &= ~BIT6;
}

if( CMP1 & BIT1 )
{
if ((COMPELCHARGE == 1) && (COMPELCHARGETIME < 1800000))
RECHAR = 0;
else
RECHAR = 1;
}

if( DIRECTION & BIT7 ) /* 已经按下下降键 */
{
WaitTime = 0;

if (!(DIRECTION & BIT3)) /* 没有到达下限位 */
if (FIREALARM & BIT0)
{
DIRECTION &= ~BIT1;
DelayTime(30);
DIRECTION |= BIT0;
}
else
if (!(DIRECTION & 0x03)) /* 没有火警,已经停止 */
{
DIRECTION &= ~BIT1;
DelayTime(30);
DIRECTION |= BIT0;
}
DIRECTION &= ~BIT7; /* 清除下降键标志位 */
}

DIRECTION &= 0x1f; /* 清除停止、下降和上升键标志位 */

}

void Reset(void)
{
EA = 0;  

FIREALARM = 0;
DIRECTION = 0;
TROUBLE = 0;



MpowerT1 = 0;
MpowerT2 = 0;
AllFallT = 0;
HalfFallT = 0;
MiddleWaitTime = 0;
EMERGE = 0;
RECHAR = 1;
RFAULT = 0;

RFALL = 0;
RLIFT = 0;

LightFlash = 0;
TimeAdd = 0;

FRQ = 0;
TONE = 1;    
    
RFIRE=1;                                
DelayTime( TIME1 );
RFIRE=0;  

SelectMode();

RB = 0;
RM = 0;
AfterLearnSound1 = 0;
AfterLearnSound2 = 0;
if  ( FIREALARM & BIT7 ) /* 学习过程中,按复位键,学习信息清零 */
{
POSITION[0] = 0;
POSITION[1] = 0;
POSITION[2] = 0;
POSITION[3] = 0;
POSITION[4] = 0;
POSITION[5] = 0;
}

EA = 1;  

}

void SystemInit(void)
{
DIVM = 2;

P0M1 |= 0x34;
P0M2 |= 0xcb;

P0M1 &= 0x34;
P0M2 &= 0xcb;

P1M1 |= 0x5e;
P1M2 |= 0xc1;

P1M1 &= 0x5e;
P1M2 &= 0xc1;

P2M1 = 0x0;
P2M2 = 0xff;

P3M1 |= 0x03;
P3M2 |= 0x03;

PT0AD = 0x14;


CMP1 = 0x2a;
CMP2 = 0x2a;

RECHAR = 1;

LMPWR = 1;
LAPWR = 1;
LFIRE = 0;
LFAULT = 1;
LFALL = 1;
LLIFT = 1;

FSTEP = 0;
LSTEP = 0;

U4051   = 1;
STYPE = 0;
RM = 0;
RB = 0;

TMOD = 0x11;
ET0 = 1;
ET1 = 1;

TH1 = -(9600/256);
TL1 = -(9600%256);
TR0 = 1;
TR1 = 1;

COMPELCHARGE = 1;
COMPELCHARGETIME = 0;

}

void Learn(void)
{

if (TROUBLE & 0x03 ) /* 没有主电,备电学习  */
{
if (DIRECTION & BIT0)
{
if (MiddleWaitTime<=300) /* 下降3秒 */
{
MiddleWaitTime++;
EMERGE = 1;
LFALL = 0;
LNSTEP++;
}
else if (MiddleWaitTime<500) /* 下降3秒后,暂停2秒 */
{
EMERGE = 0;
LFALL = 1;
MiddleWaitTime++;
LNSTEP++;
}
else if (MiddleWaitTime=500) /* 暂停后继续下降 */
MiddleWaitTime=0;

POSITION[5] = LNSTEP; /* 记录下降的步数 */

STYPE = 2;

}
else
{
EMERGE = 0;
LFALL = 1;
}
}
else
{
if (!(TROUBLE& 0x03 ) && (DIRECTION & BIT0)) /* 下降学习 */
{
RFALL = 1;
LFALL = 0;
LNSTEP++;
if (POSITION[1]==0) /* 如果 POSITION[1]==0 则表示在中位以上下降 */
POSITION[0] = LNSTEP; /* 向 POSITION[0] 中记录中位以上下降的步数 */
else
POSITION[2] = LNSTEP-POSITION[0]-POSITION[1];/* 如果越过中位,向 POSITION[2] 中记录中位以下的下降步数 */

}

if  (!(TROUBLE & 0x03 ) && (DIRECTION & BIT1)) /* 上升学习 */
{
RLIFT = 1;
LLIFT = 0;
LNSTEP++;
if (MiddleWaitTime == 0) /* 如果 MiddleWaitTime == 0 则表示在中位以下上升 */
POSITION[3] = LNSTEP; /* 向 POSITION[3] 中记录中位以下上升的步数 */
else
POSITION[4] = LNSTEP; /* 向 POSITION[4] 中记录中位以上上升的步数 */

}

if (!((DIRECTION & BIT3)||(DIRECTION & BIT2)))
{
if  (!(DIRECTION & 0x03)) /* 当停止时 */
{
RLIFT = 0;
RFALL = 0;
LFALL = 1;
LLIFT = 1;
if (POSITION[0] > 0) /* 已经开始下走了 */
LNSTEP++;

if ((POSITION[2] == 0)&&(POSITION[0] > 0)) /*  下行,停止在中位,记录暂停时间 */
POSITION[1] = LNSTEP-POSITION[0];

if (POSITION[3] > 0) /* POSITION[3] > 0 表示已经上行,停止在中位  */
{
MiddleWaitTime++;
LNSTEP = 0;
}
}
}
else
{
if ((DIRECTION & BIT3) && (POSITION[2] > 0)&& !RLIFT ) /* 到达下限位,下降过程学习完毕,并且没有上升 */
LNSTEP = 0;

if ((DIRECTION & BIT2) && (POSITION[4] > 0) && !RFALL) /* 到达上限位,上升过程学习完毕,并且没有下降 */
LNSTEP = 0;
}

}

}

void Down(void)
{

RLIFT = 0;
LLIFT = 1;
DIRECTION &=~BIT2;


if( !( TROUBLE & 0x03 )) /* 主电正常 */
{


if ((FIREALARM & BIT2)&&(FSTEP == POSITION[0])) /* 响应火警半降信号,到达中位 */
{
DIRECTION &= ~0x03; /* 停止在中位 */
RFALL = 0;
LFALL = 1;
MiddleWaitTime = 0;
RM = 1;

}


else if ((FIREALARM & BIT0)&&(DIRECTION & BIT0)&&(DIRECTION & BIT6)&&(FSTEP > POSITION[0]))
{
WaitTime = 0;
RFALL = 0;
LFALL = 1;
DIRECTION &= ~BIT0;
DIRECTION |= BIT1;
DIRECTION &= ~BIT6; /* 清除下降键标志,火警时按上升也是给BIT6赋值1 */
MiddleWaitTime = 0;

}


else if ((FIREALARM & BIT0)&&(DIRECTION & BIT0)&&(DIRECTION & BIT4)&&(MiddleWaitTime != POSITION[1])&&(FSTEP < POSITION[0]))
{
RFALL = 0;
LFALL = 1;
MiddleWaitTime++;
if (MiddleWaitTime == POSITION[1])
DIRECTION &=~ BIT4;
}
else
{
RFALL = 1;
LFALL = 0;
MiddleWaitTime = 0;
FSTEP++;
}


if (FSTEP > (POSITION[0]+POSITION[2]+300))
DIRECTION &= ~0x03;

}
else /* 主电故障,备电速降 */
{
if (DIRECTION & BIT2) /* 如果下行,还未离开上限位 则一直下行 */
{
EMERGE = 1;
LFALL = 0;
return;
}

if (DIRECTION & BIT4)             
{
if (TimeAdd1 < POSITION[1])
     {
TimeAdd1++;
EMERGE = 0;
return;
}
else
{
    DIRECTION &= ~BIT4;
TimeAdd1 = 0;
    }
}


if (TimeAdd == 0) /* 离开上限位,开始计时 */
{
RM = 0;
if (MiddleWaitTime<=200) /* 下降3秒 */
{
MiddleWaitTime++;
EMERGE = 1;
LFALL = 0;
FSTEP++;
}
else if (MiddleWaitTime<500) /* 下降3秒后暂停2秒 */
{
EMERGE = 0;
LFALL = 1;
MiddleWaitTime++;
FSTEP++;
}
else if (MiddleWaitTime=500) /* 暂停后继续下降 */
MiddleWaitTime=0;

}


if ((FSTEP == POSITION[5]) && !(FIREALARM & BIT0)  )
{
TimeAdd++;
if (TimeAdd != POSITION[1])
{
EMERGE = 0;
LFALL = 1;
}
else
TimeAdd = 0;

RM = 1;
}


if ((FSTEP >= POSITION[5]) && (FIREALARM & BIT0) &&(FIREALARM & BIT2) )
{
DIRECTION &= ~0x03; /* 清除下降标志位 */
EMERGE = 0; /* 停止速放 */
LFALL = 1;
RM = 1;
}


if ((FSTEP == POSITION[5]) && (FIREALARM & BIT0) &&(FIREALARM & BIT1)  )
{
TimeAdd++;
if (TimeAdd != POSITION[1])
{

EMERGE = 0;
LFALL = 1;

}
else
TimeAdd = 0;

RM = 1;
}



if (FSTEP > 6000) /* 如果1分中还未到下限位,则停止速放 */
{
DIRECTION &= ~0x03;
EMERGE = 0;
LFALL = 1;

FSTEP=0;                            
}

}


}

void Up (void)
{
if ( TROUBLE & 0x03 ) /* 如果主电故障则退出 */
return;

RFALL = 0;
LFALL = 1;

DIRECTION &= ~BIT3;



if (!(FIREALARM & BIT0)) /* 没有火警 */
{
RLIFT = 1;
LLIFT = 0;
LSTEP++;
RM = 0;
MiddleWaitTime = 0;
}
else
{
if (LSTEP != POSITION[3] ) /* 不在中位 */
{
RLIFT = 1;
LLIFT = 0;
MiddleWaitTime = 0;
LSTEP++;
RM = 0;
}

if (LSTEP == POSITION[3] ) /* 到达中位,等待 POSITION[3] 时间 */
{
RLIFT = 0;
LLIFT = 1;
MiddleWaitTime++;
if (MiddleWaitTime == POSITION[1])
{
RLIFT = 0;

DIRECTION &= ~BIT1;
DIRECTION |= BIT0;
WaitTime = 0;
}
   FSTEP = POSITION[0];
}
}

    
if (LSTEP >= (POSITION[3]+POSITION[4]+300)) /* 防止翻卷 */
{
DIRECTION &= ~0x03;

if (FIREALARM & BIT0) /* 正当火警,到达上限位后立即下行 */
{
RLIFT = 0;
DIRECTION &= ~BIT1;
DIRECTION |= BIT0;
WaitTime = 0;
MiddleWaitTime = 0;
}
}

}

void LearnSave(void)
{
unsigned int i;
if ( FIREALARM & BIT7 )
{
if (!(TROUBLE & 0x03)) /* 主电正常,学习 */
{
if (POSITION[0]*POSITION[1]*POSITION[2]*POSITION[3]*POSITION[4]>0)
{
for(i=0;i<=4;i++)
if (POSITION[i]>0)
E2PROMSave( 2*i, POSITION[i] );

   }
}
else if (POSITION[5]>0) /* 去掉主电,备电正常,学习 */
E2PROMSave(10, POSITION[5] );
}

}



void SelectSound(void)
{

if (TROUBLE & BIT5) /* 消音 */
{
STYPE = 0;
return;
}
else if  (( FIREALARM & BIT7 )&&!(DIRECTION & 0x03)) /* 学习 */
{
if (((POSITION[4]>0)&&!(TROUBLE & 0x03)))
{
if (AfterLearnSound1 == 0)
{
STYPE = 7;
FRQ1 = 0;
return;
}
if (!(STYPE == 7))
{
STYPE = 2;
return;
}
}
else if (((POSITION[5]>0)&&(TROUBLE & 0x03)))
{
if (AfterLearnSound2 == 0)
{
STYPE = 8;
FRQ2 = 0;
return;
}
if (!(STYPE == 8))
{
STYPE = 2;
return;
}
}
else
{
STYPE = 2;
return;
}
}
else if ( FIREALARM & BIT7 ) /* 学习 */
{
if (STYPE != 7)
{
STYPE = 2;
return;
}
}
else if (FIREALARM & BIT0 ) /* 火警 */
{
STYPE = 1;
return;
}

else if ( DIRECTION & BIT0 )
{
STYPE = 6; /* 下降 */
return;
}
else if ( DIRECTION & BIT1 )
{
STYPE = 6; /* 上升 */
return;
}
else if (( TROUBLE & BIT2 ) && (NeedSound == 1))
{
STYPE = 5; /* 备电故障 */
return;
}
else if ( TROUBLE & 0x03 )
{
STYPE = 5; /* 主电故障 */
return;
}
else if ( !( FIREALARM & BIT0 ) && !( FIREALARM & BIT7 )&&!( DIRECTION & 0x03 ) && !( TROUBLE & 0x17 ))  /* 没有火警、学习、故障和升降 */
{
STYPE = 0;
return;
}
else
STYPE = 0;

}


void CalcStep(void)
{
unsigned long t;

if (DIRECTION & BIT0) /* 下降 */
{
if (!( TROUBLE & 0x03 )) /* 主电正常 */
{
if (FSTEP < POSITION[0])
{
t = (long) POSITION[4]*FSTEP;
t = (long) t / POSITION[0];
LSTEP = POSITION[4]+POSITION[3]-t;
}

if (FSTEP == POSITION[0]) /* 下降到中位 */
LSTEP = POSITION[3];

if ((FSTEP > POSITION[0])&&(FSTEP < (POSITION[0]+POSITION[2]) )) /* 下降到中位以下 */
{
t = (long) (POSITION[0]+POSITION[2]-FSTEP)*POSITION[3];
LSTEP = t /POSITION[2];
}

if ((FSTEP >= (POSITION[0]+POSITION[2]))&&(FSTEP!=0)) /* 如果翻卷 */
LSTEP = 0;
}
else
if (FSTEP >= POSITION[5])
LSTEP = 0;

}


if (DIRECTION & BIT1) /* 上升 */
{

if (LSTEP < POSITION[3]) /* 在中位以下 */
{
t = (long) POSITION[2]*LSTEP ;
t = (long) t/POSITION[3] ;
FSTEP = (POSITION[0]+POSITION[2])-t;

}

if (LSTEP == POSITION[3]) /* 升到中位 */
FSTEP = POSITION[0];

if ((LSTEP > POSITION[3])&&(LSTEP < (POSITION[3]+POSITION[4]))) /* 中位以上 */
{
t = (long) (POSITION[3]+POSITION[4]-LSTEP)*POSITION[0];
t = (long) t/POSITION[4];
FSTEP = t ;
}

if (LSTEP >= (POSITION[3]+POSITION[4]))
FSTEP = 0;
}



}

enum ButtonStatus GetButtonStatus (void)
{

if (BtnLine1&&BtnLine2&&BtnLine3) /* 111 没有按键 */
return BtnNone;

if (BtnLine1&&!BtnLine2&&BtnLine3) /* 101 停止键 */
{

if (BtnLine1&&!BtnLine2&&BtnLine3)
return BtnStop;
}

if (!BtnLine1&&BtnLine2&&BtnLine3) /* 011 上升键 */
{
DelayTime(20);
if ((!BtnLine1&&BtnLine2&&BtnLine3)&&!(DIRECTION & BIT2))
return BtnUp;
}

if (BtnLine1&&BtnLine2&&!BtnLine3) /* 110 下降键 */
{
DelayTime(20);

if ((BtnLine1&&BtnLine2&&!BtnLine3)&&(FIREALARM & BIT0))
return BtnReset;

if ((BtnLine1&&BtnLine2&&!BtnLine3)&&!(DIRECTION & BIT3))
return BtnDown;
}


DelayTime(20); /* 消抖 */





if (BtnLine1&&!BtnLine2&&!BtnLine3) /* 100 复位键 */
{  
DelayTime(20);
if (BtnLine1&&!BtnLine2&&!BtnLine3)
return BtnReset;
}

if (!BtnLine1&&!BtnLine2&&BtnLine3) /* 001 自检键 */
{
DelayTime(20);
if (!BtnLine1&&!BtnLine2&&BtnLine3)
return BtnDetect;
}

if (!BtnLine1&&BtnLine2&&!BtnLine3) /* 010 消音键 */
{
DelayTime(20);
if (!BtnLine1&&BtnLine2&&!BtnLine3)
return BtnMute;
}

TROUBLE &= ~BIT5; /* 有键按下 就不消音 */
}

bit  GetU4051Value(unsigned char c)
{
P2 &= 0xf8;
P2 |= c;
DelayTime( TIME0 );

if ( !U4051 ) /*  低电平 */
{
DelayTime(TIME0);
if( !U4051 )
return U4051;
}

if ( U4051 ) /*  高电平 */
{
DelayTime(TIME0);
if( U4051 )
return U4051;
}

}


void E2PROMSave(unsigned char Addr, unsigned int i)
{
unsigned char Data;

Data = i;
DEECON=0x00; /* 初始化DEECON */
DEEDAT=Data; /* 赋值DEEDAT */
DEEADR=Addr; /* 赋值DEEADR */
while((DEECON&0x80)==0); /* 等待写完成 */
DEECON=DEECON&0x7F; /* 清0写完成标志位 */
Addr++;
Data = i>>8;
DEEDAT=Data; /* 赋值DEEDAT */
DEEADR=Addr; /* 赋值DEEADR */
while((DEECON&0x80)==0); /* 等待写完成 */
DEECON=DEECON&0x7F; /* 清0写完成标志位 */

}


void DelayTime( unsigned int t )
{
unsigned int  i,j;

for( i = 0; i < t; i++ )
for( j = 0; j < t; j++ )
;
}

void E2PROMRead ( unsigned int i[6] )
{
unsigned char Data[12];
unsigned char t;

DEECON=0x00; /* 初始化DEECON */
for( t = 0; t<12 ;t++)
{
DEEADR = t; /* 赋值DEEADR */
while((DEECON&0x80)==0); /* 等待 */
DEECON=DEECON&0x7F; /* 清0写完成标志位 */
Data[t] = DEEDAT; /* 将读出的值赋给Temp */
}
for( t = 0; t<6 ;t++)
i[t] = Data[2*t+1]*256 + Data[2*t];
}