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.

CCS/MSP430F5359: Code get stuck at delay cycles

Expert 1165 points
Part Number: MSP430F5359


Tool/software: Code Composer Studio

Hello, i'm trying to brigtness in led while doing the dimming effect. for so i a'm using delay cycles. but the code get stuck at  __delay_cycles() while getting an interrupt. below is the code: what could be the problem?

#include "msp430.h"

unsigned int Time_period, step, x, y, z;

typedef enum
{
GREEN = BIT2, RED = BIT3, BLUE = BIT4, WHITE = BIT2 | BIT3 | BIT4
} colors_e;

volatile colors_e currColor = RED;

volatile unsigned int Sw1, Sw3; //Counter

void PWMCLK(void)
{


TA0CCR0 = 512; // PWM Period
TA0CCTL1 = OUTMOD_7 | CLLD_1; //Green LED
TA0CCTL2 = OUTMOD_7 | CLLD_1; //Red LED
TA0CCTL3 = OUTMOD_7 | CLLD_1; //Blue LED
TA0CTL = TASSEL_2 | MC_1 | TACLR; // ACLK, up mode, clear TAR

}

void LED(unsigned int Brightnessbutton, unsigned int LEDcolorchange)
{
if (Brightnessbutton == 0)
{
Time_period = 0;
step = 0;
TA0CCR1 = 0;
TA0CCR2 = 0;
TA0CCR3 = 0;
P1SEL &= ~(BIT2 | BIT3 | BIT4);
}
else if (Brightnessbutton == 1)
{
Time_period = 128;
step = 1;
}
else if (Brightnessbutton == 2)
{
Time_period = 256;
step = 2;
}
else if (Brightnessbutton == 3)
{
Time_period = 384;
step = 3;
}
else if (Brightnessbutton == 4)
{
Time_period = 512;
step = 5;
}

if (LEDcolorchange == 1)
{
currColor = (colors_e) (P1SEL & WHITE);
switch (currColor)
{
case BLUE:

for (y = 0; y < Time_period; y = y + step)
{
TA0CCR2 = y;
P1SEL = RED;
__delay_cycles(2000);

}

for (y = Time_period; y > 0; y = y - step)
{
TA0CCR2 = y;
__delay_cycles(2000);
}

break;
case RED:

for (z = 0; z < Time_period; z = z + step)

{
TA0CCR3 = z;
P1SEL = BLUE;
__delay_cycles(2000);
}

for (z = Time_period; z > 0; z = z - step)
{
TA0CCR3 = z;
__delay_cycles(2000);
}

break;
default:
P1SEL = BLUE;
break;
}
}
else if (LEDcolorchange == 2)
{

currColor = (colors_e) (P1SEL & WHITE);
switch (currColor)
{
case BLUE:

for (x = 0; x < Time_period; x = x + step)
{
TA0CCR1 = x;
P1SEL = GREEN;
__delay_cycles(2000);
}

for (x = Time_period; x > 0; x = x - step)
{
TA0CCR1 = x;
__delay_cycles(2000);
}

break;
case GREEN:

for (z = 0; z < Time_period; z = z + step)

{
TA0CCR3 = z;
P1SEL = BLUE;
__delay_cycles(2000);
}

for (z = Time_period; z > 0; z = z - step)
{
TA0CCR3 = z;
__delay_cycles(2000);
}

break;
default:
P1SEL = BLUE;
break;
}
}
else if (LEDcolorchange == 3)
{
currColor = (colors_e) (P1SEL & WHITE);
switch (currColor)
{
case RED:

for (x = 0; x < Time_period; x = x + step)
{
TA0CCR1 = x;
P1SEL = GREEN;
__delay_cycles(2000);
}

for (x = Time_period; x > 100; x = x - step)
{
TA0CCR1 = x;
__delay_cycles(2000);
}

break;
case GREEN:

for (y = 0; y < Time_period; y = y + step)
{
TA0CCR2 = y;
P1SEL = RED;
__delay_cycles(2000);
}

for (y = Time_period; y > 0; y = y - step)
{
TA0CCR2 = y;
__delay_cycles(2000);
}

break;
default:
P1SEL = RED;
break;
}
}
else if (LEDcolorchange == 4)
{
currColor = (colors_e) (P1SEL & WHITE);
switch (currColor)
{
case RED:

for (x = 0; x < Time_period; x = x + step)
{
TA0CCR1 = x;
P1SEL = GREEN;
__delay_cycles(2000);
}

for (x = Time_period; x > 0; x = x - step)
{
TA0CCR1 = x;
__delay_cycles(2000);
}

break;
case BLUE:

for (y = 0; y < Time_period; y = y + step)
{
TA0CCR2 = y;
P1SEL = RED;
__delay_cycles(2000);
}

for (y = Time_period; y > 0; y = y - step)
{
TA0CCR2 = y;
__delay_cycles(2000);
}

break;
case GREEN:

for (z = 0; z < Time_period; z = z + step)
{
TA0CCR3 = z;
P1SEL = BLUE;
__delay_cycles(2000);
}

for (z = Time_period; z > 0; z = z - step)
{
TA0CCR3 = z;
__delay_cycles(2000);
}

break;
default:
P1SEL = BLUE;
break;
}

}

}

