Search code examples
armcortex-m

Arm Cortex M3 - Interrupt


I am relatively new to programming on a microcontroller in assembly, so I have quite a basic question. I am using the Arm Cortex M3.

What is the big advantage of using an interrupt? (reduction of power or being able to program faster code?)

I would be very thankful for an example piece of code using an interrupt in assembly.

if there is anything else I should know about using interrupts, I would be glad if you share your knowledge!

thanks a lot


Solution

  • An interrupt is just what it is called. So for example you could stand by your front door just in case someone comes by. Or you could look out the window every minute or two in case someone comes by. Or you could have a doorbell and only go to the door if the doorbell rings, based on priority. So if you are watching tv, you may decide that you can pause the program or miss some of it to go to the door and see who is there. You might be in the shower or otherwise occupied in the bathroom and may choose that that takes priority over the door assuming you dont already know who is coming...Or maybe you are out front doing yard work and if someone comes by you are already there, also an interrupt but also similar to polling.

    So you can poll, have code that constantly or every so many units of time checks the peripheral to see if something happened. Or you can setup an interrupt and then if something happens you get interrupted and deal with it in whatever way (save a flag to say you had one then deal with it later, or deal with it right then or somewhere in the middle).

    There is no right answer, sometimes polling is fine or sometimes better. Sometimes polling is bad or consumes too much time or is not fast enough to poll for that and do other foreground tasks. Likewise the interrupt may be a good idea or bad, depends. You should develop a basic understanding of each, start with polling, and then design in the solution based on the problem being solved. Just setting up your first interrupt can be quite difficult, they are in general non-trivial, often, where possible, best to start with polling anyway, understand how to enable, and see the interrupt in a polled manner (not all hardware lets you do that but often you can poll but have the last enable disabled to not actually interrupt) then learn how to service and clear the interrupt, then you have to make sure your service routine is fast enough to not mess up other things on the foreground task, also it may share resources like registers, so make sure that you preserve the state of the foreground task if or as required by the processors design. (the cortex-m3 takes care of preserving the state of the registers, but in general you should understand this), then pull the trigger on enabling the interrupt. Getting an interrupt service routine to work is not easy even for experienced folks, you may get to a point that 9 times out of 10 it works first time but then you move to a new platform or chip or whatever and you have to debug it when it doesnt work and that is the real problem, esp with embedded microcontrollers where the visibility into what is going on is so limited.