Following on from my other question, Help optimising this C (AVR) code? :), I just realised that I don't really know the mechanics of how to implement an ISR in assembly within my C program. I googled, but I didn't find anything too useful.
Basically I want to set up everything else in my C project (including the registers to get the interrupt to fire), but I want the interrupt to execute some assembly code that I've written.
I'm using AVR studio 6 on Windows if that helps. I've been told I need to use .s
files but apart from that I'm not really sure what to do. Is the basic structure just the following?
#include <avr\io.h>
TIMER1_COMPA_vect:
; assembly instructions here
If I want the assembly to run when the TIMER1_COMPA_vect interrupt is 开发者_开发百科fired (this is the name of the ISR in C). Or am I totally off track? What is a basic template of what I need to do? Or if it's a dumb question, is there a link where I can find more information?
The Art of Assembly Language (by Randall Hyde) has two chapters about ISRs (17th and 18th, specifically sections 18.5 and 18.6 might help you). Shortly:
- Go to the IVT (interrupt vector table) and modify it with your ISR segment and offset, saving the old values
- You should do a TSR (terminate and stay resident) program, so that your ISR stays resident into memory even when the user closes the window
- Remember to call the old ISR after you're done with the work (this is called interrupt chaining)
- your ISR should be re-entrant (so that if the interrupt is fired again when your ISR is still running the pc won't explode ;) )
By the way, you can obtain a pdf copy of that great book here
精彩评论