main.c 1.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
61
62
63
64
65
66

#define F_CPU 3333333UL


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


#define LED1 0 // port B, connected to WO0
#define LED2 1 // port B, connected to WO1
#define LED3 4 // Port B
#define SW1 5 // Port A
#define SW2 6 // Port A
#define SW3 7 // Port A

/*
    In this exercise, you will use PWM to control thwe brightness of LEDs
    Once we have PWM set up, controling the brightness is super easy!
*/

int main(void)
{
	//Set up the leds and buttons. You might want to copy this from previous exercises.
	
	/*We will be using timer 1 in single (not split) mode.
	  It is highly recommended that you read chapter 20.3.3.4 in the datasheet 
	  on timer compare channels.
	  There you will find a sub-capter on the single-slope PWM we will be using.
	*/
	
	//First, enable the timer
	
	
	//Set the mode of the timer to single slope PWM 
	
	
	//We have to override the normal pin opperation so the PWM is pushed directly to the pin
	//Hint: WO0 and WO1 are connected to leds. We need to override them to get the PWM out.
	
	
	
	/*Timer A is a 16 bit timer. This will give us a frequency of 25Hz, we can see it flicker.
	  By lowering the period, (PER) we get higher frequency at the cost of lower resolution.
	*/
	
	//Set the period to 12 bit. (This results in a frequency of ~400Hz.)
	
	
	//We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers.
	TCA0.SINGLE.CMP0 = 0x0000; //Max brightness (Leds are inverted)
	TCA0.SINGLE.CMP1 = 0x0fff; //Min brightness (Leds are inverted)
	
	
    while(1){
		
		/*Have some fun with the leds. Examples:
		  Have them fade between max and min
		  Have them fade in a pattern (Heartbeat?) 
		  Change the brightness based on buttons

		*/
		
    }
}