Use Timer1 Gate to Capture Pulse Widths


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Posts
    2,405

    Default Use Timer1 Gate to Capture Pulse Widths

    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.
    Code:
    ' 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
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  2. #2
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default Re: Use Timer1 Gate to Capture Pulse Widths

    Bruce can this be applied to pic18f452?

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts