I am going to try and not be so wordy in this post since there is a lot to cover on this topic. I want to do it all at once too; I would rather you have fun code to play with than me do one part of just theory. At the end of this post you will have a PWM library you can use in your projects. So here it goes, let me know what you think.
A clock in embedded electronics is what controls how fast the processor ticks. The MSP430 has multiple clocks which can used for the peripherals and the CPU. MCLK is the master clock for the CPU. SMCLK is the submain clock. Both of these can be selected for use in the peripherals such as an ADC or Timer. I won't discuss ACLK here.
Why so many clocks? Power efficiency. Different clocks are turned off in different low power modes; for example LPM0 disables MCLK and the CPU but leaves SMCLK running so the peripherals hooked up to it can continue to run. These clocks can be generated from a number of different sources and can also be divided down from the clock input. For more information on this see the User Guide.
So. What is all this talk about a silly little Timer? The Timer keeps track of how many clock cycles pass without having to write specific code to keep track of time. This can be useful not only for low power modes, but also for time sensitive projects. To keep it simple and quick, a timer needs to be initialized and enabled. It will then proceed to count as the clock ticks to a predefined value and then start over. You can set the Timer to generate events at multiple times along the way to its end value; these events could be an interrupt when it hits a certain number of clock ticks, or it can toggle, set, or clear a specialized pin. Having the Timer change a pin without calling any interrupts or any specialized code is the best way to create a simple PWM.
What is PWM? Pulse Width Modulation. As far as we are concered, it is a square wave that has a duty cycle at a cetain frequency. Duty cycle is the percentage of time the wave is high across one period. How is it useful? Motor control, light control. Think of an LED, the lower the duty cycle (the less its turned on over a period of time), the dimmer it is. There are countless numbers of applications for using PWM.
More about the Timer
There are a few things you need to know before we start coding. As I said, the Timer counts clock ticks, in reality it can be that simple, but it does not need to be. For example the Timer can count up to a certain number or count up then down. The different functional modes are as follows (directly out of the User Guide).
1. Up Mode - the Timer repeatedly counts from 0 to the value set in register TACCR0
2. Continuous Mode - the Timer repeatedly counts from 0 to 0xFFFF
3. Up-Down Mode - the Timer repeatedly counts from 0 to TACCR0 and back down to 0
The picture above is a visual representation of Continuous mode (taken from the User Guide)
Personally I like Up Mode the best, so we will be using that to build our PWM library. So you can set up an interrupt for when the timer gets to TACCR0 or TACCR1, or you can toggle one of the timer.
The Test Code
Setting up the Timer
First we set up our PWM pins. The code below sets P1.2 to output and enables the pin to be the timer output bit TA0.1.
P1DIR |= BIT2; // P1.2 to output
P1SEL |= BIT2; // P1.2 to TA0.1
Next we have to set up the CCR0 and CCR1 registers, which determine when events happen and how high the clock will count to.
CCR0 = 1000-1; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCR1 = 250; // CCR1 PWM duty cycle (25%)
Remember, the timer depends on the clock frequency it is running on. So if you have a 1MHz SMCLK and want a PWM frequency output of 100kHz, CCR0 will have to count up to 10 leaving only 9 values for CCR1 to toggle the clock at. This limits the resolution the duty cycle can have. The default DCO (digital controlled oscillator) which is the source of the MCLK and in this case the SMCLK is approximately 1.1MHz, making the PWM frequency about 1.1kHz with a duty cycle of 25%.
Setting CCTL1 = OUTMOD_7 does two things, one of which might not be so apparent. OUTMOD_7 is the Reset/Set option,which means "The output is reset when the timer counts to the TACCRx value. It is set when the timer counts to the TACCR0 value" (From User Guide). This means that when the timer hits CCR0 it starts counting over AND sets the PWM output to 1; this also means that when the timer hits CCR1 it will set the PWM output to 0. See user guide for some nice pictures and explinations.
For this PWM program we will be using SMCLK and Up Mode for the timer. Now we have to set which clock and which mode the Timer is going to use. The following line of code sets the clock and the mode of operation.
TACTL = TASSEL_2 + MC_1; // Chooses SMCLK, and Up Mode.
In this register you can also set the interrupts to be triggered but we will not be doing that here since the PWM pin will be toggled without needing an interrupt.
That is it for the code, hopefully I explained it all throuroughly and efficiently. If you have any question or don't like how I'm writing this post, please let me know. I'm trying less opinion and more fact.
Hooking Up the Hardware
In order to visually see this in action, which I'm guessing a lot of you do, you can use either a Logic Analyzer, or ... an LED. So lets hook up the LED. Since we are limited as to which pins we can hook up to the timer using this method (we can't hook up P1.0), we will need to hook up P1.2 to one of the LEDs on the LaunchPad. I'm hoping that you have installed headers (either male or female) onto the LaunchPad already. Take out the jumper which is above one of the LEDs and then hook the LED to the pin P1.2 on the board. If you are having trouble with this, write a comment, I will help you.
Run the program, then modify away. As you change the duty cycle the LED's brightness will change. The closer CCR1 is set to CCR0, the brighter the LED will be, the farther away the darker it will be. Congratulations, you have tackled PWM and have a basic understanding of the Timer.
In a future post I will be providing libraries for some of the peripherals which you can simply include in your project; this is so you will not have to worry about all the details when using a peripheral. I could just provide those but I think you should understand how it all works before you use it, otherwise you would be programming in C# on a desktop computer. Libraries like I will provide were invaluable to me many years ago when I started with microcomputers.
Hope that was concise and made sense. Give me some feedback if you liked the style of this one better.
Warning: I am attempting to use SyntaxHighlighter for the first time with this post (Thank you for the tip Zunayed!). Please be patient if it takes me a few revisions to get the code displayed correctly (or even at all). Thanks for your patience.
Edit: I just corrected the commenting error in my code which incorrectly said ACLK. Also, below I added an image from my Logic Analyzer verifying the waveform so you guys can see what it looks like. I have P1.0 and P1.2 tied together.