



              FLAG   equ   20h	;temporary storage of p1 values(switch array values)


org 0000h

     ljmp sa_main

org 0030h

;------------------------------20ms delay----------------------------------

delay_20ms:         push 00
                    push 01
                    mov r0,#36
delay_20ms_loop1:   mov r1,#255
delay_20ms_loop2:   djnz r1,delay_20ms_loop2
                    djnz r0,delay_20ms_loop1 
                    pop 01
                    pop 00
                    ret
;------------------------------main code------------------------------------

sa_main:         mov p0,#00h             ;output port/devices
                 mov p1,#0ffh             ;input port/switches   
                 mov FLAG,#0ffh		 ;device status reset
                 mov sp,#64h
                 mov IE,#00h										               

sa_main1:        mov p1,#0ffh                    
                 mov a,p1
                 cjne a,#0ffh,sa_main1 		 ;check if all keys are released initially

sa_loop:		 acall delay_20ms
                 mov a,p1
                 cjne a,#0ffh,debounce         ;if key pressed, check debounce
                 sjmp sa_loop
				 
debounce:   	 acall delay_20ms
                 mov a,p1
                 cjne a,#0ffh,sa_check         ;if key pressed, check switches
                 sjmp sa_loop     

sa_check:        jnb acc.0, flg0	  ;check if p1.0 is grounded
                 jnb acc.1, flg1	  ;check if p1.1 is grounded
                 jnb acc.2, flg2	  ;check if p1.2 is grounded
                 jnb acc.3, flg3	  ;check if p1.3 is grounded
                 jnb acc.4, flg4	  ;check if p1.4 is grounded
                 jnb acc.5, flg5	  ;check if p1.5 is grounded
                 jnb acc.6, flg6	  ;check if p1.6 is grounded
                 jnb acc.7, flg7	  ;check if p1.7 is grounded							
                 
flg0:           cpl FLAG.0			  ;if p1.0 is pulled low,compliment FLAG.0
                  sjmp sa_on
flg1:           cpl FLAG.1			  ;if p1.1 is pulled low,compliment FLAG.1
                  sjmp sa_on
flg2:           cpl FLAG.2			  ;if p1.2 is pulled low,compliment FLAG.2
                  sjmp sa_on
flg3:           cpl FLAG.3			  ;if p1.3 is pulled low,compliment FLAG.3
                  sjmp sa_on
flg4:           cpl FLAG.4			  ;if p1.4 is pulled low,compliment FLAG.4
                  sjmp sa_on
flg5:           cpl FLAG.5			  ;if p1.5 is pulled low,compliment FLAG.5
                  sjmp sa_on
flg6:           cpl FLAG.6			  ;if p1.6 is pulled low,compliment FLAG.6
                  sjmp sa_on
flg7:           cpl FLAG.7

sa_on:          mov p0,FLAG			  ;FLAG holds temporary status of p1, move the temporaryly stored status to p0
                  sjmp sa_loop


end

