;EXAM2.ASM
;USE p3.2,p3.3 to Input Port
;   and Control toggle LED1,LED2 on/off

	ORG    	0000H	

;INIT:	CLR	EA		;disable All intterrupt
;	ANL	p3,#11000011B	;clear bit 2 to 5


START:	MOV	P1,#0FFH	;Clear Port P1 All LED OFF
;*** SET PORT = "1" before use to Input	 ***
	SETB	p3.2		;SET P3.2 TO Input
	SETB	p3.3		;SET P3.3 TO Input

;*** Check Key Press ***
LOOP:	JNB	p3.2,WAIT1	;if p3.2 = "0"  Debounce Check
L1:	JNB	p3.2,KEY1P	;If p3.2 still "0' Jump to KEY1P

	JNB	p3.3,WAIT2	;if p3.3 = "0"  Debounce Check
L2:	JNB	p3.3,KEY2P	;If p3.3 still "0' Jump to KEY2P

	AJMP	LOOP		;Scan Key Loop

;*** KEY1 PRESS ***
KEY1P:	JNB	p3.2,KEY1P	;WAIT Release Key
	JB	p1.0,K1SET	;if  p3.2=0
	SETB	P1.0		;LED1 Off
	LCALL	DISDLY
	AJMP	LOOP

K1SET:	CLR	P1.0		;LED1 On
	LCALL	DISDLY
	AJMP	LOOP

;*** KEY2 PRESS ***
KEY2P:	JNB	p3.3,KEY2P	;WAIT Release Key
	JB	p1.1,K2SET	;if  p3.3=0
	SETB	P1.1		;LED2 Off
	LCALL	DISDLY
	AJMP	LOOP

K2SET:	CLR	P1.1		;LED2 On
	LCALL	DISDLY
	AJMP	LOOP

;*** Wait for test Debounce ***
WAIT1:	MOV	R2,#100
	LCALL	DMSEC
	LJMP	L1;
WAIT2:	MOV	R2,#100
	LCALL	DMSEC
	LJMP	L2;

;*** LED Display Delay ***
DISDLY:	MOV	R2,#2
	LCALL	DMSEC
	RET

; ********** DMSEC SUB ********** 03
; DELAY 1/1000 SECOND
; IN  = R2
; REG = R2,R3

DMSEC:   MOV    R3,#230         ;1 MSEC LOOP
DMSEC1:  NOP
	 NOP
	 DJNZ   R3,DMSEC1
	 DJNZ   R2,DMSEC
	 RET

	 END
