TIMER_A: Pulse-Width Modulation

Pulse-Width Modulation (PWM) is a technique that changes the active-on time of a periodic digital signal. PWM is useful for when we want to control how long a peripheral should be on. One particular application is for dimming LEDs. Usually, when the LED receives a constant, active-high input, the LED is at maximum brightness. However, suppose we wanted to decrease the LED’s brightness. Changing the hardware is one option, but not a very flexible one. Thus, we turn to software for our solution. 

When the LED is at maximum (100%) brightness, it is always on. If we turn the LED off, then it is at its lowest (0%) brightness. What if we developed a code that turned on the LED for one second, turns it off for another second, and then repeats? The human eye probably wouldn’t notice anything special aside from what is to be expected: the LED turns on, then turns off, then turns back on, etc. But what if we toggled the LED’s on-off state every 0.1 seconds? What the human eye would most likely notice is that the LED is constantly flickering. One moment it is on, the other it is off, but this is happening at a very fast rate. What if we made that rate even faster? What ends up happening is that this flickering happens so quickly that the human eye cannot directly observe it. Instead, what the human eye sees is the LED at 50% brightness, even though the LED is really just switching from maximum to zero brightness extremely quickly.


Suppose we wanted the brightness of the LED to be at 75%. If we incorporate our previous solution of making the LED alternate between on and off at a rapid rate, then we would simply want the LED to be on for longer than the time it is off. This is because the more time the LED is periodically on, the closer it gets to always being on (which means maximum or 100% brightness).


In the previous example, when the LED was at 50% brightness, it was on for 50% of the time and off for 50% of the time. Similarly, at 100% brightness, the LED was on for 100% of the time and off for 0% of the time, and the opposite holds true for 0% brightness. Following this pattern, if we want the LED to be at 75% brightness, we would want it to be on 75% of the time and off 25% of the time. In each of these cases, we are modifying the time that the LED is on in order to get our desired brightness. In other words, we are modifying the duty cycle of the signal that goes into the LED. The duty cycle is the time that our signal is at an active-high, expressed as a percentage. So an LED at 75% brightness would need a signal with a duty cycle of 75%.

How can we generate such a signal? We develop software that tells the LED to be on for a certain amount of time, then turned off for a certain amount of time, and then repeat. The LED is constantly going through these cycles of turning on and off at a very quick rate. This rate can be defined by how long in seconds it takes for the LED to go through one cycle of turning on, then turning off. The time it takes for one of these cycles to be completed is called the period. By multiplying the duty cycle with the period, we can figure out how many seconds the LED should be on. For instance, in order to achieve 75% brightness, assuming the period of the LED changing from on and off is 10 milliseconds, the LED would need to be on for 10*0.75 = 7.5 milliseconds (and then off for the remainder 2.5 milliseconds). 

Since controlling the duty cycle of a signal depends on the time elapsed, we need a way to measure the time. To accomplish this, we can utilize timers in our software. For the TI MSP432, we can use the Timer32 modules.