Using hardware capture


Results 1 to 15 of 15

Threaded View

  1. #10
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Thanks. I converted it to SF.
    Code:
    Device = 18F4620       
    Clock = 4
    
    Dim Capture As PIR1.2   ' CCP1 capture flag
    Dim CapIE As PIE1.2     ' CCP1 interrupt enable bit
    Dim CapPriEn As IPR1.2  ' priority enable bit for CCP1 interrupt
    Dim PriEnable As RCON.7 ' set to enable priority levels on interrupts
    
    Dim T1 As Word          ' 1st capture value
    Dim PW As Word          ' 2nd capture value & ultimately final pulse width
    Dim CF As Boolean       ' indicates when last capture is ready
    Dim CCP1 As Word Absolute $fbe   ' Location of CCPR1H:CCPR1L
    
    Interrupt IntHigh()
       if CCP1CON.0=1 then  ' Capture from rising edge?
          t1 = ccp1         ' Move word to T1     
       else
          pw = ccp1         ' Move word to PW
          cf = true         ' Indicate last capture
       end if
       CCP1CON.0 = not CCP1CON.0  ' toggle between rising/falling edge captures
       capture = 0          ' Clear capture interrupt flag bit
    End Interrupt
    
    Sub Init()
       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
       PriEnable = 1        ' enable priority levels on interrupts
       Capture = 0          ' clear capture flag bit
       CapPriEn = 1         ' set CCP1 int to high priority
       CapIE = 1            ' enable the CCP1 capture interrupt
       INTCON = %11000000   ' global + peripheral ints enabled
       T1CON.0 = 1          ' Turn TMR1 on here
    End Sub
    
    ' Main Program Entry
    Init                    ' Initialization
    TRISC.2 = 1             ' CCP1 input pin (Capture input on 18F4620)
    Enable(IntHigh)         ' Enable Interrupts
    
    While true
       ' do other stuff here as required
       If CF Then           ' figure out & print result only after last capture
          PW = PW-T1        ' High pulse width = PW-T1
          CF = false        ' Clear flag bit
          ' Send Out Data
       EndIf
    Wend
    Last edited by rmteo; - 30th January 2010 at 20:21.

Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 13: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, 16:50
  3. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 02:47
  4. Hardware capture on 16F876
    By MaxiBoost in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 4th April 2007, 20:19
  5. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 08:42

Members who have read this thread : 0

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