PDA

View Full Version : Use Timer1 Gate to Capture Pulse Widths



Bruce
- 25th July 2010, 02:10
Here's an example of using the Timer1 gate peripheral to capture/display external pulse widths on an LCD. This PIC has a lot of cool features.


' Name : T1G_1934X.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : MPASM
' Target PIC : 40-pin 16F1934 or similar
' Hardware : LAB-X1 Experimenter Board
' Oscillator : 4MHz external crystal
' Keywords : LCDOUT
' Description : PICBASIC PRO program to demonstrate using Timer1 gate
' in single-pulse mode to capture single pulse event times.
'
' Port B4 connects to port B5 (Timer1 gate), and provides pulse to
' measure by setting B4 high, pausing, then to setting B4 low.
' Note: Open the 16F1934.INC file in your PBP directory, and comment out the default
' __config settings.
ASM
__config _CONFIG1, _FOSC_XT & _WDTE_SWDTEN & _PWRTE_ON & _MCLRE_ON & _BOREN_ON & _IESO_ON & _FCMEN_ON
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _STVREN_ON & _BORV_25
ENDASM

' Define LCD pins
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
Pval Var Word ' Holds pulse time from Timer1
Gosub Init ' Hardware initialization routine

Lcdout $fe, 1 ' Clear LCD at POR

mainloop:
PORTB.4 = 1 ' Toggle RB4 on Timer1 gate pin high
Pauseus 1550 ' Set pulse time by adjusting pause value here
PORTB.4 = 0 ' Toggle Timer1 gate pin low to stop Timer1

Pval.LowByte = TMR1L ' Read Timer1 value
Pval.HighByte = TMR1H

Lcdout $fe, 1, "High Pulse = ",DEC Pval,"uS"

Gosub Reset ' Clear Timer1 count & reset T1 gate
Pause 500
Goto mainloop ' Do it forever

Reset:
T1GCON.3 = 1 ' Reset Timer1 Gate Single-Pulse Acquisition Status bit
TMR1H = 0 ' Clear previous count
TMR1L = 0
Return

Init:
ANSELA = 0 ' porta all digital
ANSELB = 0 ' portb all digital
ANSELD = 0 ' portd all digital
ANSELE = 0 ' porte all digital

TRISB = %00100000 ' RB5 = input for T1 gate, RB4 = output to gate
PORTB = 0 ' Hold T1 gate low at start

T1CON = %00000001 ' Timer1 on, clock is internal Fosc/4
T1GCON = %11011000 ' Timer1 gate enabled, active high, single-pulse mode

Return

End

isaac
- 23rd March 2012, 16:21
Bruce can this be applied to pic18f452?