ATtiny3217 - TCA0 interrupt frequency problem

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

Hi all,

 

I am trying to toggle an LED in ~1sec intervals. ATtiny3217 is running at 8MHz. TCA0 runs in normal mode and uses a prescaler of 256 -> Period is set to 31250=8000000/256.

 

#define USART0_BAUD_RATE(BAUD_RATE) ( (float) (8000000 * 64 / (16 * (float) BAUD_RATE)) + 0.5)

void setup() {

  CPU_SREG = 0 << CPU_I_bp;                         /* Disable interrupts */
  
  /*
   *  INITIALIZE SYSTEM CLOCK
   *  
   */
  _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm);
  while ( CLKCTRL_MCLKSTATUS & CLKCTRL_SOSC_bm ) {} /* Wait for system oscillator change to finish */
  
  /*  
   *  INITIALIZE TIMER/COUNTER TYPE A 0
   *  
   */
  TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;  /* Waveform Generation Mode: Normal */
  TCA0.SINGLE.CTRLESET = TCA_SINGLE_DIR_UP_gc;      /* Counter Direction: incrementing */
  TCA0.SINGLE.EVCTRL = 0 << TCA_SINGLE_CNTEI_bp;    /* Enable Count on Event Input: disabled */
  TCA0.SINGLE.INTCTRL = 1 << TCA_SINGLE_OVF_bp;     /* Timer Overflow/Underflow Interrupt enable: enabled */
  TCA0.SINGLE.PER = 31250;                          /* Set period */
  TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV256_gc   /* Clock Select: f_CLK_PER/256 */
                    | 1 << TCA_SINGLE_ENABLE_bp;    /* Enable: enable */

  /*
   *  INTIALIZE I/O
   *  
   */
  PORTC.DIRSET = 1 << PIN3_bp;                      /* Data Direction Set: output */
  PORTC.OUTCLR = 1 << PIN3_bp;                      /* Output Toggle */
  
  CPU_SREG = 1 << CPU_I_bp;                         /* Enable interrupts */

}

ISR(TCA0_OVF_vect) {

  PORTC.OUTTGL = 1 << PIN3_bp;                      /* Output Toggle */
  TCA0.SINGLE.INTFLAGS = 1 << TCA_SINGLE_OVF_bp;    /* Clear overflow interrupt flag */
}

void loop() {

}

 

Problem: LED doesn't toggle on the second, but shines continuously instead. (toggling frequency too high to be perceptible with human eye? but why?)

This topic has a solution.
Last Edited: Fri. May 20, 2022 - 01:52 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As that is Arduino code have you asked which timers Arduino is already using for its own purposes (ie millis() etc) ?

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

clawson wrote:

As that is Arduino code have you asked which timers Arduino is already using for its own purposes (ie millis() etc) ?

 

Thank you for your reply!

 

The problem indeed was solved when replacing the loop with

int main() {

  setup();

  while (1) {}

}

 

and therefore disabling Arduino timers!

Last Edited: Fri. May 20, 2022 - 01:55 PM