TIMER-LF.c 1.81 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
/*
2
 * TIMER-LF.c
Petter Breedveld's avatar
Petter Breedveld committed
3
4
5
6
7
8
9
10
11
 *
 * Created: 29.01.2017 23:43:39
 * Author : Petter
 */ 

#define F_CPU 3333333UL


#include <avr/io.h>
Petter Breedveld's avatar
Petter Breedveld committed
12
#include <util/delay.h>
Petter Breedveld's avatar
Petter Breedveld committed
13
14
#include <avr/interrupt.h>

BuildTools's avatar
BuildTools committed
15
16
17
#define LED1 4 // port D
#define LED2 5 // port D

Petter Breedveld's avatar
Petter Breedveld committed
18
19
20
21


int main(void)
{
Petter Breedveld's avatar
Petter Breedveld committed
22
23
24
25
	/*	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.
	*/

BuildTools's avatar
BuildTools committed
26
27
	PORTD.DIR = (1 << LED1) | (1 << LED2);
	PORTD.OUTTGL = (1 << LED1); //Starting with only 1 LED on	
Petter Breedveld's avatar
Petter Breedveld committed
28
	
spookysys's avatar
spookysys committed
29
30
31
32
33
34
35
36
37
38
39
40
	/*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.
	*/
41
	TCA0.SINGLE.CTRLA |= (TCA_SINGLE_CLKSEL_DIV256_gc) | (TCA_SINGLE_ENABLE_bm);
Petter Breedveld's avatar
Petter Breedveld committed
42
	
Petter Breedveld's avatar
Petter Breedveld committed
43

spookysys's avatar
spookysys committed
44
	//Next we Enable timer interupts on overflow on timer A
45
	TCA0.SINGLE.INTCTRL |= (TCA_SINGLE_OVF_bm);
Petter Breedveld's avatar
Petter Breedveld committed
46
	
spookysys's avatar
spookysys committed
47
48
49
	//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!
Petter Breedveld's avatar
Petter Breedveld committed
50
51
	uint16_t counterTop = 0x4000;
	
52
	TCA0.SINGLE.PER = counterTop;
Petter Breedveld's avatar
Petter Breedveld committed
53
	
Petter Breedveld's avatar
Petter Breedveld committed
54
	//Enable global interrupts
Petter Breedveld's avatar
Petter Breedveld committed
55
56
57
	sei();
	
    while(1){
58
		//Do other things?
Petter Breedveld's avatar
Petter Breedveld committed
59
60
61
62
    }
}

ISR(TCA0_OVF_vect){
spookysys's avatar
spookysys committed
63
	//Do something with the led(s), like toggle.
BuildTools's avatar
BuildTools committed
64
	PORTD.OUTTGL = (1 << LED1) | (1 << LED2);
Petter Breedveld's avatar
Petter Breedveld committed
65
	
spookysys's avatar
spookysys committed
66
67
68
	//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);
Petter Breedveld's avatar
Petter Breedveld committed
69
70
	
}