Quote:
Originally Posted by Ian G
I tried to make a quick fix to increase the coverage of our IR sensors by quickly adding a second interrupt and third interrupt. I knew the way I did it wasn't right, but it mostly worked enough. I was wondering what I would have to do to properly add more interrupts to the the IR code. I don't like wiring multiple sensors in parallel, because it looks like if the sensors receive signals at different times, their output pulses will interfere.
Here is what I did:
Code:
#ifdef ENABLE_INT_3
if(Port_B_Delta & 0x10) // did external interrupt 3 change state?
{
IR_Sensor_ISR(Port_B & 0x10 ? 1 : 0); // call the interrupt 3 handler (in interrupts.c or encoder.c)
}
#endif
#ifdef ENABLE_INT_4
if(Port_B_Delta & 0x20) // did external interrupt 4 change state?
{
IR_Sensor_ISR(Port_B & 0x20 ? 1 : 0);//Int_4_ISR(Port_B & 0x20 ? 1 : 0); // call the interrupt 4 handler (in interrupts.c or encoder.c)
}
#endif
#ifdef ENABLE_INT_5
if(Port_B_Delta & 0x40) // did external interrupt 5 change state?
{
IR_Sensor_ISR(Port_B & 0x40 ? 1 : 0);//Int_5_ISR(Port_B & 0x40 ? 1 : 0); // call the interrupt 5 handler (in interrupts.c or encoder.c)
}
Thanks in advance!
|
From what I see above, you will still be subject to interference. All three
sensors will generate edges and if 2 or more are active at the same time
the pulse width measurement will be messed up.
One solution is to dedicate timer1 as a free running timer (which is what we
do). We use it to count 100ns clocks. It overflows every 6.5536 ms. The
interrupt for the timer increments a global "tick" which can be used for less
precise timing tasks and to tell whether overflow has occurred, i.e. more
than 6.5ms has passed.
For short interval time measurements (like the IR pulse widths) you can
store the values from the timer (and the tick) and compute the time
interval that has passed.
You need to be sure to use 16 bit read/write mode on the timer
(RD16 control bitof T1CON) and read TMR1L first followed by TMR1H.
The read of TMR1L latches the upper bits of the actual counter into
a temporary buffer so that overflow is not a concern.
You should also disable interrupts globally while reading the two timer bytes
to eliminate a race conditions. You might want to include your reading
of the current global "tick" value within the same critical section.
Hope this helps!