2012-12-21 58 views
0

我有,我声明它(在std.h)一些通用的头:全局静态变量在本地函数中突然变为0 - 为什么?

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但等待printf总是显示0。为什么?

回答

4

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

您可以修复,通过在main.c声明timestamp像这样:

volatile unsigned int timestamp = 0; 

meta.c像这样:

extern volatile unsigned int timestamp; 
+0

谢谢。真正愚蠢的错误,在C#中思考静态:) – Cynede

+0

适合所有人:) – Zoneur

相关问题