简体   繁体   English

MSP430功能调用被跳过

[英]MSP430 Function Call Being Skipped

I am programming an MSP430 microcontroller with the MSP430 LaunchPad Dev Kit and I am running into some problems on this simple code. 我正在使用MSP430 LaunchPad开发套件对MSP430微控制器进行编程,并且在此简单代码上遇到了一些问题。

#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. 该代码可以很好地编译,但是在调试代码时,将完全跳过函数调用“ Delay()”,并且永远不会输入该函数。 However, when I give the function a return type of 'unsigned int' like this: 但是,当我给函数一个像这样的返回类型'unsigned int'时:

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函数,调试器将进入该函数。

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. 我一辈子都想不通为什么调试器会跳过我的第一个void函数调用。 Any wisdom? 有什么智慧吗?

swineone has responded with the following correct solution in a comment: swineone在评论中以以下正确的解决方案做出了回应:

"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." “尝试在Delay()函数中将声明int i = 0;更改为volatile int i = 0; ;。这告诉优化器不要触碰该变量,这可能是优化器是否优化代码之间的区别。”

Thanks for the help! 谢谢您的帮助!

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM