Thursday, July 22, 2010

Timers and Clocks and PWM! Oh My!

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.


84 comments:

  1. TACTL = TASSEL_2 + MC_1; // ACLK, up mode

    chooses SMCLK as you correctly state in your writting, but in commentary it's wrong.

    Great tutorial, keep up!

    ReplyDelete
  2. Primoz,

    Thanks! Just fixed it. It's hard to keep track of everything sometimes.

    :-)

    ReplyDelete
  3. Thanks NJC! You are REALLY helping us newbies. Arduino is fun and easy but making ANY TI chip do ANYTHING is a real accomplishment! Your writing style is great... don't stop!

    :)
    -HS

    ReplyDelete
  4. You can set this to BIT6 to get the LED2 working using the timer (rather than LED1)

    ReplyDelete
  5. I wanted to share back my code from what I've learnt here:
    http://gist.github.com/492301

    ReplyDelete
  6. @TDU,
    I've never heard of github.com, seems very cool! You might want to provide some more comments though up top in order to help out people who might not understand your code. From what I understand you have a PWM with a changing duty cycle every period? Also, watch out for setting LPM4 and LPM0 at the same time, I don't know how that will effect the uC. Thanks for posting your code :-)

    @HS - :-) Thanks! I won't stop, at least, anytime soon.

    ReplyDelete
  7. @TDU,
    Out of curiosity, I checked to see what happens when you set LPM0 and LPM4 together. If you check the header files, LPM0 would put 0x0010 to the SR. LPM4 puts 0x00F0. LPM0 + LPM4 puts 0x0100, which leaves all the oscillators running and sets the V bit (which I believe is normally used to mark carry over from additions). The best thing to keep in mind is any LPM includes all the previous LPM's-- eg. LPM0 and LPM1 are subsets of LPM2. I ran some example code on my LaunchPad and confirmed that this is indeed the case; just watch the SR register in debug mode and you'll see what's happening.

    ReplyDelete
  8. @NJC
    Hopefully you still check this even though this post is a few months old. First, thank you for the work you do on this blog. As a student I know you are very busy and I appreciate the time you sacrifice for this project.

    As was already pointed out in another comment, LED2 on P1.6 can be used for pwm (P1.6 has compare functionality for Timer A). No need to mess with jumpers. Look at my code for details.

    I also wanted to share some code I wrote to pulse an led.
    https://gist.github.com/750825

    There is one oddity I noticed with my code. Upon initially flashing the launchpad it works as expected. If I then immediately re-flash and run the led stays solid. Re-flash once more and the program works again. Not sure what could be causing this. Perhaps the interrupt flag is someone surviving from the previous flash?

    I had an idea while I was writing that code and I'm wondering if it would be feasible. If the chip contained another timer (some of the msp430s seem to have a Timer_B as well) would it be possible to set Timer_B to up/down mode (assume SMCLK = 1Mhz, Timer_A CCR0 = 1000), set Timer_B divider to 1000 and set Timer_A CCR1 = Timer_B TAR in order to vary the duty cycle up and down? If this would work I believe it would allow the chip to enter a lower power mode. Unfortunately I have now way of testing this as the chip included with the launchpad has only Timer_A but I would like to know if the idea is theoretically sound.

    ReplyDelete
  9. @Daniel, thanks for the post! As for the problem you are having with the LaunchPad, at first glance I have no idea what could be causing the trouble. To be honest, I have had problems with the LaunchPad's USB cable, and just loosing connection with the target chip in general. My theory is that it is the LaunchPad or the cables fault and it is nothing to worry about (it is just frustrating, I keep reminding myself this thing cost $4.30).

    For your idea with the second timer it makes perfect sense and would most definitely work if implemented correctly. Though there are two ways that you could get this functionality with the chip that came with the LaunchPad. The first option is to use the WDT as a second timer, which I haven't ever worked with so I have no pointers there. The second option is to do it mathematically with a single timer. You would still be able to stay in LPM almost all the time, and would have to just add a few extra lines to keep track of your new CCR1 value in the interrupt routine. Feel free to post in 43oh.com/forum if you have any question. I'll be glad to help.

    ReplyDelete
  10. @NJC
    Thank you for the reply. Hope that you have had a great holiday season! It occurred to me that I might be able to use the watchdog, but, looking at the Family User Guide, I see no way to set the watchdog to count up and down so I don't think I could use it as the second timer. Also, by my understanding, only Timer_A could be used to directly affect the pins, so I don't think I could have the watchdog do Timer_A's job and have Timer_A count up and down. Please correct me if I've missed something. As for a mathematical method, the only one I can think of is what I did in the code I posted above. Is there a more efficient mathematical method? Thank you for the 43oh.com recommendation. Looks like a really great forums!

    ReplyDelete
  11. I just found this blog and I'm happy I did. I just started working with the MSP430's, this is a great resource. Keep up the good work.

    ReplyDelete
  12. I am also glad I found this blog. It has really helped me a lot! I was wonder if you ever posted on the libraries for the peripherals you mentioned. I was unable to locate that blog entry. It would really help me out as I'm still learning about micro controller programing.

    Thanks for everything thus far!

    ReplyDelete
  13. It would only take a couple of quick modifications to this code to have a fixed pwm signal that is 2ms on and 20 ms off, right? I am brand spanking new to programming and this chip...but I am trying to learn as I work on a fun project.

    Scott

    ReplyDelete
  14. @Max - Which peripheral library are you looking for?

    @Anonymous - Welcome to microcomputers and programming! To answer your question, yes. All you would need to change is the clock frequency the timer is running at in addition to the compare registers.

    ReplyDelete
  15. NJC, hmmm nothing in particular actually. I was just refering to:
    "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."

    I'm just getting started with micro controllers in general so I'm just browsing different projects and such and learning as I go.

    Thanks
    Max

    ReplyDelete
  16. I have been working on these PWM code examples, but I cannot seem to get the variables to have any change in the light intensity, am I missing something?

    ReplyDelete
  17. @Max - I actually never made a library file for any of those like I planned on doing. The newer posts on the blog discuss functions which I would have included in a library.

    @kenemon - the only way to really see the intensity change is to drastically change the values. Otherwise you would need an oscilloscope or logic analyzer (or even a high end multimeter) to tell you the duty cycle is changing.

    Hope that helps!

    ReplyDelete
  18. I have a very basic question:
    the code above keeps the LED on and off forever, as long as there is power. Say I want to have an LED toggle for 1sec. Remain off for 2 seconds, and then have it toggle again for 1sec and so on, how can I do this with a timer.
    This is actually an IR application that I am trying to get working

    ReplyDelete
  19. Dominic, if I understand your question correctly, you would like to enable and disable the PWM at certain times and only for specific durations?

    This can be done by incrementing a counter every timer interrupt (you have to enable the interrupt though). This allows you to keep track of how long the PWM has been running for. Once this value hits a certain threshold, you can stop the timer counter. Once you would like to start the PWM again, you just have to re-initiate the timer as I did in this example.

    Hope that answers your question!

    ReplyDelete
  20. Do you mean that for ever pulse that is generated, the control would have to enter the interrupt vector?

    ReplyDelete
  21. Thats fantastic. Have you already provided the libraries for the peripherals you have mentioned?

    ReplyDelete
  22. @Dominic - yes it does! Hope that you have solved the problem. :-)

    @Anonymous - I have not provided a library package or any specific library files, but much of the code I have provided in all of my posts can be used as libraries since I have self contained functions for almost everything. Hope that helps!

    ReplyDelete
  23. I quote:
    "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"

    Could you please explain how many timer output bits there are?
    What if I want to drive 2 pins using the same PWM scheme?
    Can I do something like : P1DIR |= BIT2+BIT6?!

    ReplyDelete
  24. Refer to me as alex.

    I've just done this project, and hooked up my oscilloscope, I am not getting any square way at all.

    I'd set my div dial all the way to 5mV time base dial to 5 micro seconds; All I see is faint pulses equally spaced.

    I have the probe in P1.2 and the probe's ground to GND on the board.



    I only detect a change in brightness if CCR0 is set at 1 (low) anything above one. I even tried being objective about and used a salvaged photo transistor to try and detect any change at all.

    Of course all I was getting around .206 mV to .243 mV with a voltmeter.

    ReplyDelete
  25. Alex again; I would like to report that following this person's code I was able to get a square wave.

    http://forum.sparkfun.com/viewtopic.php?t=6021

    ReplyDelete
  26. Hey Guys,

    This code worked well for me. I added some code to change the duty cycle upon pressing the push-button on P1.3.

    But i'm having trouble getting PWM to come out of any other pin. Because the chip (G2231) only has 1 timer and 2 CC registers does this mean i can only ever have 1 PWM with a duty cycle specified by CCR0 and CCR1?

    I want to have 2 PWM signals coming out of the Launchpad that are compliments of each other, (with a little bit of dead time between them).

    I tried initalising the P1.1 and P1.2 at the same time with this:

    P1DIR |= 0x06;
    P1SEL |= 0x06;

    To see if PWM would come out of both pins but it still only comes out of P1.2. I am getting confused with TA0.0 and TA0.1 from the msp430g2231 data sheet.

    Can anyone steer me in the right direction?

    Regards

    Michael Dalton

    ReplyDelete
  27. Hey Guys,

    with some help from the TI E2E community (and a bit of reading) i was able to get two complimented PWM signals with dead-time between them. I ordered a free sample of the MSP430G2452 from the Texas Instruments website and plugged it into the launchpad, if you are going to get one make sure it is the correct package style (DIP).

    If anyone is interested in doing the same thing here is a link to the post on the TI E2E website

    http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/124271.aspx

    Regards

    Michael Dalton

    ReplyDelete
  28. hi all,

    I want to generate RGB pattern using MSP430G2231.
    My query is that is it possible to generate 3 PWM signals using PWM of MSP.

    Please guide

    ReplyDelete
  29. Smoking Baby - sadly, this is not a straight forward task. You would need to manually change the compare register every interrupt and set each pin transition manually in the interrupt. If you have any questions on the implimentation, the forums at http://www.43oh.com would provide quicker answers than I would.

    ReplyDelete
  30. Valeu pela dica porque em português não tem porra nenhuma pra pesquisar

    ReplyDelete
  31. It's very useful for me.
    I have a question,If I want a high frequency PWM signal(ex:10KHz sine wave),what kind of component that used?

    ReplyDelete
  32. @Unknown - I am not sure I understand your question. If you would like to generate a PWM at higher frequencies (ex. 10kHz) you just need to set the timer and compare registers differently in the MSP430.

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. Hi

    I am struggling with modifying code for my project. (almost no experience with C language)

    Basically what I am trying to do is to get two switching signals from two separate outputs of MSP430 G2231 (LaunchPad) using PWM.

    With the script in the following, I am able to detect the switching signal (with 80% duty ratio) in oscilloscope from Pin 1.6.

    How should I modify the code in order to get another inverted switching signal of Pin1.6 from Pin 1.7 (20% duty ratio in this case)?

    Can somebody help me with this?

    Thanks.

    ReplyDelete
  35. @1234 - If you are having trouble with modifying the code and would like to learn how to use C, I would recommend checking out the forums on 43oh.com. There are some very smart and nice people there who can help guide you. It is a bit easier to help in a forum than on a comment thread. :-)

    Hope that helps!

    ReplyDelete
  36. NJC Thank you very much!!!!!... i finally know how to use PWM...great tutorial...keep up!!!
    Regards from Colombia!!!

    ReplyDelete
  37. Stupid question, but WHY can't you hook P1.0 up to see the red LED blink? I did so and my scope showed a really irregular and noisy square wave.

    When I switched the code to use P1.6 (green LED on the launchpad), everything worked great.

    ReplyDelete
  38. Hi,
    I want to check how much time my code is running.
    How can I use the CLOCKS in order to check this.
    Can you write a simple example

    ReplyDelete
  39. Anonymous 1: You should post this issue on 43oh.com/forum. Personally, I have no problem blinking the red LED on my board, I don't know what your problem could be without knowing a few more things.

    Anonymous 2: I will be posting some code in the next week or so on how to use the ACLK as a real-time clock. This should get you going in the right direction. Stay tuned.

    ReplyDelete
    Replies
    1. Hi.
      Thank you very much for this walkthrough. Now i understand it much better. I just wondered about the same thing as Anonymous 1 did (see 3 posts above).
      In your Text , you say:
      "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"
      - But nowe you say "I have no problem blinking the red LED"
      ...so what's it exactly? Why do you say, we can't hook up P1.0?

      Thanks and best regards.

      Delete
    2. jip,

      Thank you for pointing this out to me. I actually made a mistake in my response (see above). You are correct, using this method you cannot blink an LED on P1.0. The green LED will blink just fine using this method.

      So, to correct my previous post:

      Anonymous: If you want to blink the red LED, you cannot use the method discussed in this post. Instead you will have to manually toggle the pin in an interrupt. There are many possible reasons to why you are seeing an odd square wave on P1.0. It all depends on your program.

      Hope this clarifies things! Thanks JIP for pointing out my mistake.

      Delete
  40. How to change the PWM duty cycle to 50%? and what should we do if we're required to vary the PWM duty cycle 10times in one PWM period?

    ReplyDelete
  41. -Fahad,
    use the CCR0 value to change the frequency of the pwm: the higher its value(up to 0FFFFh) the lower the frequency you get.
    your output will be high from TAR=0 till TAR=CCR1. If CCR1 is close to 0 your pwm will quickly change to low. The closer CCR1 is to CCR0 the closer your duty cicle will be to 100%. (assuming output mode 7 reset/set)
    to get a 50% duty cicle use CCR1= (CCR0/2).
    I hope so, heh.

    what do you mean by "vary the PWM duty cycle 10times in one PWM period"?

    ReplyDelete
  42. Hello,
    A quick question: if i put +ID_2 (input divider = 2) into TACTL register, ie TACTL = TASSEL_2 + MC_1 + ID_2; i will get a PWM frequency of about 550Hz ?
    (provided the rest of the code is left as posted in the blog)

    best

    ReplyDelete
  43. hello,
    I am trying to build a fan with MSP430 and I want the fan speed when the temperature increases augment (with PWM) .....pleas can some1 help me on the code . thx..

    ReplyDelete
  44. I want to increase the motor speed when the temperature increases and decrease when the temperature is decreased

    ReplyDelete
  45. Hi,

    I am trying to modify to drive the LED using P1.5, but not successful. The LED does not light up and not signal at P1.5. Is there any error in the code?

    These are the codes:

    #include

    void main(void)
    {

    WDTCTL = WDTPW + WDTHOLD; // Stop WDT
    P1DIR |= BIT5; // P1.5 output
    P1SEL |= BIT5; // P1.5 option select

    CCR0 = 1000-1; // CCR0 PWM Period
    CCTL0 = OUTMOD_7;
    CCR1 = 250; // 25%Duty Cycle

    TACTL = TASSEL_2 + MC_3; // SMCLK, up-downmode

    _BIS_SR(LPM0_bits); // LPM0
    }

    Thanks,

    kea

    ReplyDelete
  46. Thanks for this post. I am 15 years old and from holland and even i understand this tutorial.

    ReplyDelete
  47. I stopped using this board for a while, when I picked it back up I had forgotten almost everything I learned. Your tutorials helped me shake out the cobwebs. Great tutorial.

    ReplyDelete
  48. could someone please help me with this....
    i need a pwm program code to drive dc motor.whenever it is switched on the motor should gradually start and should attain 1 constant speed.when it is switched off it should gradually stop instead of jerk.i m trying to build a electric wheelchair using msp430g2231.

    ReplyDelete
  49. How would i modify this to generate a 15kHz freqeuncy?

    ReplyDelete
    Replies
    1. http://mspsci.blogspot.com/2010/08/tutorial-09-timers.html

      Here is another good tutorial on timers. I believe that it can help you understand how to attain different frequencies.

      Delete
  50. I'm a beginner in MSP430 and I'm using G2553 ic. I would like to know how we can give a particular duty cycle? better you give an equation for it. (pardon me if nybody asks the same here). Thanks in advance..

    ReplyDelete
    Replies
    1. http://mspsci.blogspot.com/2010/08/tutorial-09-timers.html

      Here is another good tutorial on timers. I believe that it can help you understand how get a specific duty cycle.

      Delete
  51. I am a beginnr with the MSP430. I am trying to output a 4MHz PWM. I think I understand your code and my question is how do you use the MCLK instead of the SMCLK? thanks

    ReplyDelete
    Replies
    1. TACTL = TASSEL_2 + MC_1; // SMCLK, up mode

      This is the line that you will need to change. TASSEL_2 is for SMCLK, looking at the Value Line Family User's Guide on page it seems as if you cannot use MCLK as the timer clock. Typically, SMCLK is the same value as MCLK, does this not work for you?

      Delete
  52. Hi! I have to make a digital tachometer using msp430g2553 . It's an university project . Teacher said that i must generate PWM signal from a pin and then i must convert that signal into rotations per minute . After that i should display it on a 7segments display or LCD . Please help me anyone !

    ReplyDelete
    Replies
    1. Please check out the forums on 43oh.com. This is a better place to ask questions about your specific project.

      http://forum.43oh.com/

      Delete
  53. hi
    i want to use PWM as input in msp430. but its not read the given input as digital(PWM). why?? how to read digital input directly

    ReplyDelete
    Replies
    1. Please see this post:

      http://www.msp430launchpad.com/2010/07/using-buttons-and-creating-new-project.html

      Follow all of my posts from the beginning, and I believe that it will help you out. For questions not relating to my posts, please check out forum.43oh.com.

      Delete
  54. is it possible to use pwm and uart simultaneously? if so can you please explain me. i am new to MSP430. kindly help ASAP

    ReplyDelete
    Replies
    1. The short answer is yes. For devices with a hardware UART, it is quite simple. Take a look at TI examples for more information. When using a software UART, things can get a bit more complicated, but you can manually toggle pins for PWM in the timer interrupt routine. I hope that answers your question. Look for example projects, this is one of the best ways to learn.

      Delete
  55. Hi all!
    What is diffence between TA0.0 and TA0.1 pin ? Please help
    Thanks!

    ReplyDelete
    Replies
    1. Great question. TA0.0 is tied to CCR0 and TA0.1 is tied to CCR1. The PWM output at the physical pin is determined by the settings in the timer registers.

      http://www.ti.com/lit/ug/slau144i/slau144i.pdf

      Check out the user guide, section 12.2.5 for a more detailed description. Hope that answers your question!

      Delete
  56. I'm using your code, trying to understand PWM on MSP430. If I want to set max PWM frequency output to 250kHz, CCR0 = 25-1;?

    ReplyDelete
    Replies
    1. CCR0 set's the period of the PWM, not the duty cycle. If you are trying to use a baseline frequency of 250kHz, you must use the following equation: Source_CLK/250kHz = CCR0.

      Delete
  57. How to generate two PWM with different duty cycle on two ports....

    ReplyDelete
  58. Hey,

    I need to do some time critical analysis.

    How can we measure the clock initialization time?

    Thanks in advance,
    Peter

    ReplyDelete
    Replies
    1. The best way to measure this is to toggle a pin beforehand and then once the timer initializes. This measurement could be done using a logic analyzer, such as this one:

      http://www.amazon.com/gp/product/B004G4ZKA6/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B004G4ZKA6&linkCode=as2&tag=msp0d-20

      If you trust the datasheets, TI provides this information.

      Delete
  59. Hi need to produce a PWM at pin P4.0 in msp430f5505.
    Can I do so?
    If I can't do so ,then can I generate PWM without the help of timers ?

    ReplyDelete
  60. Hey Nick, how would you generate a precise delay using the timer?

    So far, if I was using a 1MHz clock and wanted to make a delay of us microseconds, I've been using
    while(us--) { __delay_cycles(1); }
    but when measured against an oscilloscope, this approach is useless. How would you use timer and interrupt to make a more precise one?

    ReplyDelete
    Replies
    1. You can a few options. If you only need a few microsecond delay, you should think of increasing the clock speed if you plan on using the timer. Otherwise, you can use __delay_cycles(us), since each clock tick is one microsecond. To use a timer, you could set the value which throws an interrupt to be your number of microseconds, and then put the CPU to sleep. When the interrupt is thrown, you can then wake the CPU up, resuming operation after a delay.

      Delete
  61. Hello, nice article in here, I learn a lot information. I have a question: can I run the MSP430 to Low Power Mode 0 where fsmclk = 1MHz and make a pwm pulse of 800kHz or 500kHz in 1.5% duty cycle. I could not understand how the precision is lost..

    Thanks in advance.

    ReplyDelete
    Replies
    1. MCLK is disabled in LM0, but SMCLK is not. If your clock speed is set to the a reasonable value, I do not see why you could not produce the desired PWM.

      Delete
    2. Thanks for the fast answer. So I can change the SMCLK speed in LM0? Because if I have SMCLK limited to 1MHz and need the pwm in 800KHz I will have only 2 available duty cycles. So my question is: If I want a high pwm signal(e.x.800kHz), I need to activate the CPU?

      Delete
    3. I would recommend calculating the clock frequency you need to achieve your desired output. From there, you can determine what clock it is possible to use on the MSP430, if it is possible at all. You can look at the family user guide and the device datasheet to see what is possible from a maximum frequency perspective.

      Delete
  62. hey.
    i want simple timer program for msp430g2553
    thank you

    ReplyDelete
  63. i want a msp430 program in which the output- on to off time should be double. means 1:2 ratio

    ReplyDelete
  64. Hi,
    as i try to debug, codecomposer says that CCR0, CCTL1, CCR1 and TACTL is undefined (e.g. #20 identifier "CCR0" is undefined). I am using the tutorial code on an MSP430F5438A.
    I am greatfull for any kind of help. Thanks in advance.

    ReplyDelete
    Replies
    1. Each MSP430 has a different set of definitions for internal registers and peripherals. The best place to find these names is either in the definition header of the MSP430 Family User Guide. The Family User Guide is available for every MSP430 on the product page on TI's website. This is a great resource for understanding each peripheral and register.

      Delete
  65. Wow man! thanks author. the article is amazing. now I have a really good info about timers

    ReplyDelete
  66. what change i have to make in code for generate pwm wave's duty cycle same as per analog signal's duty cycle??

    ReplyDelete
    Replies
    1. Bansari, this is not a simple question to answer. I would recommend asking this question on the TI E2E forums, or on 43oh.com. Both are well suited for open ended questions. Good luck!

      Delete

Note: Only a member of this blog may post a comment.