ARDUINO EVERY - TIMER INTERRUPTS

Hi,

I am trying to find a way how to use a timer interrupt with Arduino Every (ATMEGA 4809). I found some code example, but it doesn't work. I need periodically call a timer function, that's all. Please, could you advice or point me to some resources?
I tried this:

void start_timer(void)
{
// Use Timer/Counter TCB3 using a TOP of 10k and prescaler of /2 to run at 1kHz
TCB3.CCMP = 20000;
TCB3.INTCTRL = (1 << TCB_CAPT_bp); // Enable interrupts
TCB3.INTCTRL |= (1 << TCB_CAPT_bp); // Enable interrupts
TCB3.CTRLA |= TCB_CLKSEL_CLKDIV2_gc | TCB_ENABLE_bm; // Enable timer with prescaler of /2 to start with 10MHz frequency.
}

int main(void)
{
// Enable interrupts
sei();

start_timer();

while (1) {
//check for flags
}
return 0;
}

ISR(TCB3_INT_vect)
{
//set flags
TCB3.INTFLAGS = TCB_CAPT_bm;
}

ISR(TCB3_INT_vect) is called only once and I can't get it called pariodically.

Thank you.

Peter

1 Like

Hi, I found it:

the problem was that I didn't use "volatile" keyword for my global variable accessing from the interrupt routine.
It just works. Thanks.

I can't help but notice that the keyword "static" doesn't appear anywhere in your sketch.

Please remember to use code tags when posting code.

Be aware that the Nano Every runs at 16MHz despite what it says in the specs and on the box. It can run at 20MHz if you modify the boards.txt file.

Thank you. I can't find a right formula to calculate TCB3.CCMP constant (for default prescaler, that (I hope ) should be 2). Any idea? Thank you.

I'm looking / waiting for a C style timer Interrupt function. I don't like such direct register manipulations like the above "TCB3.CCMP ...." This is the way to some incompatibilities.
Is there a timer interrupt function definition for Arduino NANO Every?

An additional info to the Timer Type B at NANO Every:
I did some Experiments with the 4 type B timers in a Sketch with no #include (!). My goal is a simple timer interrupt with 20 ms period.
TCB0, TCB1 and TCB2 will work, TCB3 produces an error:
wiring.c.o (symbol from plugin): In function clockCyclesPerMicrosecondComp': (.text+0x0): multiple definition of __vector_36'
C:\Users\Rudol\AppData\Local\Temp\arduino_build_342235\sketch\TimerInterrupt.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano Every.

The wiring library seems to be automatically included and uses TCB3 exclusive?

May I ask where did you get documentation about the Nano Every timer interrupts? I really having a hard time finding anything about this topic.

Thanks a lot!

4clci
About documentation ...
in https://content.arduino.cc/assets/Nano-Every_processor-48-pin-Data-Sheet-megaAVR-0-series-DS40002016B.pdf
you can found that series is "0" because in name "mega4809" where 0 means "series".

Then you can found docs for it like http://ww1.microchip.com/downloads/en/DeviceDoc/megaAVR0-series-Family-Data-Sheet-DS40002015B.pdf

But the information is beyond my ability. I hopes it helps.

dear Peter,

The Nano Every has a ATMEGA4809 processor that has different timer counters than the ATmega328P of the Uno, Mega en Nano boards.

I wrote a small library that implements control of the Timer B (TCB) of the Nano Every.
You can find it here:

Copy the EveryTimerB.h/cpp and MegaAvr20MHz.h files to your sketch folder and add this code:

#include "EveryTimerB.h".

void setup() {
TimerB0.initialize();
TimerB0.attachInterrupt(myisr);
TimerB0.setPeriod(1000000);
}

void myisr() {
// do something useful every second
}

1 Like

Can you pls help me: I have error in EveryTimerB.h at

#include "MegaAvr20MHz.h"

Where can I get this file?

Thx!


Rene: "You stupid cow": I found it: it's included in your lib.

Dear Kamrca,

I will update the usage description to include the MegaAvr20MHz.h include file.
As long as you don't change the clockspeed to 20 MHz, this file is not needed.

This is my first github/arduino contribution so I'm excited that it could be of
use for somebody else than me :-).

Did it help you to implement the timer interrupt functionality ?

WR,

Kees