Electronic – Interrupts and timing applications

embeddedinterruptsmicrochipmicrocontrollerpic

While learning about multiplexing 7 segments using interrupts in PIC, it was found that when this was used in real world timing applications, the frequent ISR causes a step by step cumulative delay in the main loop, which then increases further from the real world time, owing to the delay caused by the main program by ISR subroutine servicing.

I use 20 MHz crystal running PIC with timer0 for refreshing multiplexed displays but the delay caused by ISR is evident in the main loop of the program when a real-time measurement is done by a delay routine etc. Thus when 5 minutes are counted, it adds some seconds depending on the timer0 setting for ISR. Like 5 sec for 5 min and a minute or so for 1 hour etc.

Is there any way to solve this apart from using serial displays ??

Best Answer

Instead of using a main loop delay to compute the time, which gets disrupted by the hardware counter, use the hardware counter itself to provide the timing.

Set the timer0 to interrupt (say) every 1mS. This becomes your system's tick, or heartbeat. Use the interrupt routine to increment the mS counter, and to refresh the displays.

In the main loop, use the mS counter as your 'wall clock', wait until it has advanced 300,000 ticks, and you have waited 5 minutes within a non-cumulative accuracy of 1mS.

This answer is intentionally incomplete, there are some more technical details that may be necessary depending on the length of your time check routine, but it would overload the answer to present a treatise now. Once you start getting into the details, you'll find that getting clean communication between the main loop and ISRs involves a bit of care, declaring variables volatile, taking snapshots, disabling interrupts for critical periods, but it is all documented and it's the way everybody else does it.