For a project I’m currently working on I need to write a (at least for me) fairly complex firmware. The code I used in the last version of this circuit grew out of a test program and became unmaintainable. So I looked around for some inspiration and found Alan Burlison’s Arduino task manager. It’s a great piece of code, so I wanted to give it a try. One problem remained: his code is for the Arduino, but I use plain C with avr-gcc. So I adapted his code for my use.
While trying to convert his code to C I found one flaw. His implementation of timed tasks doesn’t handle timer roll-overs gracefully. As my circuit is supposed to run for months without reset, that was out of the question. Finally I found a solution by Bombastic Bob on Arduino Playground.
Now it works like a charm. I attached my code, you are free to use it for your projects. The included example is for an ATmega168 with a 12MHz crystal and an LED on PD6. But I see no reason why this code can’t be used with other micro-controllers as well.