$NOMOD51	        ; disable predefined 8051 registers
$INCLUDE (REG52.INC)	; include CPU definition file (for example, 80515) 

;This code blinks the Leds on TrikiBoard. If Motors are connected they 
; would run forward and backward.  (5seconds timing)
; This code also gives an example that how a timer can be used as Background timer
; Using background timer and its Interrupt Service Routine (ISR) has some advantages
; Code to be compiled in Keil.

; THE POWER ON VECTOR COMES HERE
org 0000H
BEGIN:
	LJMP            PWR_ON
;----------------------------------------------------------------------
;THE TF1 OR THE TIMER #1 OVERFLOW INTERRUPT COMES HERE
org 001BH
BEGIN_3:
	LJMP            ISRtimer1
;-----------------------------------------------------------------------

flag	equ 01h      
timing	equ 30h    ;variable to store timing value located at location 30H in internal ram
mot1a	equ P1.4  ;motor 1's control bit a. giving aliases to P1.4 etc. 
mot1b	equ P1.5
mot2a	equ P1.6
mot2b	equ P1.7

ORG 0030H
; Initialization routine----------------------
; this routine sets the ports to some known state
; and Sets & enables the Timer1 and its Interrupt, for reload 
; PLACE YOUR PROCESSOR INITIALIZATION CODE HERE
init: 
	mov p1,#00h
      ;
	mov tmod,#10h
	mov tcon,#45h
	mov ie,#088h
	mov ip,#08h
	mov th1,#03ch
	mov tl1,#0afh     ; values set of 0.05 sec reload
	ret
	;
;--------------------------------------------------------------	
PWR_ON: lcall init
	;

start:	
	clr mot1a    ; run motor/led 1. red
        setb mot1b
	clr mot2a     
        setb mot2b   
        mov timing,#100 ;  100 x 0.05 sec routine = 5sec delay
	setb flag	   	
	jb flag,$      ; jump here itself till flag is not zero (wait)
	; flag was set to zero by the timer ISR routine after 20 interrupts of T1 

	setb mot1a    ; run motor/led 1 in opposite direction. green
        clr mot1b
	setb mot2a     
        clr mot2b   
        mov timing,#100 ;  100 x 0.05 sec routine = 5sec delay
	setb flag	   	
	jb flag,$      ; jump here itself till flag is not zero 
	; flag was set to zero by the timer ISR routine after 20 interrupts of T1 	

	mov p1, #00h   ; all Off
      	mov timing,#20	;20  x 0.05 sec routine = 1sec delay 
	setb flag      
	jb flag,$
	ljmp start	
	
;-------------------timer Interrupt Subroutine 
; this occurs at every 0.05 sec, and then decrements the value in 'timing'.
ISRtimer1:
	mov a,timing
	jz down2
	djnz timing,down2
	clr flag 

down2:  mov th1,#03ch
	mov tl1,#0afh
	reti                         
END
