0
votes

I am programming an MSP430 microcontroller with the MSP430 LaunchPad Dev Kit and I am running into some problems on this simple code.

#include <msp430.h>

void Delay(void);

#define LED1 BIT0                   //define LED1 as bit 0 (0x00)
#define LED2 BIT6                   //define LED2 as bit 6 (0x40)
#define delayTime 20000             //define iTime as 20000

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;       //stop watchdog timer
    P1DIR |= (LED1|LED2);           //set P1.0 and P1.6 to output direction (P1.3 is naturally an input)
    P1OUT |= LED1;                  //set P1.0 high so the LEDs will blink alternatively

    while(1)
    {
        Delay();
        P1OUT ^= (LED1|LED2);       //toggle P1.0 using exclusive-OR
    }
}

void Delay(void)
{
    int i = 0;
    while(delayTime > i)
    {
        i++;
    }
}

This code compiles fine, but when debugging the code, the function call 'Delay()' is skipped entirely and the function is never entered. However, when I give the function a return type of 'unsigned int' like this:

unsigned int Delay(void)
{
    int i = 0;
    while(delayTime > i)
    {
        i++;
    }
    return 1;
}

I can call the Delay function in an if statement like the one below and the debugger will enter the function.

if(Delay() == 1)
{
    P1OUT ^= (LED1|LED2);       //toggle P1.0 using exclusive-OR
}

I'm sure there is some simple oversight that I'm making. I can't for the life of me figure out why the debugger is skipping my first void function call. Any wisdom?

2
Are any compiler optimizations enabled? - barak manos
Current optimization/debug settings: --opt_level=0 --opt_for_speed=1 -g - austincrft
Try changing the declaration int i = 0; to volatile int i = 0; in the Delay() function. This tells the optimizer not to touch that variable, and may be the difference between the optimizer optimizing the code away or not. - swineone
That worked perfectly! Thank you! Do you want to post that as an answer so I can check-mark it? Also, any idea why the if statement thing works? - austincrft
Are you observing that the Delay function is called, or that the delay you want imposed actually happens? Because the while-loop might still be optimized away without volatile. - Lasse V. Karlsen

2 Answers

1
votes

swineone has responded with the following correct solution in a comment:

"Try changing the declaration int i = 0; to volatile int i = 0; in the Delay() function. This tells the optimizer not to touch that variable, and may be the difference between the optimizer optimizing the code away or not."

Thanks for the help!

0
votes

It's recommended to work with interrupts. Such a task goes to this:

#include "io430.h"

#define ON 1
#define OFF 0

#define LED1 P1OUT_bit.P0
#define LED2 P1OUT_bit.P6

void init(void)
{

  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  P1OUT = 0x00;
  P1DIR = 0xFF;

  // initialize Timer0_A
  TA0CCR0 = 62500;  // set up terminal count
  TA0CTL = TASSEL_2 + ID_3 + MC_1; // configure and start timer

  // enable interrupts
  TA0CCTL0_bit.CCIE = 1;   // enable timer interrupts
  __enable_interrupt();    // set GIE in SR
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void myTimerISR(void)
{
  LED1 = ~LED1;
}