Article: Polling Vs Interrupts
Device drivers written for most microcontroller peripherals used in an Embedded System either use the polling method or the interrupt method in order to check status or completion of a particular action. The article below compare these two methods describing the pros and cons of both.
Polling method simply uses a code section which checks a particular flag or flags, to know the status of operations. Polling method is always part of the main code and not part of an ISR.
Polling is a simpler method to execute, and is recommended for the early stages of design where the working of a peripheral is to be verified. It does not involve any priority. Also the code segment will always execute within a fixed time and in a fixed sequence. It is easy to debug and has no effect on the execution of other sections of code. There are no big memory management issues. There are no issues with stack.
However it does have some disadvantages. There is a greater chance of missing events. This is because polling code executes only in a particular order and does not execute in response to an event. This may cause two events to occur in succession before the polling code executes. The code would in this case only register a single event.Polling also takes up more of the microcontrollers execution time. The same code segment has to be executed over and over again. This is irrespective of whether the event in question has occurred or not.
Polling can then be used either in the early stages of development, or in cases where the peripheral is armed or activated just before polling code executes, and is deactivated immediately after. So the system expects events during very specific times. This is a good place for polling method.
Interrupts use a feature of the controller whereby in response to a specific event the microcontroller stops executing main code and switches to a different section called Interrupt Service Routine.
Due to the nature of Interrupts, there is less likelihood of events being missed. Also ISR code executes only if an event occurs and so there is less burden on the microcontroller.Another feature of Interrupts is priority where the response of the system to simultaneously occurring events can be controlled. Nested Interrupts allow the system to respond even in cases where an event occurs while a previous one is being responded to. Polling may miss such occurances.
There are however some disadvantages. Interrupts are more difficult to setup and cause many complexities. Since main code can be interrupted at any time, critical sections need to be protected. Interrupts delay the execution time of main code and it is not possible to calculate the execution time of any section of main code. This is because the sections can be delayed by Interrupts. There are also many stack and memory management issues related to Interrupts. Data loss and data corruption are quite possible due to Interrupts.
Interrupts need to be used where the events occurring are truly random and unpredictable. They are needed when the response to such events has to be immediate, or within a fixed amount of time.
Both Polling and Interrupts have their place and usefulness in Production code. Using the right method at the right place reduces code complexity and increases reliability.