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.

Compiler/MSP430G2553: SERVO MOTOR CONTROLS

Part Number: MSP430G2553

Tool/software: TI C/C++ Compiler

I wrote this program but it has some errors as follows

1.Unstable output pulse response
2.No synchronous control of all 3 motor
I have read from many different sources. But I can't find error in the program. 
I look forward to receiving help from you. thanks in advance
#include "msp430g2553.h"

//---------------------Define cho PWM
#define G_CPU           1000000UL
#define PWM_PERIOD      2500
#define PWM_DUTY(x)     ((unsigned long)x*PWM_PERIOD/(unsigned long)(0xff))

//---------------------Dinh nghia chan
#define SERVO1  6
#define SERVO2  1
#define SERVO3  4
 
//----------------------Khai bao prototype ham
void SetPWM(int pin, int val);
int ReadADC10(int channel);
long map(long x, long in_min, long in_max, long out_min, long out_max);

//---------------------Chuong trinh chinh
void main()
{
  WDTCTL = WDTPW + WDTHOLD;
    
//---------------------Khoi tao Timer
  TA0CCR0 = PWM_PERIOD; 
  TA0CTL = TASSEL_2 + MC_1 + ID_3;
  
  TA1CCR0 = PWM_PERIOD; 
  TA1CTL = TASSEL_2 + MC_1 + ID_3;
//---------------------Khoi tai ADC 
  ADC10CTL1 = ADC10DIV_3;
  ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON;
//--------------------------------------------------
  while(1)
  {
     TA0CCR1 = map(ReadADC10(5),0,1023,0,312);
     SetPWM(SERVO1, TA0CCR1);
     TA1CCR1 = map(ReadADC10(4),0,1023,0,312);
     SetPWM(SERVO2, TA1CCR1);
     TA1CCR2 = map(ReadADC10(3),0,1023,0,312);
     SetPWM(SERVO3, TA1CCR2);
     __delay_cycles(100000);
  }//end while
}//end main

//----------------------Khoi tai timer A
void SetPWM(int pin, int val)
{ 
  switch (pin)
  {
  case 1: P2DIR |= BIT1;
          P2SEL |= BIT1;
          TA1CCTL1 = OUTMOD_3;
          TA1CCR1  = PWM_DUTY(val);
          break;
  case 4: P2DIR |= BIT4;
          P2SEL |= BIT4;
          TA1CCTL2 = OUTMOD_3;
          TA1CCR2  = PWM_DUTY(val);
          break;
  case 6: P1DIR |= BIT6;
          P1SEL |= BIT6;
          TA0CCTL1 = OUTMOD_3;
          TA0CCR1  = PWM_DUTY(val);
          break;
  }
}

//----------------------Ham chuyen doi gia tri
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//----------------------Chuong trinh doc ADC10
int ReadADC10(int channel)
{
ADC10AE0 = (BIT0<<channel);
ADC10CTL1 = (channel*0x1000u);
ADC10CTL0 |= ENC + ADC10SC;
while(!(ADC10CTL0 & ADC10IFG));
return ADC10MEM;
}

  • Hello,

    What do your output signals look like? Are you seeing any PWMs?
    Is your ADC function reading as expected?

    Also, I see that you are setting the TA1CCRx register in the main, then using it as in input and setting it again inside the function with possibly a different value. I think this is definitely a problem.

    I would suggest starting with a single PWM channel and scaling up from there. There are PWM examples for the MSP430G2553 here: dev.ti.com/.../node

    Thanks,
    JD
  • Hello,

    Were you able to get the PWMs working with the examples? I'm going to close this thread for now, but feel free to respond here!

    Thanks,
    JD
  • It still doesn't work!
    I use the MSP430G2553 PWM to control the SERVO motor and read the ADC by a variable resistor to change the pulse width of SERVO
    For the case of using TIMER TA0CCR1 and A5 channel to control a SERVO, everything works well
    But when I simultaneously use TIMER TA0CCR1 (T0.1), TA1CCR1 (T1.1), TA1CCR2 (T1.2) to control 3 motors, the result is not what I programmed. When turning the first potentiometer, all three SERVOs rotate simultaneously, and when the two other resistors are turned, the SERVOs have no effect. In addition, the MSP430 is reset repeatedly when turning the resistor.
    I then added 3 buttons to the pins A0, A1, A2. It is possible to overcome the state of three SERVOs rotating at the same time when rotating VR1 (A5). VR2 (A4) and VR3 (A3) still have no impact. MSP430 is still reset
    Therefore, I picked up the opto PC817 to isolate the power, preventing the reset status of the MSP430. But then the whole system is not working.
  • Thanks you vevy much for your answer!

**Attention** This is a public forum