#define F_CPU 3333333UL #include #include #include #define LED1 4 // port D, connected to WO4 #define LED2 5 // port D, connected to WO5 /* In this exercise, you will use PWM to control the 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 split (not single) mode. We use split to get access to WO4 and WO5, see Figure 19-13. It is highly recommended that you read chapter 19.3.3.4 and 19.3.3.6 in the datasheet. There you will find a sub-chapter on the single-slope PWM we will be using. */ //First, enable the timer //Set the mode of the timer to split slope PWM //We have to override the normal pin opperation so the PWM is pushed directly to the pin //Hint: WO4 and WO5 are connected to leds. We need to override them to get the PWM out. /*Timer A is a 16 bit timer. When we use split mode, this will be split into two 8 bit timers. This will give us a frequency of 25Hz/2, we can see it flicker. By lowering the period, (PER) we get higher frequency at the cost of lower resolution. (Since in split mode, we must use HPER or LPER.) */ //Set the period to 0xff bit. (What frequency does this give?) //We can now control the PWM duty cycle by simply writing values to the CMP0 and CMP1 registers. TCA0.SPLIT.HCMP1 = 0x00; //Max brightness (Leds are inverted) TCA0.SPLIT.HCMP2 = 0xff; //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 */ } }