Jump to content

[MSP432] Servo librairy conflicting with PWM analogWrite

Recommended Posts



Sorry for the repost, I save my preview topic in the wrong place I think (http://forum.43oh.com/topic/9286-msp432-servo-librairy-conflicting-with-pwm-analogwrite/, if someone can delete this one...).


I am using an MSP432P401R launchpad Rev B with Energia 17 on Windows 10.

I've recently got this issue: I can't control servomotors with the Servo.h librairy and at the same time send several PWM.

As soon as I called the "attach" function from Servo, the others call to analogWrite in my code don't work anymore.

I think it is due to some timer conflicts (both functions using A0 timer ?), but I can't figured out how to fix the problem.

I see something wierd in the Servo librairy code (in Servo.cpp:72):

//timerParams.clockSource = Timer_Source_SMCLK;

This instruction, that seems to choose the timer to use, is commented. Moreover I noticed that several defined are not used in the code of Servo (from Servo.h):

#define SERVO_TIMER               TIMERA0_BASE
#define SERVO_TIMER_AB            TIMER_B

I have tried to modify "A0" by "A1" without success.


Did someone run in a similare problem and could give me some clue to fix it ?





Link to post
Share on other sites

The piece of code you point to is actually not used in the implementation. It is a left over from a previous implementation. I will remove it to avoid confusion in the future.

Servo picks a timer automatically from the pool of timers to allow maximum flexibility. Unfortunately this does not feedback into analogWrite. analogWrite might use the same timer as the Servo does and thus causing a conflict. I'll look into if there is a way to avoid this conflict.


In the meantime, which pins are you using for analogWrite? Can you share your Sketch so that we can have a look at it in detail?



Link to post
Share on other sites

Hi Robert,


Thank you for your answer. Here the servo code:

volatile int i = 90; 
volatile boolean raise = true; 

void setup_turret() 

void loop_turret()
  if ((i < T1_cmd_max)&&(raise))
    i = i + 5;
  else if ((i >T1_cmd_min)&&(!raise))
    i = i - 5;
  if (i == T1_cmd_min)
    raise = true; 
  if (i == T1_cmd_max)
    raise =false; 
    Serial.print("T0 : ");
    Serial.print(" \t ");
    Serial.print("T1 : ");
    Serial.println(" ");

We control two motors using a H bridge, so we send four PWMs. Here a code used for debuging:

 analogWrite(motor_R_Forward, 255); //v_R_for);  // ~490 Hz
 analogWrite(motor_R_Backward, 0); //v_R_back);  // ~490 Hz
 analogWrite(motor_L_Forward, 255); //v_L_for);  // ~490 Hz
 analogWrite(motor_L_Backward, 0); //v_L_back);  // ~490 Hz

And the file used to define pin numbers:

// Common Libraries #include <math.h>#include <Servo.h> ///////////////// Debug Prints /////////////////#define DEBUG_DODGE 0 // Print dodge results#define DEBUG_MOTOR1 1 // Print motor command sent (-1 -> 1)#define DEBUG_MOTOR2 1 // Print motor command sent (back 0->1, front 0->1)#define DEBUG_US 0    // Print distance detected by US sensors #define DEBUG_PHOTO 0 // Print Command send by photosensors #define DEBUG_SETPOINT 0 // Print vector command sent#define DEBUG_TURRET 0 // Print agnle sent to turret///////////////////// Pins /////////////////////// Motor Pins #define motor_R_Forward 31 // P3_7 #define motor_R_Backward 32  //P3_5 #define motor_L_Forward 4 //P3_3#define motor_L_Backward 3  //P3_2// turret pins #define turretPin_0 18 //P3_6#define turretPin_1 11 //P3_0// Turret parameters Servo T0;                    // create servo object to control down servo Servo T1;                    // create servo object to control up servo #define T1_cmd_min 30        // (
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.

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...