Using hardware capture


Results 1 to 15 of 15

Threaded View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405

    Default Using hardware capture

    Got a PIC with hardware capture, and looking for a way to record high & low pulse widths
    with better resolution than PULSIN?

    Here's how .. with an example from the Microchip CCP & ECCP Tips & Tricks modified to
    work with PBP.
    Code:
    ' Measuring signal pulse widths with capture module
    
    ' Procedure for high-going pulse:
    ' 1. Configure CCP to capture on rising edge
    ' 2. Setup Timer1 so it will not overflow during max pulse width time
    ' 3. Enable ccp capture
    ' 4. Once capture flag bit is set, save captured value as T1
    ' 5. Reconfigure CCP to capture on falling edge
    ' 6. On 2nd capture, save 2nd value as PW
    ' 7. Subtract T1 from PW for the pulse width value
    
        DEFINE OSC 4
    
        Symbol    Capture = PIR1.2 ' CCP1 capture flag
        T1        VAR WORD         ' 1st capture value
        PW        VAR WORD         ' 2nd capture value & ultimately final pulse width
    
        TRISC.2 = 1          ' CCP1 input pin (Capture input on 18F242)
        INTCON = 0           ' Interrupts off
        
    ReLoad:
        CCP1CON = %00000101  ' Capture mode, capture on rising edge
        T1CON = 0            ' TMR1 prescale=1, clock=Fosc/4, TMR1=off
        TMR1H = 0            ' Clear high byte of TMR1 counter
        TMR1L = 0            ' Clear low byte
        T1CON.0 = 1          ' Turn TMR1 on here
    
        Capture = 0         ' Clear capture int flag bit
        While !Capture      ' Wait here until capture on rising edge
        Wend
        
        ' Rising edge detected / stuff 'captured' Timer1 value in T1
        T1.HighByte = CCPR1H
        T1.LowByte = CCPR1L
        
        CCP1CON.0 = 0       ' Configure capture for falling edge now
        Capture = 0         ' Clear capture interrupt flag bit
        
        While !Capture      ' Wait here until capture on falling edge
        Wend
        
        ' Falling edge detected / stuff 'captured' Timer1 value in PW
        PW.HighByte = CCPR1H
        PW.LowByte = CCPR1L
        
        PW = PW-T1           ' High pulse width = PW-T1
    
        HSEROUT [DEC PW,"uS High",13,10]	' Output to RS232 display
        GOTO ReLoad
        
        END
    If you need to measure the width of a low-going pulse , just change it to capture
    the falling edge first with CCP1CON = %00000100, and rising edge next with
    CCP1CON.0 = 1.

    Real simple ... real effective ... and spot-on timing. Even works on tiny little PICs
    like the 12F683 12F615, etc...;o)
    Last edited by Bruce; - 27th January 2010 at 02:13.
    Regards,

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

Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 12:12
  2. Using CCP1 and CCP2 to measure instant fuel consumption
    By srspinho in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 20th September 2008, 15:50
  3. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 01:47
  4. Hardware capture on 16F876
    By MaxiBoost in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 4th April 2007, 19:19
  5. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 07:42

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