TIMER-LF.c 1.7 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>

15
#define LED0 5 // port D
BuildTools's avatar
BuildTools committed
16

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


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

25
	PORTF.DIR = (1 << LED0);
Petter Breedveld's avatar
Petter Breedveld committed
26
	
spookysys's avatar
spookysys committed
27
28
29
30
31
32
33
34
35
36
37
38
	/*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.
	*/
39
	TCA0.SINGLE.CTRLA |= (TCA_SINGLE_CLKSEL_DIV256_gc) | (TCA_SINGLE_ENABLE_bm);
Petter Breedveld's avatar
Petter Breedveld committed
40
	
Petter Breedveld's avatar
Petter Breedveld committed
41

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

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