;
SHOW1    EQU    22H
OUTH     EQU    P3.5
OUTZ     EQU    P3.4
OUTL     EQU    P3.3
FFFF     EQU    P1.6
;/按键
FENGM    EQU    P1.7
ONFF     EQU    P1.0
WULL     EQU    P1.2
FULZ     EQU    P3.7
TTIME    EQU    P1.1
BAO      EQU    P3.2;/*磁簧管*/
REM      EQU    P1.5;/*红外接收头*/
;/*----*/
TIMERS   EQU    45H
TIMERM   EQU    46H
TIMERH   EQU    47H

TDATA    EQU    49H
TTMIE    EQU    4AH
WUDATA   EQU    4BH
YTIME    EQU    4CH
DJME     EQU    56H
SHOW2    EQU    57H

LEDE     EQU    (22H).0;
LEDF     EQU    (22H).1;
LEDG     EQU    (22H).2;
LEDA     EQU    (22H).3;
LEDH     EQU    (22H).4;
LEDB     EQU    (22H).5;
LEDC     EQU    (22H).6;
LEDD     EQU    (22H).7;
RECEIVE  EQU    (24H).1     ;接收正确位
ENABLE   EQU    (24H).2     ;
SECOND   EQU    (24H).3
FUL      EQU    (24H).4
OF       EQU    (24H).5
DOWNJ    EQU    (24H).6;待机标志
QSBZ     EQU    (24H).7;缺水标志
;STOPBB   EQU    (25H).0;

       ORG 0000H
       LJMP MAIN
       ORG 000BH
       LJMP ST0
;---------------
MAIN:
       CLR  OUTL
       CLR  OUTZ
       CLR  OUTH         ;清输出
       CLR  FFFF
       MOV  SHOW1,#0FFH
       LCALL  UARTO
       CALL   DELAY
       CLR  EA
       CLR FENGM
       LCALL T50MS
       LCALL T50MS
       SETB FENGM       ;开机响一声
       CLR  A            ;清零片内单元
       MOV  R0,#20H
CLEAR: MOV  @R0,A
       INC  R0
       CJNE R0,#40H,CLEAR
       MOV  SP,#70H
       MOV  TIMERS,#0
       MOV  TIMERM,#0
       MOV  TIMERH,#0
       MOV  TDATA,#0
       MOV  WUDATA,#2
       SETB EA
       SETB ET0
       MOV  TMOD,#01H
       MOV  TH0,#3CH
       MOV  TL0,#0B0H
       CLR  TR0
       MOV  WUDATA,#2
       MOV  55H,#40
       SETB DOWNJ
       CLR  FUL
;       CLR  STOPBB
       MOV  SHOW1,#0FFH
       LCALL  UARTO
       CALL   DELAY

MLOOP:
       LCALL  JK1
       LCALL  JK2
       LCALL  JK3
       LCALL  JK4
       LCALL  REMOTE
       LCALL  WORK
       LCALL  QWATER     ;缺水
       LCALL  FMMF       ;报警,报警5次自动关机
       LJMP   MLOOP
       LJMP   MAIN
;----
JK1:
       JB     ONFF,JK199    ;开关键
RM1:   CLR    DOWNJ
       CLR FENGM
       LCALL T50MS
       LCALL T50MS
       SETB FENGM       
       JB     OF,JK11
       SETB   OF
       SETB   LEDB
       CLR    LEDC
       SETB   LEDD
       LCALL  UARTO        ;点亮中档灯
       CALL   DELAY
       SETB   OUTZ         ;开机默认设置中档
       CLR    OUTL
       CLR    OUTH
       MOV    TIMERS,#0
       MOV    TIMERM,#0
       MOV    TIMERH,#0
       MOV  TDATA,#0
       MOV  WUDATA,#2
       MOV    55H,#40
       MOV    DJME,#0
       SETB   EA
       JNB    ONFF,$
       JMP    JK19
JK199: LJMP   JK19
JK11:
       CLR    OF
       SETB   DOWNJ        ;待机标志
       CLR    FUL
       CLR    OUTL
       CLR    OUTZ
       CLR    OUTH         ;清输出
       CLR    FFFF
       SETB   FENGM
       SETB   LEDA
       SETB   LEDB
       SETB   LEDC
       SETB   LEDE
       SETB   LEDF
       SETB   LEDD
       SETB   LEDG
       LCALL  UARTO        ;熄灭所有指示灯
       CALL   DELAY
       MOV    TIMERS,#0
       MOV    TIMERM,#0
       MOV    TIMERH,#0
       MOV  TDATA,#0
       MOV  WUDATA,#2
       MOV    55H,#40
       CLR    EA
       SETB   ET0
       MOV    TMOD,#01H
       MOV    TH0,#3CH
       MOV    TL0,#0B0H
       CLR    TR0
       JNB    ONFF,$
