Capture loop exit


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158

    Default Capture loop exit

    I am using a capture module to measure RPM with a PIC16F628A. It works very well, but if the motor isn't running there will be no pulse so I am stuck in an endless loop waiting for the pulse. I need to do other things on the pic, so this isn't good. Besides incrementing a value, how else can I add a failsafe to exit the loop?

    Code:
    Trash:
      If Capture = 0 then
        Goto Trash          'stuck in loop if no capture
      endif
    
      T1CON.0=1              'starts timer
      Capture=0               'trash first capture
    
    CaptureLoop:
      If Capture = 0 then
        Goto CaptureLoop
      endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    In the CaptureLoop I can look for a timer1 overflow flag which would work, but the trash loop the timer isn't running, so that wouldn't work.
    Shawn

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Capture loop exit

    Hi Shawn,
    As far as I can see you have two options, software and hardware.
    Use a timer interrupt (or poll the interrupt flag) to abort the wait loop OR use a software "timer" to abort the wait loop. If you, for whatever reason, don't want to increment a variable (software "timer") inside your waitloop then the only option left is hardware. I see no reason why you can't use TMR1 if you want to, just reset it when the trigger comes.

    I've never used the capture module though so I may be missing something.

    /Henrik.

  3. #3
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: Capture loop exit

    Thanks Henrik,
    I was concerned that stopping the timer and restarting it would throw off the measurement, as I am sure it would. I tried it with just clearing the timer registers and tested it, and all seems good. Just checking for a timer overflow which if occurred will exit the capture routine.

    Code:
      TMR1H=0      'Clear 8-bit register                              
      TMR1L=0      'Clear 8-bit register
      T1CON.0=1    'starts timer  
      overflow=0   'Clear overflow bit
      capture=0    'Clear Timer
    Trash:
      If Capture = 0 then
         if overflow=1 then 
           rpm=0
           T1Con.0=0  
           return
           endif    
        Goto Trash          
      endif
      TMR1H=0      'Clear 8-bit register                              
      TMR1L=0      'Clear 8-bit register
      Capture=0    'waste first capture
      Overflow=0   'Clear overflow bit     
    CaptureLoop:
      If Capture = 0 then
         if overflow=1 then 
           rpm=0
           T1Con.0=0  
           return  
           endif  
        Goto CaptureLoop
      endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
      TMR1H = 0
      TMR1L = 0
      Capture = 0
      Overflow = 0
    Shawn

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