PDA

View Full Version : Capture loop exit



spcw1234
- 10th February 2012, 16:07
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?



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.

HenrikOlsson
- 10th February 2012, 17:21
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.

spcw1234
- 10th February 2012, 20:29
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.



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