/* * TIMER-LF.c * * Created: 29.01.2017 23:43:39 * Author : Petter */ #define F_CPU 3333333UL #include #include #include #define LED1 4 // port D #define LED2 5 // port D int main(void) { /* In this exercise we will get LEDs to blink at even intervals using interrupts First set up the LEDs like in the previous exercise. You can either copy paste from the previous exercise or start fresh. */ PORTD.DIR = (1 << LED1) | (1 << LED2); PORTD.OUTTGL = (1 << LED1); //Starting with only 1 LED on /*We will be using timer A that will trigger an overflow interupt. This is a 16 bit timer that can run in 2 modes -single mode as 1 16-bit timer -dual/split mode as 2 8-bit timers We will be using single mode in this exercise. Hint because the register names can be hard to understand: TCA0.SINGLE.CTRLA addresses the control A register for timer A First we set the prescaler to clk=clk/256 and enable the timer. This is done by setting the right bits in the control A register. */ TCA0.SINGLE.CTRLA |= (TCA_SINGLE_CLKSEL_DIV256_gc) | (TCA_SINGLE_ENABLE_bm); //Next we Enable timer interupts on overflow on timer A TCA0.SINGLE.INTCTRL |= (TCA_SINGLE_OVF_bm); //Finally we have to set the max value of the timer, the top. //This defines the period of the interrupt (which hints at the register's name.) //Note that this is a 16 bit value! uint16_t counterTop = 0x4000; TCA0.SINGLE.PER = counterTop; //Enable global interrupts sei(); while(1){ //Do other things? } } ISR(TCA0_OVF_vect){ //Do something with the led(s), like toggle. PORTD.OUTTGL = (1 << LED1) | (1 << LED2); //Clear the interrupt flag. //If we do not clear the flag, we will instantly jump back into the ISR again TCA0.SINGLE.INTFLAGS = ( TCA_SINGLE_OVF_bm); }