Edit 1: I'm starting to wonder if there is a fast transition to zero which terminates TMR1 but is too fast for me to see on the scope at 10 ms/cm. I'm going to remove the external pulse source and insert a software delay loop to test the timer independently of the external pulse source.
I have also inserted some lines of ******* in the code to mark where the actual timing begins (with comments). There is also a 10 micro sec trigger pulse to the external device which then generates the longer 40 ms pulse I'm trying to measure.
End Edit 1:
I'm trying to measure the width of a positive going pulse. My scope shows the pulse width is about 40 milliseconds. TMR1 measurements on a 12F683 with an 8MHz oscillator (0.5 us clock time) show a pulse width of h'417E' = 16766 x 0.5 = 8383us or about 8.4 milliseconds.
I have tried just turning the timer on (bsf) and off (bcf) TMR1ON. The code below uses the TMR1 gate input T1G. Both methods give the same results.
The pulse is applied to Pin 3, GPIO,T1GATE. The code waits for GPIO,T1GATE to go Hi and sets Pin 5, GP2 Hi. The timer is then started. The reverse process occurs at the end of the pulse. I used GP2, Pin 5 just to confirm that the pulse was getting into the PIC correctly by looking at GPIO,T1GATE and Pin 5 simultaneously with a 2 channel scope. Both waveforms were identical.
Where am I going wrong? All help would be appreciated.
Thanks, Jess
I have also inserted some lines of ******* in the code to mark where the actual timing begins (with comments). There is also a 10 micro sec trigger pulse to the external device which then generates the longer 40 ms pulse I'm trying to measure.
End Edit 1:
I'm trying to measure the width of a positive going pulse. My scope shows the pulse width is about 40 milliseconds. TMR1 measurements on a 12F683 with an 8MHz oscillator (0.5 us clock time) show a pulse width of h'417E' = 16766 x 0.5 = 8383us or about 8.4 milliseconds.
I have tried just turning the timer on (bsf) and off (bcf) TMR1ON. The code below uses the TMR1 gate input T1G. Both methods give the same results.
The pulse is applied to Pin 3, GPIO,T1GATE. The code waits for GPIO,T1GATE to go Hi and sets Pin 5, GP2 Hi. The timer is then started. The reverse process occurs at the end of the pulse. I used GP2, Pin 5 just to confirm that the pulse was getting into the PIC correctly by looking at GPIO,T1GATE and Pin 5 simultaneously with a 2 channel scope. Both waveforms were identical.
Where am I going wrong? All help would be appreciated.
Thanks, Jess
Code:
;12F683 excerpt - Measure Pulse Width
MAIN movlw b'00010111' ; Set all Analog(GP0, GP1, GP2, GP4)
; as digital I/O
movwf CMCON0 ; digital I/O
bsf STATUS,RP0 ; bank 1
clrf ANSEL ; digital I/O
movlw b'00011010' ; GP1, GP3, GP4 as Digital inputs
movwf TRISIO ; setup TRISIO as desired
;
; setup 8-MHz INTOSC
;
movlw b'01110000' ;
movwf OSCCON ; 8-MHz INTOSC system clock
Stable btfss OSCCON,HTS ; oscillator stable?
goto Stable ; no, branch
bcf STATUS,RP0 ; bank 0
;
; initialize RS-232 Serial I/O
;
call Init232
bsf INTCON,GIE ; turn on interrupts for RS232
; Clear Screen and Show Title
_Title "Measure Pulse Width\r\n\n"
bcf PIR1,TMR1IF ; clear TMR1 overflow flag
bsf STATUS,RP0 ; bank 1
movf OSCCON,W
bcf STATUS,RP0 ; bank 0
call PutByte ; Print OSCCON value
_Print " OSCCON value\r\n\n"
movf PIR1,W
call PutByte ; Print PIR1 value
_Print " PIR1 value\r\n\n"
MT _Print "\n MT - Press a key to send a Ping: \r\n"
call Get232
_Print "\nConfirm Key Pressed\r\n\n"
; ************* Measure Pulse Width ************
;
; turn off RS232 interrupts
;
bcf INTCON,GIE ; turn off interrupts
;
; setup TMR1 Turn Off Timer1
;
clrf CMCON1 ;
bsf CMCON1,T1GSS; Enable T1G gate input
movlw b'11000100' ; Set up TMR1 for T1G input
movwf T1CON ; TMR1 Off
;bcf T1CON,TMR1ON; TMR1 Off
bcf PIR1,TMR1IF ; clear TMR1 overflow flag
clrf TMR1L ; Clear Lo counter register
clrf TMR1H ; Clear Hi counter register
;*********************************************************
;*********************************************************
; Trigger Hi duration time >= 10uS
;
bsf GPIO,TRIGOUT; Generate Trigger
call Dly10 ; 10 uS
bcf GPIO,TRIGOUT
;The above 3 lines create a trigger pulse to another device which then generates the
;40 ms pulse.
;
;*********************************************************
;*********************************************************
;
EchoWt btfss GPIO,T1GATE ; wait for PULSE Hi
goto EchoWt ; wait til Hi
;********************************************************
; PULSE STARTS HERE - MEASURE Pulse duration
;********************************************************
bsf T1CON,TMR1ON; TMR1 ON
bsf GPIO,2 ; Pin 5 Hi when PULSE Hi
;
; wait for Hi to Lo transition
;
EW2 btfsc GPIO,T1GATE ; wait for PULSE Lo
goto EW2 ; wait til Lo
bcf T1CON,TMR1ON; stop TMR1
;********************************************************
; PULSE STOPS HERE
;********************************************************
bcf GPIO,2 ; Pin 5 Lo when PULSE Lo
;
; Enable interrupts
;
bsf INTCON,GIE ; turn on RS232 interrupts
;
movf PIR1,W
call PutByte ; Print PIR1 value
_Print " Overflow if PIR1 is odd value\r\n\n"
bsf STATUS,RP0 ; bank 1
movf OSCCON,W
bcf STATUS,RP0 ; bank 0
call PutByte ; Print OSCCON value
_Print " OSCCON value\r\n\n"
movf TMR1H,W ;
movwf WH
call PutByte ;
movf TMR1L,W ;
movwf WL
call PutByte ;
_Print " Hex 500 nS periods\r\n\n"
; divide count by two to get microSec (8 MHz clock)
bcf _C ; clear carry
RRF WH,f ; rotate Hi right, bit 0 into C
RRF WL,f ; rotate Lo right, C into bit7,
bcf _C ; discard Lo carry bit
movf WH,W ; counter high byte
call PutByte
movf WL,W ; counter low byte
call PutByte
_Print " Pulse Width , Hex Microseconds\r\n\n"
goto MT
end
Last edited: