#define F_CPU 3333333UL #include #include #include #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 */ } }