HCS12 hardware interrupts

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HCS12 hardware interrupts

Jump to solution
5,523 Views
elee40
Contributor I
I am looking to use hardware interrupts, but have found no examples or code.  I simply want an interrupt to occur when a button is pressed.  I am new to the HCS12 so please bear with me.  Any comments, approaches or links would be greatly appreciated.

         Thanks in advance,
               Erik
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,394 Views
Lundin
Senior Contributor IV
Generally, you don't want your interrupts to do much except setting a flag in a global variable and then exit and let main() handle what needs to be done. It is very important to set all such global variables to "volatile", otherwise the compiler might optimize them away, because it believes that the variable is only used in main(). You will get strange, random errors which are very hard to track down.

For cases where you need to execute things directly from the interrupt, a good advise is to disable the specific interrupt temporary the first thing you do, then clear the "i" bit in the CCR to allow other interrupts to occur. When you have finished your task, enable the interrupt source again.

And of course, in every isr you should clear the interrupt source in the corresponding flag register before you leave the isr.

In case of debouncing, you probably just want to save a timer value, then compare it with the value you get next time you enter the isr. The HCS12 timer port is very suitable for this, since it allows a lot of flexibility in the hardware.

View solution in original post

0 Kudos
Reply
3 Replies
1,394 Views
mke_et
Contributor IV
Interrupts are easy to use, just keep a few key things in mind.

ANYTHING you touch has to be preserved. Always keep that in mind. The other side of that is depend on NOTHING to be set up when your interrupt happens.

First thing I would do is write a subroutine you can call from somewhere in your program. Have that subroutine do what you want the interrupt to do. Call the subroutine, get it debugged. Once that's done and working, then you can turn your subroutine into an ISR call, but I wouldn't do that. I would just write the ISR so that it calls your subroutine the same way as if it were called from the main code.

When you do anything with something like a switch, make sure you consider 'hardware' and how it really acts. Keybounce. You interrupts may be generated by the thousands. Have your ISR in early development turn off the interrupt so it's a 'one shot'. Then once it's working, then figure out how to turn it back on safely if bounce is an issue.

If you're just doing something like turning an LED on while a button is pressed, it's not a big issue, but if you're using your interrupt to start a complex forground task that is dispatched, then set things up so the ISR disables further interrupts, and the COMPLETION of your foreground task is where you turn the interrupt back on at the earliest. If you have a timer tick running, you may even want to set a flag with a timestamp and then have the timer tick turn it back on after a delay.
1,395 Views
Lundin
Senior Contributor IV
Generally, you don't want your interrupts to do much except setting a flag in a global variable and then exit and let main() handle what needs to be done. It is very important to set all such global variables to "volatile", otherwise the compiler might optimize them away, because it believes that the variable is only used in main(). You will get strange, random errors which are very hard to track down.

For cases where you need to execute things directly from the interrupt, a good advise is to disable the specific interrupt temporary the first thing you do, then clear the "i" bit in the CCR to allow other interrupts to occur. When you have finished your task, enable the interrupt source again.

And of course, in every isr you should clear the interrupt source in the corresponding flag register before you leave the isr.

In case of debouncing, you probably just want to save a timer value, then compare it with the value you get next time you enter the isr. The HCS12 timer port is very suitable for this, since it allows a lot of flexibility in the hardware.
0 Kudos
Reply
1,394 Views
Lundin
Senior Contributor IV
Either check technical note tn101.pdf that came with the compiler, or search for "defining interrupt" in the Codewarrior help.
0 Kudos
Reply