void INTERRUPT_CLK(void)
{
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = 50000;
TA1CTL = TASSEL_1 | MC_1; // SMCLK, countmode, clear TAR

//__bis_SR_register(LPM0_bits | GIE); // Enter LPM0, enable interrupts
// __no_operation(); // For debugger
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1DIR |= BIT2 | BIT3 | BIT4; // P1.2, P1.3 and P1.4 output
P1SEL &= ~(BIT2 | BIT3 | BIT4); // P1.2 and P1.3 options select

P3REN |= (BIT1 + BIT2 + BIT3); //Enables a puller-Resistor on the button-pin
P3OUT |= (BIT1 + BIT2 + BIT3); //Writes a "1" to the portpin, tellling the resistor to pullup
P3IES |= (BIT1 + BIT2 + BIT3); //Triggers when you PRESS the button :: Pick one...
P3IFG &= ~(BIT1 + BIT2 + BIT3); // Triggers when you RELEASE the button :: ...or pick the other
P3IE |= (BIT1 + BIT2 + BIT3); //Enables the selector-mask for generating interrupts on the relevant pin


PWMCLK();
INTERRUPT_CLK();
__enable_interrupt(); // Enable interrupts globally

while (1)
{
LED(Sw1, Sw3);

}

}

#pragma vector=PORT3_VECTOR
__interrupt
void Port_3(void)
{
switch (__even_in_range(P3IV, 0x10))
{
case 0x00: //None
break;
case 0x02: //Pin0
break;
case 0x04:
//BRI
{
if (Sw1 < 4)
{
Sw1++;

}
else
{
Sw1 = 0;
}

P3IFG &= ~BIT1;
}
break;
case 0x06:
//Fix color


break;
case 0x08:
//ALTcolor
{

if (Sw3 < 4)
{
Sw3++;

}
else
{
Sw3 = 1;
}

P3IFG &= ~BIT3;
}

break;
case 0x0A:
//pin4
break;
case 0x0C:
//pin5
break;
case 0x0E:
//pin6
break;
case 0x10:
//pin7
break;
}
}

#pragma vector=TIMER1_A0_VECTOR
__interrupt
void TIMER1_A0_ISR(void)

{

if (currColor == BLUE)
{
currColor = GREEN;
}
else if (currColor == GREEN)
{
currColor = RED;
}
else if (currColor == RED)
{
currColor = BLUE;
}
else
{
currColor = RED;
}


}

  • Hello Kary,

    When posting code onto the forum, please use the code formatting tool marked by the </> button. This allows the code to be easily read and can help you get support faster on the forums. I've modified your previous post to reflect the use of the tool.

    Can you describe the issue/behavior you're seeing? Is it stuck at a the same delay or point in the code? Which interupts do you think are causing the issues?

    I would suggest starting with a Timer A0 examples like msp432F665x_ta0_02.c from TI Resource Explorer and add in the software that isn't working and see if you can locate the issue.

    Regards,

    J.D.
  • 1) Your program spends pretty much all of its time in __delay_cycles, so if you pause it you'll almost certainly find yourself in one of the calls.

    2) For an unsigned variable, ">0" is identical to "!=0". Notably (unsigned)-1 > 0.
    > for (y = Time_period; y > 0; y = y - step)
    If step is, e.g. =5, and Time_period isn't a multiple of 5, this step-down will overshoot 0 and go "negative" which unsigned means "wrapped to large (near 0xFFFF)". Some combinations, e.g. Time_period an odd number and step=2, will loop forever. It may be enough to do something like:
    > for (y = Time_period; y >= step; y = y - step)
    Though that won't always be exactly the same thing.

    Even after you fix this, you should still expect effect (1).
    ----------------------------
    Unsolicited: There's rather a lot of almost-identical code sequences here, which I think you'll find unwieldy. I suggest a few abstractions, like maybe "void count_down(unsigned period, unsigned step)" or some such.

**Attention** This is a public forum