Timing 2 events with interrupts

Peter Fender

New Member
I am trying to time 2 events with interrupts with a 20X2.
Think model race cars, or Pinewood Derby (for those in US).

The attached code is simplified to show what I am trying to do.
For this example, pushbuttons or switch closures are on pins 5, 17, 18 to +V, with a 10k to ground,
and an LED in pin 3 with a resistor to ground.
It usually works, but it can miss the second switch inside the interrupt handler, between lines marked [1] and [2].
(the 'OR PushB1=1' and 'OR PushB1=1' is added as a workaround to catch near simultaneous closures in the first half of the handler.)
If the switches are really simultaneous, Lane 1 always wins due to sequential execution of the 2 IF blocks.

And if the second switch is closed between lines marked [2] and [3], the PICAXE locks up.

The above can be tested by connecting an output pin to hint2 (pin 17), and putting pulsout command in various places, then pressing the hint1 switch.

Is there a better way to do this?
Increasing the clock speed to max inside the handler then back to 8MHz before the return reduces the chance of a problem, but does not fix it.
 

Attachments

Buzby

Senior Member
Hi Peter,

I did something similar with a 20X2 a while back, but used a different philosophy.

The 20X2 was configured to use the timer interrupt, set to run at 1000 interrupts per second.

The interrupt handler did the following :
- incremented a variable, in my case I counted to 59999, i.e 59.999 seconds.
- got the state of the pins on one port, as a single byte.
- compared this byte with the previous value.
- if the the bytes were different then saved the mS count and the pin state to a buffer in the scratchpad.

The main code looked to see if the buffer had any entries, and if so used the buffered time and pinstates to work out what had happened and when.

The 20X2 needs to run at 64MHz, but the result is a timing mechanism accurate to 1mS and has no problems with simultaneous events.

The only way to avoid have one lane always winning a draw is to read both lanes simultaneously.

You could do it the way I did, or you could use your interrupts to read the entire port to a variable, then use the bits in that variable. The bits won't have priority, because they were all read at the same time.

Cheers,

Buzby
 
Last edited:

Peter Fender

New Member
Thanks for the ideas.

I will change my routine to just read the timer once, and read the switches as a port to better handle ties.

And I'll try the method of interrupting on the timer, and reading the switches.
I was thinking I needed to approach this from a different direction, and that may work better.

Thanks again.
 
Top