Using a timer with an Atmega8 (and the arduino IDE)

Hey there. At the moment I am programming an atmega8 (with an external 8MHz quarz) for a project of mine. I am using arduino code for this. To be able to use the atmega8 I copied the "Arduino NG or oder w/ ATmega8" from the board.txt and created a new one with 8Mhz:

##############################################################

atmega8.name=Arduino w/ ATmega8 internal @8Mhz

atmega8.upload.protocol=arduino
atmega8.upload.maximum_size=7168
atmega8.upload.speed=19200

atmega8.bootloader.low_fuses=0xE4 
atmega8.bootloader.high_fuses=0xca
atmega8.bootloader.path=atmega8
atmega8.bootloader.file=ATmegaBOOT-prod-firmware-2009-11-07.hex
atmega8.bootloader.unlock_bits=0x3F
atmega8.bootloader.lock_bits=0x0F

atmega8.build.mcu=atmega8
atmega8.build.f_cpu=8000000L
atmega8.build.core=arduino
atmega8.build.variant=standard

My program has some recurring time(ing)critical code. This part of the code should not be late and not be interrupted by the rest of the (not equally time critical) code. Therefore i want to use timers. The ATmega 8 basically has the same timers as the arduino uno (afaik), so I tried to use the Timer1 library. The result: 'TIMSK1' was not declared in this scope. GTCCR and PSRSYNC too. So I manually tried some examples I found online, but all of them use TIMSK1.

How do I get timers to work on my ATmega8? This may be really easy, but I did read alot about timers and all these different registers are a little bit overwhelming ;)

Thanks for your help!

In the 328P datasheet register list are TIMSK0, TIMSK1, TIMSK2.

In the 8 datasheet, there is only TIMSK.

(331 pages)
Perhaps a code change is needed.

Found this in the internet ;)

// enable support for ATMEGA8

if defined(AVR_ATmega8)

define TIMSK1 TIMSK

define GTCCR SFIOR

define PSRSYNC PSR10

endif

If you add this to the timerone class it should work. Will try this out when i can (http://forum.arduino.cc/index.php?topic=244599.0).

The above code works with an example like this:

#include <TimerOne.h>


void setup() {
  Timer1.initialize(500000);
  Timer1.attachInterrupt(test);
  pinMode(10,OUTPUT);
  Timer1.start();
  Timer1.resume();
}


void loop() {
  
}

void test() {
     digitalWrite(10, digitalRead(10) ^ 1);
}

which blinks a led.

I want to use this in a more complex code which drives a led matrix. I have this code which works without real timers (just my own class which uses millis() and checks that against the last time it called millis())

void setup() {  
...rowTimer.start();
...}
void loop() {
...
// update row
  if (rowTimer.isReady()) {
    refresh(); // paint the next row (and change columns accordingly)
  }
...
}

I am trying to rewrite this using the “real” timer. This is my new code:

...
void setup() {
Timer1.initialize(500);
Timer1.attachInterrupt(refresh);
... }

void refresh() {
...
  }

}

This just doesn’t work. No leds light up. The refresh function calls some other functions too and uses (global) variables. I tried to declare all variables as volatile.
Any idea why this does not work?

Any idea why this does not work?

Because the code you posted won't even compile.

What do you mean? Sure it wont compile… it’s just part of the code.
Ah and I just saw that part

  Timer1.start();
  Timer1.resume();

thats not needed.

Or do you mean the define… part? That compiles and works.