PDA

View Full Version : Avoiding getting stuck in a loop



AndrewC
- 30th July 2008, 22:27
I'm writing some code for a photography application which looks for i/p A, then waits for i/p B to change state. I/P B should change within say 500msec max. My naive approach would be to just to stick the program in a FOR count = 1 to xxxx loop, check I/P B inside the loop and if it has changed jump out of the loop with a GOTO statement. Something is bugging me about this approach and it seems that there might be a better way, perhaps using an interrupt ? Any comments ? I've never programmed with interrupts before.

Also, is there an easy way of working out how many clock cycles a particular set of PBP instructions is going to take so I can calculate what to set the count to.

Thanks, Andrew

Jerson
- 31st July 2008, 05:44
This may not find favour with the experts, but you could try this code. No interrupts :)


Counter var byte

for Counter = 1 to 500 ' 500 mS time to find inputb
pauseus 1000 ' pause a millisecond. You may need to trim this value
if InputB then ProcessB
next
' ..... do code following 500mS timeout of not getting InputB
return ' to wherever

ProcessB:
' do whatever you want here. You could also tell after how long InputB has come by reading the value of the Counter which counts in 1mS increments
return


The grain here is 1mS between samples of the inputB which I guess should be overkill for a photography application if InputB is a manual push button

mackrackit
- 31st July 2008, 06:11
This may not find favour with the experts, but you could try this code. No interrupts :)

:eek: No interrupts!

AndrewC
- 31st July 2008, 11:41
Thanks Jerson, that is pretty much what I was proposing. This is actually for the flash sync part of a laser trigger for capturing flying insects. To reduce the granularity I coded it without the PAUSEUS. Jumping out of the FOR ... TO ... loop works fine. I was just wondering what the alternatives might be.

Andrew