This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

PWM using P1.2 or P1.3 on MSP430fr5969

Other Parts Discussed in Thread: MSP430FR5969, ENERGIA

I would like to generate a PWM on P1.2 or P1.3 or P1.4 since these pins can be connected to a wire ...There is software example for P1.0 and P1.1 which are LEDs which is the following...

#include <msp430.h>

int main(void)

// Configure GPIO
P1DIR |= BIT0 | BIT1; // P1.0 and P1.1 output
P1SEL0 |= BIT0 | BIT1; // P1.0 and P1.1 options select

// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings

CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL1 = DCOFSEL_6; // Set DCO = 8MHz
CSCTL3 = DIVA__8 | DIVS__8 | DIVM__8; // Set all dividers
CSCTL0_H = 0; // Lock CS registers

// Configure Timer0_A
TA0CCR0 = 1000-1; // PWM Period
TA0CCTL1 = OUTMOD_7; // CCR1 reset/set
TA0CCR1 = 750; // CCR1 PWM duty cycle
TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
TA0CCR2 = 250; // CCR2 PWM duty cycle
TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR

__bis_SR_register(LPM0_bits); // Enter LPM0
__no_operation(); // For debugger

when i try change P1DIR |= BIT0 | BIT1; // P1.0 and P1.1 output
                                P1SEL0 |= BIT0 | BIT1;


P1DIR |= BIT0 | BIT2; // P1.0 and P1.2output
P1SEL0 |= BIT0 | BIT2; 

i dont get any output for p1.2?...please tell me how to scale the same program for p1.2 and other p1.x pins...

  • Krishna, why do you like your own question? :)

    If you want to change the PWM to another pin you first have to check the datasheet for the device's pinout. P1.0 has TA0.1 as special function. P1.1 has TA0.2 - these are the internal connections to the timer module.

    Now look at P1.2 - it has TA1.1, so you have to change your code for using TA1 instead of TA0.
    P1.3 is TA1.2 and P1.4 is connected to TB0.1, again another timer module for the last one.

  • Setting a bit in P1SEL disables that pin's GPIO function, and connects the pin to some other hardware instead. That other connection is usually hardcoded; to see what you actually get, you have to look into the datasheet of your chip.

    The MSP430FR5969 datasheet (section 6.11) says that the P1.0 and P1.1 pins are connected to TA0.1 and TA0.2, which are the CCR1 and CCR2 functions of timer A0.

    Pin P1.2 is connected to TA1.1, which means that to get a PWM signal out of this pin, you must use a different timer (A1), and that timer's capture/compare register CCR1.

  • Thank you very much Dennis...
  • Thank you very much Clemens..
  • I have a beginner question that may be stupid but,

    If I wanted to use two PWM channels, to control two different motor controllers could I:

    -Use analogwrite() on pin P1.2 since it is hardware PWM.

    -Use the above code for a general I/o pin

    Additionally, how can use pin P1.1 or P1.0 if they are power pins?

    Sorry if these are ridiculous questions. I am very new to this. 



  • Hi Nolan!

    I don't know the analogwrite()-function, but PWM is a digital signal, so anything analog is wrong here. The above code does not use a "general" I/O pin - the used pins aren't chosen intentionally because these ones can internally be linked to the timer module of the processor. Look at the datasheet - they have TA0.x or TA1.x as pin functions.

    To use two different PWM signals you should use pins that share TA0.1 and TA0.2 for example.

    And power pins have never a "Px.x" port function. They are power pins, nothing else. The names for them are DVcc and DVss or AVcc and AVss.


  • Dennis Eichmann said:
    but PWM is a digital signal, so anything analog is wrong here

    Sorry Dennis but not true. PWM can be used as DAC to control an analogue motor driver.

  • Ah, OK, yes - did not think about that. But the PWM becomes analogue due to the low pass filter following the PWM. The PWM itself is on/off with variable duty cycle and this is digital.

  • So going back to my original statement, analogwrite() was the recommended method to generate pwm signals in energia. 

    If i need to run two motors off a pwm signal, could i simply use one motor on pin P1.2 (the hardware PWM), and then i would need the code from the original post in order to use the second motor controller?

    Is there an easier way that I am missing? Will this be to memory intensive if i am running other operations such as serial communication and logic for the motor control?



  • Nolan,

    then use analogwrite() for that - I have never worked with Energia, so I don't know those functions. If it controls the PWM signal, then it just might change the CCR value of the timer module. Is there a low pass filter on your board?

    Using the hardware PWM generates almost no code (at least without using Energia) since everything is done in hardware. Without Energia you would use TA0.0 as frequency and TA0.1 and TA0.2 for control of the duty cycle of your two PWM signals. How this is configured in Energia...I don't know. But the method will be the same. Then connect your two driver stages to the pins that have TA0.1 and TA0.2 as special functions. That's it.

  • Hey,

    Is there anyway that i can introduce phase difference between the two PWMs for the above code. The knobs for changing frequency and duty cycle is very clear but somehow I am unable to introduce a fixed phase difference between PWMs.



  • If you need to have symmetrical signals (such as for controlling a H-bridge), you can use the timer in up/down mode, and output modes like toogle/set or toggle/reset. (See section of the User's Guide.)

    If you need to have two 50% signals with the same frequency, you can program the timer in up mode with double the frequency, and configure both outputs in toggle mode.

    However, for usual PWM signals, phase does not matter, so there is no mechanism to have a single output signal with arbitrary pulse width and phase.

    You can get what you want with two outputs in set/reset mode and additional hardware in the form of a single XOR gate.

  • My usecase is that I need a PWM for sample/hold of a signal and the sample/hold circuit is connected to an integrator circuit ( op amp with RC)..

    So I need the other PWM for resetting my output to zero after a specific time.

    The first PWM is my sample/hold pulse and the second one is my reset PWM. I need the reset PWM to go on a high just before  and come back the fourth cycle of sample/hold.

    my sample/hold timeperiod is 4ms and top level duration is 20 microseconds. and the top level duration of my rest is 10 microseconds.

    Can such a system be configured using MSP430 alone..

  • The signals you are describing are not PWM signals; please do not call them "PWM".

    As far as I can tell, the outputs should cycle every four milliseconds. However, the timing of the pulses is not clear. Please describe exactly when the two outputs should go high and low.

  • It seems you have a periodic signal that has a period (sampling frequency) and a duty cycle (sampling time). This can be done using PWM. Your second signal also have a period (time between to integrator resets) and a duty cycle (duration of reset). However, the two have two different periods and therefore cannot be automatically generated by one timer.
    Either you get an interrupt with the same frequency as the sampling PWM signal, count the samples and manually set and reset the reset signal (the reset of the reset signal can be synchronized to one of the PWM signal edges, but not the start), or you route the PWM signal output as clock input to a second timer, where you can set the number of periods per reset cycle. But the reset pulse will then be synchronized two rising edges of the sampling PWM signal.
    What you have drawn is not possible in plain hardware and requires CPU support - with its inherent jitter and latency.

**Attention** This is a public forum