新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 關于STM32 Systick 延時函數(shù) 變量全局引用的問題

關于STM32 Systick 延時函數(shù) 變量全局引用的問題

作者: 時間:2016-12-03 來源:網(wǎng)絡 收藏
有這樣一段代關于systick.c,如下:

#include "SysTick.h"

volatile unsigned int TimingDelay ; // 精確延時在SysTick中斷里用的計數(shù)變量
// 在全局變量里面一定要加上volatile關鍵字(易變的),否則在全局引用中容易造成編譯器的傳遞錯誤

void SysTick_1ms_Configuration(void)
{
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(SystemFrequency / 1000))
{
/* Capture error */
while (1);
}
}

void Delay_Ms(unsigned int nTime)
{
TimingDelay = nTime;

while(TimingDelay);
}

本文引用地址:http://butianyuan.cn/article/201612/325152.htm
systick.c的頭文件如下:

#ifndef __SYSTICK_H
#define __SYSTICK_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported Global variables -------------------------------------------------*/
extern volatile unsigned int TimingDelay; // 精確延時在SysTick中斷里用的計數(shù)變量

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void SysTick_1ms_Configuration(void); // 配置 1ms 時鐘心跳
void Delay_Ms(unsigned int nTime); // 1ms 延時

#endif /* __SYSTICK_H */

在中斷向量里的函數(shù)如下:

/**
* @brief This function handles SysTick Handler.
* @param None
* @retval : None
*/
void SysTick_Handler(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}

就是這樣一段代碼,在stm32里是最常用的精確延時函數(shù),在編譯器編譯等級為0的時候一切OK,但是一旦上調(diào)編譯等級到2或者3的時候程序就會死在代碼中綠色的位置。最后經(jīng)嘗試知道了是需要在全局變量里使用volatile關鍵字,否則在編譯器進行優(yōu)化的時候容易產(chǎn)生錯誤。
仔細分析下類似與變量競爭,一個是中斷不斷在引用,另外一個是while的循環(huán)。如果使用volatile關鍵字,編譯器就會對每次的變量操作進行實際賦值,從而保證了變量數(shù)據(jù)的真實性。


評論


技術專區(qū)

關閉