
/*This routines makes measurement for a certain period of time
Msr_dutycyc:   
            push ACC
            call TI0_init
            call TI1_init
            setb P3.7           ;Startup the temp Sensor
            jb   P3.2,$         
            jnb  P3.2,$         ;Waiting for sensor startup
            setb EA             
            jnb  Int2,$         ;Waiting for the first 1-0 interrupt to start measurement
            clr  EA             
chk_pin:    jnb  P3.2,$         ;Wait here till timer overflows which is 
            jb   TF1,Ovr_flw    ;preloaded to overflow after certain period of time
            jb   P3.2,$         ;and all the periods within this period are measured
            jnb  P3.2,chk_pin
Ovr_flw:    setb EA
            jb   Int2,$
            clr  EA
            call Addoffset
            clr P3.7 
            pop  ACC
            ret


;*********Timer0 and Int0 Intialisation***************
;Timer0 in 16 bit timer mode an di initialise external interrupt
TI0_init:
        anl TMOD,#0F0h      
        orl TMOD,#009h          
        mov TL0,#000h
        mov TH0,#000h
        setb IT0
        clr  IE0
        clr  TR0
        clr  TF0
        setb EX0
        ret

;**********Timer1 intialisation**************************
;Timer1 in 16 bit timer mode
TI1_init:
        anl TMOD,#00Fh
        orl TMOD,#010h
        mov TL1,#LOW(0FFFFh-OFFSET)
        mov TH1,#HIGH(0FFFFh-OFFSET)
        clr  TR1
        clr  TF1
        ret
 
;Interrupt which handles the 1-0 transitions

Interrupt0:
        push ACC
        jnb  Int2,FrstInt  
        clr  TR1
        clr  TR0
        mov  T0L,TL0 
        mov  T0H,TH0
        mov  T1L,TL1
        mov  T1H,TH1
        clr  TF1
        clr  Int2
        pop ACC
        ret
;********When measuring the first routine***********
FrstInt:
        setb TR1
        setb TR0
        setb Int2
        pop ACC
        ret

AddOffset:
            push ACC
            push PSW
            clr  C
            mov a,T1L
            add a,#LOW(OFFSET) ; this offset is set to so that             
            mov T1L,a          ;timer1 overflows after some time 
            mov a,T1H          ; to count a certain no of periods
            addc a,#HIGH(OFFSET)
            mov T1H,a
            pop PSW
            pop ACC
            ret
      
