Skip to main content
deleted 3263 characters in body
Source Link
#include "io430g2553.h"
#include <stdint.h>
#include <string.h>


#define LED_RED BIT0
#define LED_GREEN BIT6

void config_Rx(void);
void getDutyCycle(void);

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

uint8_t txCount=0,rxCount=0;

char command[15];
char ch='a';
uint8_t rxaFlag=0,rxbFlag=0;
uint8_t DUTY_CYCLE=0;
uint8_t PWM_FLAG=0;

voidint main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // Run at 1 MHz
DCOCTLWDTCTL = CALDCO_1MHZ; // Run atWDTPW 1+ MHz
WDTHOLD;
//By default Clock is 1MHz
config_Rx();
PWM_Config();

UCA0CTL0BCSCTL1 = 0;
UCA0CTL1CALBC1_1MHZ; = UCSSEL_2 ; // Clock from SMCLK; 
UCA0BR1 = 0; // Upper byte of divider word
UCA0BR0 = 104; // 9600 BaudrateRun at 1 MHz
 
UCA0MCTL  DCOCTL = UCBRS_1;CALDCO_1MHZ; // Low -frequency mode

//UCA0STAT = UCLISTEN; // Loopback mode
UCA0CTL1 &= ~UCSWRST; // Release from reset

__enable_interrupt();    // enableRun allat interrupts

1 MHz
IE2 |= UCA0RXIE; 
 //Enable RX interrupt

forPWM_Config(;;)
{;
// __low_power_mode_0  PWM(5);
 
switch   __delay_cycles(ch5000000){;
//State 'a' is default statePWM(15);
 in which Green LED__delay_cycles(5000000);
 is 'ON' PWM(25);
case 'a': 
P1OUT |= LED_RED;
if__delay_cycles(rxaFlag==15000000);
{ 
rxaFlag=0; 
rxCount=0;}

IE2 |=PWM(50);
 UCA0RXIE; //Whenever in this__delay_cycles(5000000);
 state enable Rx interrupt

break;PWM(25);
//State 'b' corresponds to received command "ID_CON"__delay_cycles(5000000);
//When ever the device is in this statePWM(15);
 RED LED is ON__delay_cycles(5000000);
case 'b' : 
P1OUT &= ~LED_RED;
PWM(DUTY_CYCLE5);
DUTY_CYCLE=0; //Duty Cycle Achieved 
ifwhile(rxbFlag==11)
{ 
rxbFlag=0; 
rxCount=0;}
IE2 |= UCA0RXIE; //Re-Enable Rx interrupt{}
ch='a';
break; 
}

}

}

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
}

void config_Rx(void)
{
// Configure P1.1 as UCA0RXD 

P1SEL |= BIT1 ;
P1SEL2 |= BIT1 ;

P1DIR |= (LED_RED) ; // Set P1.0 to output direction 
// P1.3 must stay at input
P1OUT &= ~(LED_RED); // set P1.0 to 0 (LED OFF) 
}

// ----------------------------------------------------------------------
// ISR for USCI_A ,B0 RX: store data , process when all bytes received
// ----------------------------------------------------------------------
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR (void) // Acknowledge by read of RXBUF
{
command[rxCount]=UCA0RXBUF;
if(command[rxCount]==0xD) // Store recd data , update counter
{
IE2 &= ~UCA0RXIE; // if user enters 'Enter (Hex 0xD)
// Disable further receive interrupts
getDutyCycle();
if (PWM_FLAG==1)
{
ch='b'; // Go To state 'b'
rxbFlag=1;
PWM_FLAG=0;//PWM command serviced
}
else{
ch='a'; // Got to State 'a'
rxaFlag=1;
PWM_FLAG=0;//PWM command serviced
DUTY_CYCLE=0;
}
}
rxCount++;
}

/*******************************************************************************
The Function compareCommand(char str[]) compares the sent str with command 
received. If str matches with the command received it returns one else
it returns zero
*******************************************************************************/

void getDutyCycle(void)
{
DUTY_CYCLE=((command[0]-48)*10)+(command[1]-48);
flushCommand();
PWM_FLAG=1;
}

/*****************************************************************************
flushCommand() clears the received command buffer
******************************************************************************/
void flushCommand(void)
{
uint8_t i=0;
for(i=0;i<15;i++)
{
command[i]='\0';
}

}

/******************************** END *****************************************/

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.

