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;
}