TIMER_A: Timer_A PWM

We can use Timer_A to accomplish the task of setting an LED to 40% brightness. We tried to solve this problem using Timer32 in here. For generating PWM using Timer_A, the first step is to set up the Timer_A itself. Since we have a specified period, the Timer_A should be in count-up mode, and considering that an LED is being operated, the Timer_A should be configured in Output-Compare mode. For the output mode, we want the LED to periodically switch from high to low, so either SET_RESET or RESET_SET will suffice. For this example, we will use SET_RESET. Thus, the timer will output 0 (reset) until it reaches the value in the output compare register. Once that value is reached, the timer will output 1 (set) until the counter value is reloaded to its starting value (in which case the output will return to 0).

Following the same example in the Timer32 PWM, our LED PWM period is 1 ms or 3000 cycles.. The on-time is 1200 cycles and the off-time is 1800 cycles. In this case, the load value is the period of our PWM (3000). From the counter value 0 to the output compare value, Timer_A will output low, thus the LED is off. We want the LED to be off for 1800 cycles, so we can make our OC value equal to that time off. Thus, when the counter reaches 1800, Timer_A will then output high (LED is on) until the counter reaches 3000 and resets to 0 (marking the end of the period).