$NOMOD51
$include (c8051f020.inc)               ; Include register definition file.


				ORG	0H

    		MOV   WDTCN, #0DEh
    		MOV   WDTCN, #0ADh 
		
				MOV   XBR2, #0x40 ; Enable the Port I/O Crossbar

				; Set P1 and 2 as digital output in push-pull mode.  
				ORL   P1MDOUT,#0xff
				ORL   P2MDOUT,#0xff	

				MOV	R0,#20
				MOV	R1,#0			   ;Set time value = 0, seconds
				MOV	R2,#0			   ;minutes
				MOV	R3,#0			   ;hours
				ACALL SETDIS		 ;initialise the display
				MOV TMOD,#0x01	 ;initialise counter-timer 0 

REPEAT:	MOV	TH0,#0x3C
		    MOV	TL0,#0xB0
		    SETB TR0

WAIT:	  JNB TF0,WAIT
		    CLR TR0
		    CLR TF0
     	  DJNZ R0,REPEAT
		    MOV	TH0,#0x3C
		    MOV	TL0,#0xB0
		    SETB TR0
	    	MOV	R0,#19
		    CPL P2.3			   ;output every second
		    ACALL INCT			 ;Increment time
		    ACALL DIST			 ;Display time
		    AJMP WAIT
		
SETDIS: MOV	A,#30H			 ;Display initialisation routine
				ACALL	COMNWRT
				ACALL	DELAY1
				MOV	A,#0CH
				ACALL	COMNWRT
				ACALL	DELAY1
				MOV	A,#01
				ACALL	COMNWRT
				ACALL	DELAY2
				MOV	A,#06H
				ACALL	COMNWRT
				ACALL	DELAY1
				RET
		
INCT:		
		    MOV A,R1 	       ;	Update time count routine
				;;;;;	Your code here

				;;;;; Your code


INCE:	  RET
		
DIST:	  MOV	A,#01			   ; Update display routine
		    ACALL	COMNWRT    ; Reset display   	
		    ACALL	DELAY2	   ;Long delay
		    MOV	A,R3   	     ;Most significant digit first: Hours		//MSD

				;;;;;	Your code here

				;;;;; Your code

		    RET		;Return to WAIT routine

COMNWRT:							   ;send command to LCD
				MOV	P1,A			   ;copy reg A to Port 1
				CLR	P2.0			   ;RS= 0 for command
				CLR	P2.1			   ;R/W/= 0 for write
				SETB	P2.2		   ;E=1 for high pulse
				ACALL	DELAY1     ;give LCD some time
				CLR	P2.2			   ;E=0 for H-to-L pulse
				RET

DATAWRT:							   ; write data to LCD
				MOV	P1,A			   ;copy reg A to Port 1
				SETB	P2.0		   ;RS= 1 for data
				CLR	P2.1			   ;R/W/= 0 for write
				SETB	P2.2		   ;E=1 for high pulse
				ACALL	DELAY1
				CLR	P2.2			   ;E=0 for H-to-L pulse
				RET

DELAY1:	MOV	R5,#30       ;Short delay
LP1: 		DJNZ	R5,LP1
				RET
		
DELAY2:	MOV	R5,#50 			 ;long delay 
HERE2:	MOV	R4,#50	
HERE: 	DJNZ	R4,HERE
				DJNZ	R5,HERE2
				RET
		
DELAY3:	MOV	R5,#250 		 ;extra long delay 
HERE4:	MOV	R4,#250
HERE3: 	DJNZ	R4,HERE3
				DJNZ	R5,HERE4
				RET

		END