HElp with timed interrupts (mega128)

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I'm trying to get an interrupt to fire ever 50us. The documentation on the mega 128 is a bit wacky. Anyone know where i can get some nice easy to understand code examples?? or any resource that will help?

On how to use the output compare without affecting an output pin.. i want a sort of real time interrupt

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
Do a seach on the website, you will find plenty of examples. What you need to do is,

I suggest use a Timer interupt (the data sheet is in fact IMO quite clear) In the initialize routine for your micro, set a few variables, such as divider from the main clock source, reload value for the timer (you will need to calculate this based on your main clock source), and the interupt flags.

Then, make sure you have a jump placed at the approprate interpt vector address, and in the interupt, remember to keep it short, reload your timer and push and pop any variables that are used both in program and in interupt as well as the Status register. Be careful, if you do have variables that are used in both the main program and the interupt.

I would also suggest do this yourself, understand it well as once done it is applied to all micro's in the same way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

RancidWannaRiot wrote:
Hello,

I'm trying to get an interrupt to fire ever 50us. The documentation on the mega 128 is a bit wacky. Anyone know where i can get some nice easy to understand code examples?? or any resource that will help?

On how to use the output compare without affecting an output pin.. i want a sort of real time interrupt

I agree that it is hard to start from square 1 and read/understand the documentation. I has a similar job (needed to interrupt every 1000us (1ms), and I had good luck. Look at some of the documents here and there on using timers. Then attack it on your own. The simulator for the 128 is buggy, but the 103 simulator is OK.

50us is about 800 ticks at 16mhz clock with a prescaler of ONE.

Set the timer to CTC, Remember that the interrupt fires AFTER the CTC is equal (to 800), so it happens on the 801'st clock cycle. I found that I needed to set my compare value to one less than the exact value as a result (in my case 15999 instead of 16000).

This will trigger a timer interrupt. You have to enable the appropriate itimers and interrupts. Set the appropriate prescaler, set the compare value, turn the timer on, and I forget what else. It was reasonably easy even for a raw beginner like me. I did get confused when the simulator was screwing up as I single stepped thru. Atmel was notified. They reproduced the bug, but indicated no clear timetable to make a fix.

All you really need to know IS in the documentation. Then look at the freaks stuff for timer usage.

-Tony