JK19:
       RET
;---
JK2:
       JB     WULL,JK29
RM2:   JB     DOWNJ,JK29
       CLR FENGM
       LCALL T50MS
       LCALL T50MS
       SETB FENGM       
       INC    WUDATA
       MOV    A,WUDATA
WD01:  CJNE   A,#01,WD02
       CLR    LEDD
       SETB   LEDB
       SETB   LEDC
       LCALL  UARTO
       CALL   DELAY
       SETB   OUTL
       CLR    OUTZ
       CLR    OUTH
WD91:  JNB    WULL,$
       JMP    JK29
WD02:  CJNE   A,#02,WD03
       SETB   LEDB
       CLR    LEDC
       SETB   LEDD
       LCALL  UARTO
       CALL   DELAY
       CLR    OUTL
       SETB   OUTZ
       CLR    OUTH
WD92:  JNB    WULL,$
       JMP    JK29
WD03:  CJNE   A,#03,WD04
       SETB   LEDD
       SETB   LEDC
       CLR    LEDB
       LCALL  UARTO
       CALL   DELAY
       CLR    OUTL
       CLR    OUTZ
       SETB   OUTH
WD93:  JNB    WULL,$
       JMP    JK29
WD04:
       MOV    WUDATA,#01
       MOV    A,WUDATA
       LJMP   WD01
JK29:
       RET
;---
JK3:
       JB      FULZ,JK39
RM3:   JB      DOWNJ,JK39
       CLR FENGM
       LCALL T50MS
       LCALL T50MS
       SETB FENGM     
       JNB     FUL,JK31
       CLR     FUL
       CLR     FFFF
       SETB    LEDA
       LCALL  UARTO
       CALL   DELAY
       JNB     FULZ,$
       JMP     JK39
JK31:
       SETB    FUL
       SETB    FFFF
       CLR     LEDA
       LCALL  UARTO
       CALL   DELAY
       JNB     FULZ,$
JK39:
       RET
;---
JK4:
       JB      TTIME,JK449
RM4:   JB      DOWNJ,JK449
       CLR FENGM
       LCALL T50MS
       LCALL T50MS
       SETB FENGM       ;开机响一声
       INC     TDATA
       MOV     A,TDATA
JK41:  CJNE    A,#01,JK42
       SETB    LEDG
       SETB    LEDF
       CLR     LEDE
       LCALL  UARTO
       CALL   DELAY
       MOV     YTIME,#2         ;2H
       SETB    TR0
       JNB     TTIME,$
JK449: JMP     JK49
JK42:  CJNE    A,#02,JK43
       SETB    LEDE
       CLR     LEDF
       SETB    LEDG
       LCALL  UARTO
       CALL   DELAY
       MOV     YTIME,#4         ;4H
       SETB    TR0
       JNB     TTIME,$
       JMP     JK49
JK43:  CJNE    A,#03,JK44
       SETB    LEDE
       SETB    LEDF
       CLR     LEDG
       LCALL  UARTO
       CALL   DELAY
       MOV     YTIME,#8         ;8H
       SETB    TR0
       JNB     TTIME,$
       JMP     JK49
JK44:  CJNE    A,#04,JK45       ;不定时
       CLR     TR0
       MOV     TIMERS,#0
       MOV     TIMERM,#0
       MOV     TIMERH,#0
       SETB    LEDE
       SETB    LEDF
       SETB    LEDG
       LCALL   UARTO
       CALL    DELAY
       JNB     TTIME,$
       JMP     JK49
JK45:
       MOV     TDATA,#01
       MOV     A,TDATA
       LJMP    JK41
JK49:
       RET
;--------
QWATER:
       JNB     BAO,WATERD
       CLR     OUTL
       CLR     OUTZ
       CLR     OUTH
       CLR     LEDH
       SETB    QSBZ            ;缺水标志
       LCALL   UARTO           ;缺水灯
       CALL    DELAY
       JMP     QUII
WATERD:
       SETB    LEDH
       LCALL   UARTO
       CALL    DELAY
       CLR     QSBZ            ;清缺水标志
       MOV     DJME,#0
       MOV     55H,#40
       JB      DOWNJ,QUII
        MOV      A,WUDATA
QSQ1:   CJNE     A,#1,QSQ2
        SETB     OUTL
        CLR      OUTZ
        CLR      OUTH
        JMP      QUII
QSQ2:   CJNE     A,#2,QSQ3
        SETB     OUTZ
        CLR      OUTL
        CLR      OUTH
        JMP      QUII
QSQ3:   CJNE     A,#3,QUII
        SETB     OUTH
        CLR      OUTZ
        CLR      OUTL
