
;红外接P1.3(INT1)/P1.4(T0);     P1.5（T1）接按键    
;P1.7(绿）;P1.6(红);P1.5(蓝)   P1.4/P1.3/P1.2接LED
RRR BIT P1.5;蓝
BBB BIT P1.6;红
GGG BIT P1.7;绿

;计数器
COUNTER   EQU 23H
COUNTER1  EQU 24H

LDBS      EQU 27H;亮灯标志寄存器
T28       EQU 28H

ORG 0000H
AJMP START
ORG 0BH
AJMP TOO
RETI
ORG 013H
AJMP INT0123
RETI
	NOP
	NOP
AJMP START

;初始化
CUSU:MOV P1,#00
MOV P3,#0FFH
MOV TMOD,#10B
MOV TH0,#20;236
SETB PT0
SETB EA
SETB EX1
SETB IT1
SETB ET0;调用初始化
MOV 38H,#1;37H与38H是设置输出PWM可变周期及比例.
MOV 39H,#250
MOV 40H,#250;39H给最大时返回值.
RET




;主程序开始++++++++++++++++++++++++++++++++
START:ACALL CUSU
      ACALL BBBH;          调用红渐亮
      SETB  BBB ;     红亮
      ACALL DELAY2
      ACALL GGGH ;         调用绿渐亮
      SETB  BBB;      红亮
      SETB  GGG;      绿亮
      ACALL DELAY2
START1:ACALL RRRHBBBGGGL;  调用蓝渐亮/红绿渐灭
      SETB  RRR;      蓝亮
      CLR   BBB;      红灭
      CLR   GGG;      绿灭
      ACALL DELAY2
      ACALL BBBGGGH;       调用红绿渐亮
      SETB  BBB;      红亮 
      SETB  GGG;      绿亮
      ACALL DELAY2
      ACALL BBBRRRL;       调用红蓝渐灭
      CLR   BBB;      红灭
      CLR   RRR;      蓝灭
      ACALL DELAY2
      ACALL BBBHGGGL;      调用红渐亮/绿渐灭 55555  
      SETB  BBB;      红亮
      CLR   GGG;      绿灭
       ACALL DELAY2
      ACALL GGGH;         调用绿渐亮      
      SETB  GGG;      绿亮
       ACALL DELAY2
      ACALL RRRHBBBGGGL;   调用蓝渐亮/红绿渐灭
      SETB  RRR;      蓝亮
      CLR   BBB;      红灭
      CLR   GGG;      绿灭
       ACALL DELAY2
      ACALL BBBGGGH;       调用红绿渐亮
      SETB  BBB;      红亮 
      SETB  GGG;      绿亮
       ACALL DELAY2
      ACALL BBBRRRL;       调用红蓝渐灭
      CLR   BBB;      红灭   
      CLR   RRR;      蓝灭
       ACALL DELAY2
      ACALL BBBHGGGL;      调用红渐亮/绿渐灭*
      SETB  BBB;      红亮 
      CLR   GGG;      绿灭
       ACALL DELAY2
      ACALL GGGH;          调用绿渐亮   
      SETB  GGG;      绿亮
       ACALL DELAY2
      AJMP START1;转移循环 
;主程序到此结束+++++++++++++++++++++++++++++++++++++++      
     





;以下被调用程序++++++++++++++++++++++++++++++++++++         

; 红渐亮
BBBH: MOV LDBS,#0;作标志,为了在按键或中断解码时继续调用顺序运行程序
      MOV  COUNTER,38H
      MOV  COUNTER1,39H    
BBBH1: mov 10h,#1
j1:     MOV 26H,#40
BBBH2:ACALL BBBH4
      ACALL INTI;调用扫描按键*****************
      JB 01H,BBBH2;查标志01h为0顺序进行，为1则不变循环
      DJNZ  26H,BBBH2
      djnz 10h,j1
      INC   COUNTER
      DEC   COUNTER1
      MOV  A,COUNTER
      CJNE A,40H,BBBH3
BBBH3:JC   BBBH1 
      MOV LDBS,#7
      RET

;绿渐亮
GGGH: MOV  LDBS,#1;作标志,为了在按键或中断解码时继续调用顺序运行程序
      MOV  COUNTER,38H
      MOV  COUNTER1,39H   
GGGH1:mov 10h,#1
 j2:   MOV  26H,#40
GGGH2:ACALL GGGH4
      ACALL INTI;*****************
      JB 01H,GGGH2
      DJNZ  26H,GGGH2 
      djnz 10h,j2
      INC   COUNTER
      DEC   COUNTER1
      MOV  A,COUNTER
      CJNE A,40H,GGGH3
GGGH3:JC   GGGH1 
  MOV LDBS,#7
      RET

;  蓝渐亮/红绿渐灭
RRRHBBBGGGL: MOV  LDBS,#2;作标志,为了在按键或中断解码时继续调用顺序运行程序
             MOV  COUNTER,38H
             MOV  COUNTER1,39H  
RRRHBBBGGGL1:mov 10h,#1
j3:            MOV  26H,#40
RRRHBBBGGGL2: ACALL RRRHBBBGGGL4
              ACALL INTI;*****************
             JB 01H,RRRHBBBGGGL2   
             DJNZ  26H,RRRHBBBGGGL2  
  	     djnz 10h,j3
             INC   COUNTER
             DEC   COUNTER1
             MOV  A,COUNTER
             CJNE A,40H,RRRHBBBGGGL3
RRRHBBBGGGL3:JC   RRRHBBBGGGL1 
  MOV LDBS,#7
            RET

;红绿渐亮
BBBGGGH:  MOV  LDBS,#3;作标志,为了在按键或中断解码时继续调用顺序运行程序
          MOV  COUNTER,38H
          MOV  COUNTER1,39H     
BBBGGGH1:mov 10h,#1
j4:         MOV  26H,#40
BBBGGGH2: ACALL BBBGGGH4
           ACALL INTI;*****************
             JB 01H,BBBGGGH2 
          DJNZ  26H,BBBGGGH2 
          djnz 10h,j4
          INC   COUNTER
          DEC   COUNTER1
          MOV  A,COUNTER
          CJNE A,40H,BBBGGGH3
BBBGGGH3: JC   BBBGGGH1 
  MOV LDBS,#7
          RET

;      红蓝渐灭
BBBRRRL:  MOV  LDBS,#4;作标志,为了在按键或中断解码时继续调用顺序运行程序
          MOV  COUNTER,38H
          MOV  COUNTER1,39H   
BBBRRRL1:mov 10h,#1
j5:         MOV 26H,#40
BBBRRRL2: ACALL BBBRRRL4
           ACALL INTI;*****************
             JB 01H,BBBRRRL2
          DJNZ  26H,BBBRRRL2 
          djnz 10h,j5
          INC   COUNTER
          DEC   COUNTER1
          MOV  A,COUNTER
          CJNE A,40H,BBBRRRL3
BBBRRRL3: JC   BBBRRRL1 
          MOV LDBS,#7
          RET

;      红渐亮/蓝渐灭
BBBHRRRL:  MOV  LDBS,#5;作标志,为了在按键或中断解码时继续调用顺序运行程序
          MOV  COUNTER,38H
          MOV  COUNTER1,39H   
BBBHRRRL1:mov 10h,#1
j6:        MOV  26H,#40
BBBHRRRL2: ACALL BBBHRRRL4
           ACALL INTI;*****************
           JB 01H,BBBRRRL2
          DJNZ  26H,BBBHRRRL2 
          djnz 10h,j6
          INC   COUNTER
          DEC   COUNTER1
          MOV  A,COUNTER
          CJNE A,40H,BBBHGGGL3
BBBHRRRL3: JC   BBBHRRRL1 
  MOV LDBS,#7
          RET

;      红渐亮/绿渐灭
BBBHGGGL: MOV  LDBS,#6;作标志,为了在按键或中断解码时继续调用顺序运行程序
          MOV  COUNTER,38H
          MOV  COUNTER1,39H    
BBBHGGGL1: mov 10h,#1
 j7:          MOV  26H,#40
BBBHGGGL2: ACALL BBBHGGGL4
          JB 01H,BBBHGGGL2
          DJNZ  26H,BBBHGGGL2 
          djnz 10h,j7
          INC   COUNTER
          DEC   COUNTER1
          MOV  A,COUNTER
          CJNE A,40H,BBBHGGGL3
BBBHGGGL3: JC   BBBHGGGL1 
          MOV LDBS,#7
          RET

;***************************************************************
BBBHGGGL4:  SETB  BBB   ;红渐亮/绿渐灭
            CLR  GGG        
            ACALL DELAY
            CLR  BBB
            SETB  GGG
            ACALL DELAY1
            RET

BBBHRRRL4:SETB  BBB        ;红渐亮/蓝渐灭
	 CLR  RRR         
         ACALL DELAY
         CLR   BBB          
         SETB  RRR       
         ACALL DELAY1
         RET                   

BBBRRRL4:CLR  BBB        ;红蓝渐灭
	 CLR  RRR          
         ACALL DELAY
         SETB BBB          
         SETB RRR        
         ACALL DELAY1
         RET                 

BBBGGGH4:SETB BBB          ;红绿渐亮
         SETB GGG
         ACALL DELAY
         CLR  BBB
	 CLR  GGG        
         ACALL DELAY1
         RET

RRRHBBBGGGL4: SETB RRR          ;蓝渐亮/红绿渐灭
              CLR   BBB
              CLR   GGG
              ACALL DELAY
              CLR   RRR
              SETB  BBB
              SETB  GGG
              ACALL DELAY1
              RET

GGGH4:SETB  GGG           ; 绿渐亮
      ACALL DELAY
      CLR   GGG
      ACALL DELAY1
      RET

BBBH4:SETB  BBB            ; 红渐亮
      ACALL DELAY
      CLR   BBB
      ACALL DELAY1
      RET

;可动态延时
DELAY:MOV 25H,COUNTER
      DJNZ 25H,$
      RET

DELAY1:MOV 25H,COUNTER1
       DJNZ 25H,$
       RET


;LED切换变化时的延时++++++++++++++++++++++++++++++++++++++++++
DELAY2:MOV LDBS,#7;作标志,为了在按键或中断解码时继续调用顺序运行程序
      MOV 36H,#10
DDD1: MOV R5,#255
DDD:  ACALL DELAY3
DELAY5: ACALL INTI;调用按键检测处理程序
       JB 01H,DELAY5 ;01H为标志位，为1则暂停/0顺序运行，
                     ;此行是在暂停时不停检测按键。    
       DJNZ R5,DDD
       DJNZ 36H,DDD1
       RET

DELAY3:NOP
       NOP 
       NOP       
       ACALL DELAY
       NOP
       NOP 
       NOP
       ACALL DELAY1
       RET
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++






;被调用按键检测处理程序++++++++++++++++++++++++++++++++++++++++++++++
INTI:JNB P3.5,INTII
KLL:  RET
INTII:MOV 37H,P1;为了进入关闭后退出恢复原先状态。
      ACALL DY   
      JB P3.5,KLL
      MOV 35H,#255
INTIII: ACALL DY        
        ACALL DY
        ACALL DY           
      DJNZ 35H,INTIII
      JB P3.5,PPP
      MOV P1,#00
INTIP:;长按按键关闭输出++++
      SETB P1.3
      ACALL DELAY3
      JNB P3.5,INTIP
      ACALL DELAY3      
      JNB P3.5,INTIP

INTIP1:ACALL DELAY3
      JB P3.5,INTIP1
      ACALL DELAY3
      JB P3.5,INTIP1

INTIP2:ACALL DELAY3
       JNB P3.5,INTIP2
       ACALL DELAY3 
       JNB P3.5,INTIP2

INTIP3:CLR P1.3
       MOV P1,37H
       RET
;+++++++++++++++++++++++++
PPP:CPL 01H
    JNB 01H,JHJ
    SETB P1.4
    RET
JHJ:CLR P1.4
    RET
;按键处理程序至此结束+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++






;为了让PWM信号持续输出保持LED同样的亮度（不闪烁）给调用之用
DY:  MOV A,LDBS;13us+524us=537us
     RL A    
     MOV DPTR,#KEYFUNTB
     JMP @A+DPTR     
KEYFUNTB:  AJMP ABBBH
           AJMP AGGGH
	   AJMP ARRRHBBBGGGL
	   AJMP ABBBGGGH
	   AJMP ABBBRRRL
	   AJMP ABBBHRRRL
	   AJMP ABBBHGGGL
	   AJMP ADELAY3

ABBBH:     ACALL BBBH4;每节524us
           NOP
           NOP
           RET;++++++++++++++++++
AGGGH:     ACALL GGGH4
           NOP
           NOP
           RET
ARRRHBBBGGGL:ACALL RRRHBBBGGGL4
             RET
ABBBGGGH:    ACALL BBBGGGH4
             NOP
             RET
ABBBRRRL:    ACALL BBBRRRL4
             NOP
             RET
ABBBHRRRL:   ACALL BBBHRRRL4
             NOP
             RET
ABBBHGGGL:   ACALL BBBHGGGL4
             NOP
             RET
ADELAY3:     ACALL DELAY3
             RET
 ;++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
 
 


;以下是中断解码程序   
INT0123:PUSH ACC     
        CLR EX1
         MOV T28,#16
INT0124:JB P3.3,INTRET
        ACALL DY;调用LED请灯持续点亮总时间约525us        
        DJNZ T28,INT0124
	JNB P3.3,$
;9ms判断结束-------------------------------
       MOV T28,#9
INT01235: ACALL DY
       DJNZ T28,INT01235
       MOV R1,#41H
       MOV R2,#4
INT01238: MOV R3,#8
      

INT01236:
KL:    JNB P3.3,$  ;4.5ms判断结束-------------------------------
       ACALL DY
       ACALL DY
       MOV C,P3.3
       JNC INT01237
       ACALL DY
       JB P3.3,$
INT01237:MOV A,@R1
       RRC A
       MOV @R1,A     
       DJNZ R3,INT01236
       INC R1
       DJNZ R2,INT01238
;读码装入41H/42H(用户码）43H/44H(数据识别码)完成.+++++++++++++++++++



     MOV A,41H;比较高8位地址码
     XRL A,#2H ;判断1AH的值是否等于00000000，相等的话A为0
     JNZ INTRET;如果不相等说明解码失败退出解码程序
     MOV A,42H;比较低8位地址
     XRL A,#0BDH ;再判断高8位地址是否正确
     JNZ INTRET;如果不相等说明解码失败退出解码程序
     MOV A,43H;比较数据码和数据反码是否正确?
     CPL A
     XRL A,44H ;将1CH的值取反后和1DH比较 不同则无效丢弃，核对数据是否准确
     JNZ INTRET;如果不相等说明解码失败退出解码程序
;判读入码是否成功----------完成---------------------------------------

     MOV A,44H;按键数值判断执行
     CJNE A,#0E7H,INTRET 
     CPL P1.4;解码成功发光二极管点亮指示!
     CPL 01H

  
INTRET:MOV T28,#120
INTRET1:ACALL DY
       CPL P1.2
       JNB P3.3,INTRET
       DJNZ T28,INTRET1
      SETB EX1      
      POP ACC
      CLR P1.2
      RETI
TOO:  INC 28H
      RETI

  END   