繁体   English   中英

全局静态变量在局部函数中突然变为0-为什么?

[英]Global static variable suddenly becoming 0 in local function - why?

我在std.h声明了一些通用标头:

static volatile unsigned int timestamp;

我在增加中断的地方(在main.c ):

void ISR_Pit(void) {
    unsigned int status;
    /// Read the PIT status register
    status = PIT_GetStatus() & AT91C_PITC_PITS;
    if (status != 0) {
        /// 1 = The Periodic Interval timer has reached PIV since the last read of PIT_PIVR.
        /// Read the PIVR to acknowledge interrupt and get number of ticks
        ///Returns the number of occurrences of periodic intervals since the last read of PIT_PIVR.
        timestamp += (PIT_GetPIVR() >> 20);
        //printf(" --> TIMERING :: %u \n\r", timestamp);
        }
    }

在另一个模块中,我有必须使用它的过程(在meta.c ):

void Wait(unsigned long delay) {
    volatile unsigned int start = timestamp;
    unsigned int elapsed;
    do {
        elapsed = timestamp;
        elapsed -= start;
        //printf(" --> TIMERING :: %u \n\r", timestamp);
        }
    while (elapsed < delay);
    }

第一个printf显示正确的timestamp但是Wait printf始终显示0 为什么?

你声明你的变量为static ,这意味着它的地方把它包含在文件的timestampmain.c比一个不同meta.c

您可以通过在main.c声明timestamp来解决此问题,如下所示:

volatile unsigned int timestamp = 0;

meta.c是这样的:

extern volatile unsigned int timestamp;

暂无
暂无

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

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