How to make two compare channel interrupts on TAC0?

Hello,

I'm trying to use Nano Every to generate two TCA0 based interrupts by using two compare channels. This is supposed to be a simple task and I've read though ATmega4808 DS multiple times, especially the TCA0 chapter, and manipulated almost all the register configurations, but still could get both compared channel ints working at the same time. Below is the my full test code:

#include <avr/io.h>
#include <avr/interrupt.h>

volatile bool flag = false;

void setup() {  
    PORTA.DIR |= PIN0_bm; /* set PORT PA0 (pin20.D2) as output */
    PORTA.OUT |= PIN0_bm; /* Set initial level of PA0 */
    PORTA.DIR |= PIN1_bm; /* set PORT PA1 (pin25.D7) as output */
    PORTA.OUT |= PIN1_bm; /* Set initial level of PA0 */
  
    Serial.begin(9600);
    unsigned int per_value;
    cli();
    
    per_value = 0xF423;             // set period value for 2Hz freq
    //TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_FRQ_gc;
    TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV64_gc;
    TCA0.SINGLE.INTCTRL = TCA_SINGLE_CMP1_bm | TCA_SINGLE_CMP0_bm;   
    TCA0.SINGLE.CNT = 0;
    TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);
    TCA0.SINGLE.PER = per_value;   
    TCA0.SINGLE.CMP1 = 0x7FFF;     // set some arbitrary number for comp channel 1
    TCA0.SINGLE.CMP0 = 0x3FFF;     // set some arbitrary number for comp channel 0
    TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm;  

    Serial.print("TCA0.SINGLE.INTCTRL= ");
    Serial.println(TCA0.SINGLE.INTCTRL);
    Serial.print("TCA0.SINGLE.CTRLA= ");
    Serial.println(TCA0.SINGLE.CTRLA);  
    Serial.print("TCA0.SINGLE.CTRLB= ");
    Serial.println(TCA0.SINGLE.CTRLB);  
    Serial.print("TCA0.SINGLE.EVCTRL= ");
    Serial.println(TCA0.SINGLE.EVCTRL);  
    Serial.print("TCA0.SINGLE.PER= ");
    Serial.println(TCA0.SINGLE.PER);  
    Serial.print("TCA0.SINGLE.CMP1= ");
    Serial.println(TCA0.SINGLE.CMP1);   
    Serial.print("TCA0.SINGLE.CMP0= ");
    Serial.println(TCA0.SINGLE.CMP0);   

    sei();
}

void loop() {
}

ISR(TCA0_CMP1_vect) {
    flag = true;
    TCA0.SINGLE.INTFLAGS |= TCA_SINGLE_CMP1_bm;                 // Clears the interrupt flag. Rather confusingly,
    //TCA0.SINGLE.CNT = 0;
    PORTA.OUTTGL = PIN1_bm;   /* Toggle PIN 1 of PORT A (PA1=D7) for debugging */
}

ISR(TCA0_CMP0_vect) {
    TCA0.SINGLE.INTFLAGS |= TCA_SINGLE_CMP0_bm;                 // Clears the interrupt flag. Rather confusingly,
    //TCA0.SINGLE.CNT = 0;
    PORTA.OUTTGL = PIN0_bm;   /* Toggle PIN 0 of PORT A (PA0=D2) for debugging */    
}

A few notes to quickly explain my test codes:

  1. Using TCA0 normal mode, keep default DIV64 prescale (so not to mess delay or millis functions);
  2. Enable both CMP0 and CMP1 channels and set different (arbitrary) CMP values;
  3. I read some example codes suggesting use 'TCA0.SINGLE.CNT = 0;' to ensure correct counting. And even used in ISR to clear interrupt, that's why that line of code currently commented out in both ISRs.

My problem is, while running above codes, and monitor both D2 and D7 test pin outputs with digital scope, I can see both channels are outputting 2Hz waveform, as if both interrupts are generated by PER value (TOP value here) overflow. I'm supposed to expect see two waveforms with different frequencies. But that's not what I observed. Please see the waveform snapshot below.

I've tried everything I could, but just couldn't figure out how to make both interrupts working as expected. Is it possible to generate two ints with different compare values on two channels? or Nano every only support one channel at one time?

Please help take a look and let me know where I did wrong. Any help would be greatly appreciated!

Terry

It seems that TCA can only be used as one interrupt mode out of 4 (Comp channel 0/1/2 and OVF) modes at one time, not possible to do any combinations. Is this correct? can some expert please confirm and give some rational explanation?

Nano every is a really good addition and step-up on nano line, but I feel like it's lacking proper support and full of bugs. Am I the only one?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.