To make my point clear say what I type at my Laptop terminal (

10(Enter (ASCII 0xD) - DC 10 acheieved,

20the fisrt 2 PWM functions in the code changes to correct duty cycle PWM(Enter5) DC achieved,

Now I try to decrease DC PWM(15 Enter) -Nothing happens),

Even after some point then the rest of time I amPWM values do not able to increase the DCproduce desired dutycycle.

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


#define LED_RED BIT0
#define LED_GREEN BIT6

void config_Rx(void);
void getDutyCycle(void);

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

uint8_t txCount=0,rxCount=0;

char command[15];
char ch='a';
uint8_t rxaFlag=0,rxbFlag=0;
uint8_t DUTY_CYCLE=0;
uint8_t PWM_FLAG=0;

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

//By default Clock is 1MHz
config_Rx();
PWM_Config();

UCA0CTL0 = 0;
UCA0CTL1 = UCSSEL_2 ; // Clock from SMCLK; 
UCA0BR1 = 0; // Upper byte of divider word
UCA0BR0 = 104; // 9600 Baudrate at 1 MHz
 
UCA0MCTL = UCBRS_1; // Low -frequency mode

//UCA0STAT = UCLISTEN; // Loopback mode
UCA0CTL1 &= ~UCSWRST; // Release from reset

__enable_interrupt(); // enable all interrupts


IE2 |= UCA0RXIE; //Enable RX interrupt

for(;;)
{
// __low_power_mode_0 ();
 
switch (ch){
//State 'a' is default state in which Green LED is 'ON' 
case 'a': 
P1OUT |= LED_RED;
if(rxaFlag==1)
{ 
rxaFlag=0; 
rxCount=0;}

IE2 |= UCA0RXIE; //Whenever in this state enable Rx interrupt

break;
//State 'b' corresponds to received command "ID_CON"
//When ever the device is in this state RED LED is ON
case 'b' : 
P1OUT &= ~LED_RED;
PWM(DUTY_CYCLE);
DUTY_CYCLE=0; //Duty Cycle Achieved 
if(rxbFlag==1)
{ 
rxbFlag=0; 
rxCount=0;}
IE2 |= UCA0RXIE; //Re-Enable Rx interrupt
ch='a';
break; 
}

}

}

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
}

void config_Rx(void)
{
// Configure P1.1 as UCA0RXD 

P1SEL |= BIT1 ;
P1SEL2 |= BIT1 ;

P1DIR |= (LED_RED) ; // Set P1.0 to output direction 
// P1.3 must stay at input
P1OUT &= ~(LED_RED); // set P1.0 to 0 (LED OFF) 
}

// ----------------------------------------------------------------------
// ISR for USCI_A ,B0 RX: store data , process when all bytes received
// ----------------------------------------------------------------------
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR (void) // Acknowledge by read of RXBUF
{
command[rxCount]=UCA0RXBUF;
if(command[rxCount]==0xD) // Store recd data , update counter
{
IE2 &= ~UCA0RXIE; // if user enters 'Enter (Hex 0xD)
// Disable further receive interrupts
getDutyCycle();
if (PWM_FLAG==1)
{
ch='b'; // Go To state 'b'
rxbFlag=1;
PWM_FLAG=0;//PWM command serviced
}
else{
ch='a'; // Got to State 'a'
rxaFlag=1;
PWM_FLAG=0;//PWM command serviced
DUTY_CYCLE=0;
}
}
rxCount++;
}

/*******************************************************************************
The Function compareCommand(char str[]) compares the sent str with command 
received. If str matches with the command received it returns one else
it returns zero
*******************************************************************************/

void getDutyCycle(void)
{
DUTY_CYCLE=((command[0]-48)*10)+(command[1]-48);
flushCommand();
PWM_FLAG=1;
}

/*****************************************************************************
flushCommand() clears the received command buffer
******************************************************************************/
void flushCommand(void)
{
uint8_t i=0;
for(i=0;i<15;i++)
{
command[i]='\0';
}

}

/******************************** END *****************************************/

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.

