8051紅綠燈設計

8051紅綠燈設計

兩個七段顯示器+黃綠紅LED

 

;  TRAFFIC_LIGHTS.ASM
;  2014/7/27
;  Joe Li
 GREEN  EQU 11H	
 YELLOW EQU 12H	
 RED    EQU 13H
 BLACK  EQU 14H
 ORG 000H
 MOV R1,#0
 MOV DPTR,#TABLE
 MOV Green, #11111110B	
 MOV YELLOW,#11111101B	
 MOV RED,   #11111011B 
 MOV BLACK ,#11111111B
MAIN:
 MOV  P1,GREEN
 MOV  P3,RED
 MOV R3,#2      	;LOOP number of times	   
 MOV R7,#1        	;flag 
Initcall:          
 MOV R1,#0
 MOV R5,#5	       ;5 SEC	green
 MOV R6,#2         ;2 sec   yellow
 MOV A,R1       
 MOVC A,@A+DPTR	
WAIT5Sec:
 CJNE R7,#0,FL2	   ;check r7 flag !=0 goto  FL2
 CJNE R7,#1,FL0
FL2:
  CALL LEDp2
  CJNE R7,#0,FRUN
FL0:
  CALL LEDp0
FRUN:
 CALL DELAY
 CALL AddArray
 CJNE R7,#0,PL2
 CJNE R7,#1,PL0
PL2:
 CALL LEDp2
 MOV P0,#11111111B    ;close 7-led
 CJNE R7,#0,GORUN
PL0:
 MOV P2,#11111111B    ;close 7-led
 CALL LEDp0
GORUN:
 DJNZ R5,WAIT5Sec	  ;Run 5 number
 CALL CHANGE		  ;call TRAFFIC_LIGHTS_CHANGE G->Y->R
 MOV B,R7             ;Traffic_7-led flag
 XRL  B,#01H          ;  0-->1    1-->0          XOR 
 MOV R7,B             
 DJNZ R3,Initcall       
 JMP MAIN

AddArray:              ;Table Address Counter 
 INC R1
 MOV A,R1
 MOVC A,@A+DPTR			 
 RET
LEDp2:                 ;Show P2 7-Led
 MOV P2,A 
 RET
LEDp0:                 ;Show P0 7-Led
 MOV P0,A 
 RET
CHANGE:	  			   ;TRAFFIC_LIGHTS_CHANGE
CJNE R7,#1,DD1
CJNE R7,#0,DD2
RET
DD1:
   MOV  P3,BLACK  
   CALL DELAY 
   CALL AddArray
   CALL LEDp0
   MOV  P3,GREEN
   CALL DELAY
   CALL AddArray
   CALL LEDp0	    
   MOV  P1,RED
   MOV  P3,YELLOW
  
DDD1:
   CALL DELAY
   CALL AddArray
   CALL LEDp0
   DJNZ R6	,DDD1
   MOV  P3,#RED
   CALL DELAY
   CALL AddArray
   CALL LEDp0
   MOV  P1,GREEN
   MOV  P3,RED
   RET
DD2:  
   MOV  P1,BLACK
   CALL DELAY
   CALL AddArray
   CALL LEDp2
   MOV  P1,GREEN
   CALL DELAY
   CALL AddArray
   CALL LEDp2
   MOV  P3,RED
   MOV  P1,YELLOW
DDD2:
   CALL DELAY
   CALL AddArray
   CALL LEDp2
   DJNZ R6,DDD2
   MOV  P1,#RED
   CALL DELAY
   CALL AddArray
   CALL LEDp2
   MOV  P3,GREEN
   MOV  P1,RED
   RET
DELAY:
   MOV R4,#20
   MOV TMOD,#00000001B
D1:
   MOV TH0,#3CH
   MOV TL0,#0B0H
   SETB TR0
   JNB TF0,$
   CLR TF0		;clear
   DJNZ R4,D1
   RET
TABLE:
 DB 10010000B ;9
 DB 10000000B ;8		     	
 DB 11111000B ;7		        
 DB 10000010B ;6			    						
 DB 10010010B ;5			
 DB 10011001B ;4			    
 DB 10110000B ;3				  
 DB 10100100B ;2					
 DB 11111001B ;1					  
 DB 11000000B ;0						
END

 

 

 

 

 

示意圖

image

 

 

 

 

大家一起加入blogads 賺零用錢!!