Jump to content
DomTech809

Enable PWM on Port 2 of MSP430

Recommended Posts

Hello,

 

I'm trying to enable PWM of P2.0 of my MSP430 LaunchPad and have not been able to do so yet. The Chip I'm using is the msp430g2553. The code works fine with P1.6, but I have not been successful on getting the same PWM settings to output on P2.0.

 

Any help will be appreciate.

#include <msp430g2553.h>
void main(void){
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_8MHZ; //Set DCO Range, ACLK DIV/1, XTS Low-frequency mode.
DCOCTL = CALDCO_8MHZ; //Set DCO Step and Modulation
BCSCTL3 |= LFXT1S_0 + XCAP_3; //LFXT1 = 32768-Hz crystal on LFXT1, ~12.5 pF
BCSCTL2 |= SELM_0 + DIVM_0 + DIVS_3; //MCLK = DCO, SMCLK = MCLK/8
P1DIR |= BIT6; 
P2DIR |= BIT0;
P1SEL |= BIT6; 
P2SEL |= BIT0;
TA0CCR0 = 5000;
TA1CCR0 = 5000;
TA0CCR1 = 500; 
TA1CCR1 = 500;
TA0CCTL1 = OUTMOD_7; 
TA1CCTL1 = OUTMOD_7;
TA1CTL = TASSEL_2 + MC_1 + ID_2;
TA0CTL = TASSEL_2 + MC_1 + ID_2;
_BIS_SR(LPM0_bits); // Enter Low power mode 0
}

Share this post


Link to post
Share on other sites

Where is the datasheet does it say it can't be used as PWM?? I can't find that reference. I know it can be done I just can't figure out how to do it.

I think my problem is with TA1CCTL1 = OUTMOD_7; as someone else pointed out in the ti forum.

Share this post


Link to post
Share on other sites

You can not do PWM (mode 7 or 3) on CCR0.

 

CCR0 is used to set the period of the PWM signal. CCRx (x != 0) is used to set the width.

 

It is possible to do a simple square wave on CCR0 using mode 4.

 

From SLAU144i...

 

 

 

12.2.5.1 Output Modes
The output modes are defined by the OUTMODx bits and are described in Table 12-2. The OUTx signal is
changed with the rising edge of the timer clock for all modes except mode 0. Output modes 2, 3, 6, and 7
are not useful for output unit 0, because EQUx = EQU0.

Share this post


Link to post
Share on other sites

Where is the datasheet does it say it can't be used as PWM?

MCU datasheets are mostly useful as references to pinouts and electrical characteristics.  If you need programming information, you need to read the users' guide associated with the part.  It's usually available on the part webpage.

 

I actually quite like the MSP's timer setup.

Share this post


Link to post
Share on other sites

Yeah because you are familiar with it, not when you are a noob like me with MSP :!!!: .

But now that I somewhat understand how it works and have it setup so that I can use P2.1 and P2.2 as PWM output at 50Hz to control two servos, the only thing I need to do now is figure out how to set separate duty cycles for each PWM output. Both ports are on TA1.1, so right now when I change TA1CCR1 to set the duty cycle both outputs will have the same output PW, can they be set individually or do I have to use different port? For example I would like P2.1 to output a PW of 2ms and P2.2 to output a PW of 1ms.

Share this post


Link to post
Share on other sites

Yeah because you are familiar with it, not when you are a noob like me with MSP :!!!: .

That's certainly part of it.  However, for the last week I have been fighting the timer setup on an STM32L, and I've been working with STM32s for years and years.  In fact, I worked with them before I ever touched an MSP430.  I've never had to fight the 430 timer for as long... not anywhere close.  With MSP430, you can honestly get all you need via RTFM (or RTFUG).  The Users' Guide is really quite excellent.

Share this post


Link to post
Share on other sites

JAJA, had to search what RTFM is, learned something new. So do I have to use different timers to set different duty cycles for PWM??

 

Duty cycle, no.  You must have one time period, stored in CCR0.  Each other CCR channel, 1-x, represents a duty cycle of the PWM.  If CCR0 is set to 255, CCR1=16, CCR2=32, CCR3=64, then you have three PMWs.  one is 1/16 duty, one is 1/8 duty, one is 1/4 duty.  This is just an example.

 

If you want to change the period, you will need a second timer.  Actually, it's possible to hack a single timer to support multiple periods, but doing so requires some hardware design and it is not the most efficient use of resources.

Share this post


Link to post
Share on other sites

MCU datasheets are mostly useful as references to pinouts and electrical characteristics.  If you need programming information, you need to read the users' guide associated with the part.  It's usually available on the part webpage.

 

I actually quite like the MSP's timer setup.

 

I completely agree that manuals and application data helps lots, but I hope you understand that some of them need a trained eye. For example If try to find out on which ports  I can use PWM, I go to slas735h ...CTRL+F(pwm)

Does not show me what I need and I assume should appear with a different name, but which....by looking at it, I can not tell how many I can use either.

Could you give us some clues on what to look for?

Share this post


Link to post
Share on other sites

If you are a total beginner to MCU programming, it might be unusual to think that the Timer peripheral is used for PWM generation.  Maybe PIC still calls-out a specific PWM generator... no-one else does.

 

Anyway, now you know.  Use the timer output capture to generate PWM.  There are instructions in the users' guide.  I used those instructions to get working code the first time.  Nowadays, you can also look into the energia codebase or easyMSP or any of those abstraction libraries and get code examples, too.  I'm sure you can figure it out.

Share this post


Link to post
Share on other sites

Sorry, perhaps I was not clear....I have slas735h on front of me on:

Table 12. Timer0_A3 Signal Connections >>> (CCR0, TA0)

 

 

At this point I can only think to match it with column "PW20, N20"....but P1.1-3 and P1.5-7 does not say much to me.

 

If I wanted to learn "energia codebase or easyMSP", I rather go for AVR and arduino....but I'm trying to build a good base to Program on TI MCUs. Please understand, sometimes is really frustrating have the information on front of you and not being able to decipher it. Most of my friends go for arduino, If it wasn't for this forum I would of gave up weeks ago.

 

Share this post


Link to post
Share on other sites

if u read the "device output signal" column, it says TA0.0 for P1.1-3, P1.5-7

 

this means for clock output on timer 0, channel 0, u can use P1.1 or P1.5, the 3 and 7 are pin number of the device. u just need to use P1SEL register to select either BIT1 or BIT5 and use that pin to drive your led / servo, etc.

 

similarly we have TA0.1, TA0.2, TA1.0, TA1.1, TA1.2 mapping entries for 2 x 3 channels timer output (2 timer TA0 and TA1, and 3 channels each)

 

also note there are less choice on which pin to use on 20 pin devices as there are no port 3 available.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...