I can't stop PWM signal. It always works.

Thread Starter

ali ahmet

Joined Apr 30, 2019
3
Hi, i am new with MSP430.
I would like to create a PWM signal when button is pressed to transmit IR signal and stop it after 50 miliseconds. But i cannot stop it.

Here is my code

#include <msp430.h>

void startPWM(void);

unsigned char counter=0;

void stopPWM(void);

int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
CCTL0 = CCIE;

P2DIR |= BIT2; //IR transmitter
P2SEL |= BIT2;// Select pwm pin (transmitter)

P2OUT=0x00;


while(1){

if((P1IN & BIT4)!=BIT4) //button is pressed
{
startPWM(); //send pwm
stopPWM(); // stop after 50 ms
}

}

}

void startPWM(void) // pwm signal
{
TA1CCR0 = 26-1; // Period Register
TA1CCR1 = 13; // 50% dutycycle
TA1CCTL1 |= OUTMOD_6; // TA1CCR1, Reset/Set
TA1CTL = TASSEL_1 + MC_1 + TACLR;
}


void stopPWM(void) // trying to stop after 50 ms
{
CCR0 = 5000; // CCR0 5 ms
TACTL = TASSEL_2 + MC_2; //
}

#pragma vector=TIMER0_A0_VECTOR // Timer0_A0
__interrupt void Timer_A (void)
{
if(++counter==10){ // is counter 10 (5*10=ms)
TA1CCR1 = 0; ; // Stop timer so it can't send PWM signal
counter = 0;
}
}
 

BobaMosfet

Joined Jul 1, 2009
2,211
Use code formatting tags please, and flowchart your code- that will reveal any logic problems.

Cleaned up a little bit- Don't be lazy when it comes to formatting code, make it easy to read, and put semicolons where they go (syntax):
Code:
/*****
*
*  INCLUDES
*
*****/

#include <msp430.h>


/*****
*
*  PRAGMAS
*
*****/

#pragma vector=TIMER0_A0_VECTOR                // Timer0_A0



/*****
*
*  GLOBALS
*
*****/

unsigned char counter=0;



/*****
*
* GLOBAL PROTOTYPES
*
*****/

void startPWM(void);
void stopPWM(void);



/**************************************
*
*  INTERRUPTS
*
*****/

__interrupt void Timer_A(void)
    {
    if(++counter==10)
        {                        // is counter 10 (5*10=ms)
        TA1CCR1 = 0;                    // Stop timer so it can't send PWM signal
        counter = 0;
        };
    }



/**************************************
*
*  FUNCTIONS
*
*****/

void startPWM(void)                    // pwm signal
    {
    TA1CCR0 = 26-1;                    // Period Register
    TA1CCR1 = 13;                    // 50% dutycycle
    TA1CCTL1 |= OUTMOD_6;                // TA1CCR1, Reset/Set
    TA1CTL = TASSEL_1 + MC_1 + TACLR;
    }


void stopPWM(void)                    // trying to stop after 50 ms
    {
    CCR0 = 5000;                    // CCR0 5 ms
    TACTL = TASSEL_2 + MC_2;
    }



/**************************************
*
*  MAIN
*
*****/

int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    BCSCTL1 = CALBC1_1MHZ; // Set range
    DCOCTL = CALDCO_1MHZ;
    CCTL0 = CCIE;

    P2DIR |= BIT2;                    //IR transmitter
    P2SEL |= BIT2;                    // Select pwm pin (transmitter)

    P2OUT=0x00;

    while(1)
        {
        if((P1IN & BIT4) != BIT4)            // button is pressed
            {
            startPWM();                    //send pwm
            stopPWM();                    // stop after 50 ms
            };
        };
    }
Likely your counter is zero, because every time the interrupt exits, that variable is dumped off the stack frame. If you're going to keep a count in an interrupt, it has to be a volatile global variable.
 
Last edited:
Top