To make my point clear say what I type at my Laptop terminal (

10(Enter (ASCII 0xD) - DC 10 acheieved,

20(Enter) DC achieved,

Now I try to decrease DC (15 Enter) -Nothing happens),

Even after some point of time I am not able to increase the DC.

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

deleted 111 characters in body
Source Link
jippie
  • 33.4k
  • 18
  • 95
  • 161

Thanks

Gaurav Kumar

Masters Engineering

Embedded Software

University Of Glasgow : 2011-2012

Thanks

Gaurav Kumar

Masters Engineering

Embedded Software

University Of Glasgow : 2011-2012

Source Link

Controlling MSP430 PWM with a Laptop

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>
#include <string.h>


#define LED_RED BIT0
#define LED_GREEN BIT6

void config_Rx(void);
void getDutyCycle(void);

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

uint8_t txCount=0,rxCount=0;

char command[15];
char ch='a';
uint8_t rxaFlag=0,rxbFlag=0;
uint8_t DUTY_CYCLE=0;
uint8_t PWM_FLAG=0;

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

//By default Clock is 1MHz
config_Rx();
PWM_Config();

UCA0CTL0 = 0;
UCA0CTL1 = UCSSEL_2 ; // Clock from SMCLK; 
UCA0BR1 = 0; // Upper byte of divider word
UCA0BR0 = 104; // 9600 Baudrate at 1 MHz

UCA0MCTL = UCBRS_1; // Low -frequency mode

//UCA0STAT = UCLISTEN; // Loopback mode
UCA0CTL1 &= ~UCSWRST; // Release from reset

__enable_interrupt(); // enable all interrupts


IE2 |= UCA0RXIE; //Enable RX interrupt

for(;;)
{
// __low_power_mode_0 ();

switch (ch){
//State 'a' is default state in which Green LED is 'ON' 
case 'a': 
P1OUT |= LED_RED;
if(rxaFlag==1)
{ 
rxaFlag=0; 
rxCount=0;}

IE2 |= UCA0RXIE; //Whenever in this state enable Rx interrupt

break;
//State 'b' corresponds to received command "ID_CON"
//When ever the device is in this state RED LED is ON
case 'b' : 
P1OUT &= ~LED_RED;
PWM(DUTY_CYCLE);
DUTY_CYCLE=0; //Duty Cycle Achieved 
if(rxbFlag==1)
{ 
rxbFlag=0; 
rxCount=0;}
IE2 |= UCA0RXIE; //Re-Enable Rx interrupt
ch='a';
break; 
}

}

}

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
}

void config_Rx(void)
{
// Configure P1.1 as UCA0RXD 

P1SEL |= BIT1 ;
P1SEL2 |= BIT1 ;

P1DIR |= (LED_RED) ; // Set P1.0 to output direction 
// P1.3 must stay at input
P1OUT &= ~(LED_RED); // set P1.0 to 0 (LED OFF) 
}

// ----------------------------------------------------------------------
// ISR for USCI_A ,B0 RX: store data , process when all bytes received
// ----------------------------------------------------------------------
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR (void) // Acknowledge by read of RXBUF
{
command[rxCount]=UCA0RXBUF;
if(command[rxCount]==0xD) // Store recd data , update counter
{
IE2 &= ~UCA0RXIE; // if user enters 'Enter (Hex 0xD)
// Disable further receive interrupts
getDutyCycle();
if (PWM_FLAG==1)
{
ch='b'; // Go To state 'b'
rxbFlag=1;
PWM_FLAG=0;//PWM command serviced
}
else{
ch='a'; // Got to State 'a'
rxaFlag=1;
PWM_FLAG=0;//PWM command serviced
DUTY_CYCLE=0;
}
}
rxCount++;
}

/*******************************************************************************
The Function compareCommand(char str[]) compares the sent str with command 
received. If str matches with the command received it returns one else
it returns zero
*******************************************************************************/

void getDutyCycle(void)
{
DUTY_CYCLE=((command[0]-48)*10)+(command[1]-48);
flushCommand();
PWM_FLAG=1;
}

/*****************************************************************************
flushCommand() clears the received command buffer
******************************************************************************/
void flushCommand(void)
{
uint8_t i=0;
for(i=0;i<15;i++)
{
command[i]='\0';
}

}

/******************************** END *****************************************/

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.

To make my point clear say what I type at my Laptop terminal (

10(Enter (ASCII 0xD) - DC 10 acheieved,

20(Enter) DC achieved,

Now I try to decrease DC (15 Enter) -Nothing happens),

Even after some point of time I am not able to increase the DC.

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

Thanks

Gaurav Kumar

Masters Engineering

Embedded Software

University Of Glasgow : 2011-2012