QUII:   RET
;-----------
FMMF:
        JB      DOWNJ,FMMF2
        JNB     QSBZ,FMMF2
        DJNZ    55H,FMMFD
        MOV     55H,#40
        INC     DJME
        MOV     A,DJME
        CLR     C
        SUBB    A,#10
        JC      GOGO
        MOV     DJME,#0
       CLR    OF
       SETB   DOWNJ        ;待机标志
       CLR    OUTL
       CLR    OUTZ
       CLR    OUTH         ;清输出
       CLR    FFFF
       SETB   FENGM
       CLR    QSBZ
       SETB   LEDA
       SETB   LEDB
       SETB   LEDC
       SETB   LEDE
       SETB   LEDF
       SETB   LEDD
       SETB   LEDG
       LCALL  UARTO        ;熄灭所有指示灯       ;/*注意：74164的输出排列是：H=>G=>F=>E=>D=>C=>B=>A*/
       CALL   DELAY
       MOV    TIMERS,#0
       MOV    TIMERM,#0
       MOV    TIMERH,#0
       MOV    WUDATA,#2
       JMP    FMMFD
GOGO:   JNB     FENGM,FMMF2
        CLR     FENGM
        JMP     FMMFD
FMMF2:  SETB    FENGM
FMMFD:
        RET
;<<UART0>>;/发送单个字节;控灯
UARTO:   MOV     A,SHOW1
         XRL     A,SHOW2
         JZ      UUT
         MOV    SCON,#00H
         CLR    TI
         MOV    SBUF,SHOW1
WAO:     JNB    TI,WAO
         MOV     SHOW2,SHOW1
UUT:     RET
;----
;<<红外线遥控>>
;--------------------------------------------------------
REMOTE:                           ;检测50ms 内有没有编码接收
             MOV    40H,#23
REMOTEA:
             MOV    41H,#245
             JNB    TTIME,REMOTE_END
             JNB    WULL,REMOTE_END
             JNB    ONFF,REMOTE_END
             JNB    FULZ,REMOTE_END
REMOTEB:
             JNB    REM,REMOTE0   ;有码发送则跳转
             DJNZ   41H,REMOTEB
             DJNZ   40H,REMOTEA
             CLR    ENABLE;/*45,543us*/
REMOTE_END:
             RET
;---------------------------------------------
REMOTE0:
             JB    ENABLE,REMOTE_END  ;按键没有放开则返回
                                      ;清除上次解码内容
                                      ;接收12 位编码
             MOV   34H,#12
             MOV   30H,#00H
             MOV   31H,#00H
             MOV   32H,#00H
             MOV   33H,#00H
                                      ;解码
                                      ;先找出接收码的开头即8ms 左右的高电平
                                      ;设置高电平时间为4~6ms
                                      ;检测和等待6ms 的高电平
REMOTE1:
             MOV   40H,#30
REMOTE2:
             MOV   41H,#24
REMOTE3:
             JNB   REM,REMOTE1
             DJNZ  41H,REMOTE3
             DJNZ  40H,REMOTE2
             MOV   40H,#15;/*6,003us*/;等待在2ms 内接收到的低电平
REMOTE4:
             MOV   41H,#25
REMOTE5:                              ;1ms 到3ms 内接收到下降沿则跳去解码否则返回
             JNB   REM,ROE1
             DJNZ  41H,REMOTE5
             DJNZ  40H,REMOTE4        ;超出3ms 接收错误返回
             CLR   ENABLE;/*3,123us*/
             JMP   REMOTE_END
ROE1:
             MOV   40H,#30
ROE2:
             MOV   41H,#24
ROE3:
             NOP
             DJNZ  41H,ROE3
             DJNZ  40H,ROE2           ;等待4ms后在开始检测8ms
RTE1:
             MOV   40H,#30;/*4,563us*/
RTE2:
             MOV   41H,#24
RTE3:
             JNB   REM,RTE1
             DJNZ  41H,RTE3
             DJNZ  40H,RTE2
             MOV   40H,#15;/*6,003us*/;等待在2ms 内接收到的低电平
RTE4:
             MOV   41H,#25
RTE5:                                 ;1ms 到3ms 内接收到下降沿则跳去解码否则返回
             JNB   REM,REMOTE66
             DJNZ  41H,RTE5
             DJNZ  40H,RTE4        ;超出3ms 接收错误返回
             CLR   ENABLE;/*3,123us*/
             JMP   REMOTE_END
REMOTE66:    JNB   REM,$
REMOTE6:                               ;等待753us 后采集接收信号;1就是1,0就是0.
             MOV   40H,#188
             DJNZ  40H,$               ;采集接收信号并记录
             MOV   C,REM
             MOV   A,31H
             RLC   A
             MOV   31H,A
             MOV   A,30H
             RLC   A
             MOV   30H,A               ;等待第二个上升沿
             JB    REM,$
             JNB   REM,$               ;等待753us 后采集接收信号
             DJNZ  34H,REMOTE6
             MOV   34H,#12              ;准备再次接收
