main.c 1.73 KB
Newer Older
spookysys's avatar
spookysys committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


#define F_CPU 3333333UL


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

#define LED1 0 // port B
#define LED2 1 // port B
#define LED3 4 // Port B


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.
	*/

	
	/*We will be using timer A that will trigger an overflow interupt.
	  This is a 16 bit timer that can run in 2 modes
spookysys's avatar
spookysys committed
25
		-single mode as 1 16-bit timer
spookysys's avatar
spookysys committed
26
27
28
29
		-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:
spookysys's avatar
spookysys committed
30
	  TCA0.SINGLE.CTRLA addresses the control A register for timer A
spookysys's avatar
spookysys committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	  
	  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.
	*/

	
	//Next we Enable timer interupt for overflow on timer A. 

	
	//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!

	
	
	//To be able to react to the interrupts from the module we have to enable interrupts globally.
	//This is done in the function sei(), whih is located in the already included header file <avr/interrupt.h>
	sei();
	
    while(1){
		//Do other things?
		
		//Remember to fill in the ISR at the bottom of the file, else nothing will happen!
    }
}

ISR(TCA0_OVF_vect){

	//Do something with the led(s), like toggle.
	
spookysys's avatar
spookysys committed
61
	//Clear the interrupt flag.
spookysys's avatar
spookysys committed
62
63
64
	//If we do not clear the flag, we will instantly jump back into the ISR again
	
}