[英]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
,这意味着它的地方把它包含在文件的timestamp
在main.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.