Whats wrong with this code, ATTINY 1614 periodic interrupt

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


 

 

I am trying to get the periodic interrupt working to toggle an IO pin on TINY 1614 Timer B. 

The timer is not getting a clock, as when I read the TCB1_CNT register it is always at zero.

Can someone please suggest the problem, im going in circles in the data sheet! 

 

Code:

Fuses as follows:

 

Any takers on this?

 

Cheers :)

 

 

This topic has a solution.
Last Edited: Thu. Jan 16, 2020 - 06:54 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have not enabled interrupts.
And since the interrupt flag is not automatically released, you need to clear it yourself.

 

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

#define PIN_TOGGLE  PORTB_OUT ^= (1<<0)

int main(void)
{
    PORTB_DIR = 0b00000001;

    TCB1.INTCTRL = TCB_CAPT_bm; //////////////// add
    TCB1_CCMP = 0xFFFE;
    TCB1_CTRLA = 1;

    SREG = 0b10000000;
    while (1);
}

ISR(TCB1_INT_vect)
{
    PIN_TOGGLE;
    TCB1.INTFLAGS = TCB_CAPT_bm; //////////////// add
}

 

 

It doesn't matter, but I would write:

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

#define PIN_TOGGLE  PORTB.OUTTGL = PIN0_bm

int main(void){
    PORTB.DIRSET = PIN0_bm;

    TCB1.INTCTRL = TCB_CAPT_bm; //////////////// add
    TCB1.CCMP = 0xFFFE;
    TCB1.CTRLA = TCB_ENABLE_bm;

    sei();
    while (1);
}

ISR(TCB1_INT_vect){
    PIN_TOGGLE;
    TCB1.INTFLAGS = TCB_CAPT_bm; //////////////// add
}

 

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

Thanks for your reply.

Noted regarding your version of code, I will learn to use this way. Is the best way to set multiple bits still to use | between definitions?

 

Say I wanted to set bits 6 (ASYNC) and bit 1 (CNTMODE[2:0]) in TCB1.CTRLB, would you suggest using TCB1.CTRLB=TCB_ASYNC_bm | #define TCB_CNTMODE1_bm; ?

 

if then I wanted to clear the ASYNC bit what is your suggestion?

 

 

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

I would write like this.

    // setting 0b01000010
    TCB1.CTRLB = TCB_ASYNC_bm | TCB_CNTMODE_CAPT_gc;

    // ASYN off
    TCB1.CTRLB &= ~TCB_ASYNC_bm;

 

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

ClaytonLL wrote:
if then I wanted to clear the ASYNC bit what is your suggestion?
kabasan wrote:

TCB1.CTRLB &= ~TCB_ASYNC_bm;

perhaps you know this already but if &=~ is a new construct to you then you may want to read the "Bit Manipulation 101" article found in the Tutorial Forum here.