Electronic – Controlling MSP430 PWM with a Laptop

msp430

I have written a code which takes two digit number from laptop and changes the PWM dutycycle to that number. It is part of a bigger requirement where I need to control motor speed over UART.

#include "io430g2553.h"
#include <stdint.h>


void PWM(uint8_t duty_cycle);
void PWM_Config();


int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
   WDTCTL = WDTPW + WDTHOLD;
  BCSCTL1 = CALBC1_1MHZ;                  // Run at 1 MHz
  DCOCTL = CALDCO_1MHZ;                   // Run at 1 MHz

   PWM_Config();
   PWM(5);
   __delay_cycles(5000000);
   PWM(15);
    __delay_cycles(5000000);
   PWM(25);
   __delay_cycles(5000000);
   PWM(50);
    __delay_cycles(5000000);
    PWM(25);
    __delay_cycles(5000000);
     PWM(15);
    __delay_cycles(5000000);
     PWM(5);
   while(1)
   {}


}


void PWM_Config()
{
  P1OUT &= 0x00; // Clearing P1OUT 
  P1SEL |= BIT6 ;
  P1SEL2 &= ~BIT6 ;
  P1DIR |= BIT6; // Configuring P1.6 as Output

}

void PWM(uint8_t duty_cycle)
{
 TA0CTL =0;
 TA0CTL |= TACLR; 
 TA0CCR1 |= (duty_cycle*100);
 TA0CCR0 |= 10000; 
 TA0CTL |= TASSEL_2 + MC_1 + ID_0;// Register TA0CTL -> SMCLK/8, Up mode
 TA0CCTL1 |= OUTMOD_7 ;//Set/Reset Mode
 TA0CCTL0 &= ~CCIE; // Interrupt Disabled
}

Edited My question on received comment:

The problem with the void PWM(uint8_t duty_cycle) function is that first time it generates the correct PWM at P1.6, next if it is given a value it changes PWM to that DC, but I can not go back to lower DC.

the fisrt 2 PWM functions in the code changes to correct duty cycle PWM(5),PWM(15) then the rest of PWM values do not produce desired dutycycle.

I am not able to troubleshoot where am I wrong, can any1 help?

Best Answer

Change the:

TA0CCR1 |= (duty_cycle*100);

To a simple assignment:

TA0CCR1 = (duty_cycle*100);

Otherwise you are OR'ing the previous value with the new one, eventually filling it with ones. That is why you can't go back to a lower duty cycle.

By the way, I don't know if your MSP430 has a hardware multiplier, but if it doesn't you should avoid direct multiplications whenever possible because they may take quite a few cycles to execute. Use instead powers of 2 (like 128), because you only need to left-shift the value.

Related Topic