0

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?

austincrft
  • 343
  • 1
  • 2
  • 13
  • Are any compiler optimizations enabled? – barak manos Jul 09 '14 at 15:01
  • Current optimization/debug settings: --opt_level=0 --opt_for_speed=1 -g – austincrft Jul 09 '14 at 15:04
  • 2
    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 Jul 09 '14 at 15:06
  • 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 Jul 09 '14 at 15:09
  • 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 Jul 09 '14 at 16:18
  • This was intended to be a test of calling functions in C (because it had been a while) and debugging them with Code Composer Studio. I was merely observing that the delay function was being called. I know that it is better to use the intrinsic `__delay_cycled()` function or a timer/interrupt. – austincrft Jul 10 '14 at 18:20

2 Answers2

1

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!

austincrft
  • 343
  • 1
  • 2
  • 13
0

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;
}
Ruslan Gerasimov
  • 1,752
  • 1
  • 13
  • 20
  • I just responded to a comment by Lasse V. Karlsen above talking about this. This was intended to be a personal test. But this post did help me with a similar test that I'm running with interrupts and timers, so thanks for being preemptive! :) – austincrft Jul 10 '14 at 18:24