SEC1:        MOV   40H,#30
SEC2:
             MOV   41H,#24
SEC3:
             JNB   REM,SEC1
             DJNZ  41H,SEC3
             DJNZ  40H,SEC2
             MOV   40H,#15;6,003        ;等待在2ms 内接收到的低电平
SEC4:
             MOV   41H,#25
SEC5:                                   ;1ms 到3ms 内接收到下降沿则跳去解码否则返回
             JNB   REM,SECC
             DJNZ  41H,SEC5
             DJNZ  40H,SEC4             ;超出3ms 接收错误返回
             CLR   ENABLE;/3,123
             JMP   REMOTE_END
SECC:        JNB   REM,$
SEC10:
             MOV   40H,#180
             DJNZ  40H,$                ;采集接收信号并记录
             MOV   C,REM;753
             MOV   A,33H
             RLC   A
             MOV   33H,A
             MOV   A,32H
             RLC   A
             MOV   32H,A                ;等待第二个上升沿
             JB    REM,$
             JNB   REM,$                ;等待700us 后采集接收信号
             DJNZ  34H,SEC10
             MOV   A,33H
             XRL   A,31H
             JNZ   REMOTE8              ;结果非0就是错误.
             MOV   A,32H
             XRL   A,30H
             JNZ   REMOTE8              ;不相同就是错误
REMOTE7:                                         ;置接收正确位
             SETB   RECEIVE             ;置ENABLE 用于检测按键有没有放开
             SETB   ENABLE
             RET
REMOTE8:
             CLR    ENABLE
             CLR    RECEIVE
             RET
;---------------------------------------------

WORK:
             JNB    RECEIVE,WORK_END     ;每次按键都一次响应
             CLR    RECEIVE
             MOV    A,31H
PD1:         CJNE   A,#06FH,PD2
             LJMP   RM1
             JMP    WORK_END
PD2:         CJNE   A,#7BH,PD3
             LJMP   RM3
             JMP    WORK_END
PD3:         CJNE   A,#7FH,PD4
             ;LJMP   RM5
             JMP    WORK_END
PD4:         CJNE   A,#3DH,PD5
             LJMP   RM4
             JMP    WORK_END
PD5:         CJNE   A,#7DH,PD6
             ;LJMP   RM6
             JMP    WORK_END
PD6:         CJNE   A,#77H,WORK_END
             LJMP   RM2
WORK_END:    LCALL  DELAY
             RET
ST0:
	 PUSH       ACC             ;保护现场
         PUSH       PSW
         MOV        TH0,#3CH
         MOV        TL0,#0B0H
         INC        TIMERS
         MOV        R4,TIMERS
         CJNE       R4,#10,T1LAB0       ;1S
         MOV        TIMERS,#0
         INC        TIMERM
         MOV        R4,TIMERM
         CJNE       R4,#60,T1LAB0       ;60S=1M
         MOV        TIMERM,#0
         INC        TIMERH
         MOV        R4,TIMERH
         CJNE       R4,#60,T1LAB0        ;60M=1H
         MOV        TIMERH,#0
         DEC        YTIME
         MOV        A,YTIME
         JNZ        T1LAB0
         SETB       DOWNJ        ;待机标志
         CLR        OUTL
         CLR        OUTZ
         CLR        OUTH         ;清输出
         CLR        FFFF
         SETB       LEDA
         SETB       LEDB
         SETB       LEDC
         SETB       LEDE
         SETB       LEDF
         SETB       LEDD
         SETB       LEDG
         LCALL      UARTO        ;熄灭所有指示灯
         CALL       DELAY
         MOV        TIMERS,#0
         MOV        TIMERM,#0
         MOV        TIMERH,#0
         MOV        TDATA,#0
         MOV        WUDATA,#2
         CLR        FUL
         CLR        OF
         CLR        TR0
T1LAB0:	 POP        PSW             ;恢复现场
	 POP        ACC             ;***恢复A和标志位***
         RETI
       LJMP   MAIN
DELAY:
         mov     R6,#8                         ;延时消除抖动
D1:      mov     R7,#235                       ;7,570us;6MHz
         djnz    R7,$
         djnz    R6,D1
         RET
       LJMP   MAIN
TIME:
	MOV     R2,#249
TIMEL:  NOP
        NOP
        NOP
	DJNZ    R2,TIMEL
	RET
T10MS:  MOV     R3,#10
T10MSL: LCALL   TIME
	DJNZ    R3,T10MSL
T10MSE:
	RET
T50MS:  MOV     R3,#25
T50MSL: LCALL   TIME
	DJNZ    R3,T50MSL
T050L:	RET
       LJMP   MAIN
        END
