<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

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

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

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

          #include "SysTick.h"

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

          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://www.ex-cimer.com/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關(guān)鍵字,否則在編譯器進行優(yōu)化的時候容易產(chǎn)生錯誤。
          仔細分析下類似與變量競爭,一個是中斷不斷在引用,另外一個是while的循環(huán)。如果使用volatile關(guān)鍵字,編譯器就會對每次的變量操作進行實際賦值,從而保證了變量數(shù)據(jù)的真實性。


          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();