新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32學(xué)習(xí)筆記——使用SysTick定時(shí)器做延時(shí)

STM32學(xué)習(xí)筆記——使用SysTick定時(shí)器做延時(shí)

作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏

本文引用地址:http://2s4d.com/article/201611/322898.htm

例:

選擇1/8的AHB時(shí)鐘作為SysTick時(shí)鐘源

[cpp]view plaincopy
  1. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

3.例程

3.1程序代碼

本例程代碼在點(diǎn)亮LED燈例程代碼上做修改,使用SysTick定時(shí)延時(shí),除延時(shí)外其他代碼不變,與SysTick相關(guān)語(yǔ)句給予注釋。

[cpp]view plaincopy
  1. #include"stm32f10x.h"
  2. voidDelay(u32nTime);//聲明延遲函數(shù)
  3. voidGPIO_Configuration(void);
  4. intmain(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  7. GPIO_Configuration();
  8. while(SysTick_Config(SystemCoreClock/1000)!=0);//配置SysTick,裝入初始值,裝載值根據(jù)時(shí)鐘源頻率而定,72MHz時(shí)鐘源則產(chǎn)生1ms中斷需要裝載值為(72000000/1000)
  9. while(1)
  10. {
  11. GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
  12. GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
  13. Delay(1000);
  14. GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);
  15. GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);
  16. Delay(1000);
  17. GPIO_Write(GPIOC,0x0140);
  18. Delay(1000);
  19. GPIO_Write(GPIOC,0x0280);
  20. Delay(1000);
  21. }
  22. }
  23. voidGPIO_Configuration(void)
  24. {
  25. GPIO_InitTypeDefGPIO_InitStructure;
  26. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
  27. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  28. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  29. GPIO_Init(GPIOC,&GPIO_InitStructure);
  30. }
  31. volatileu32TimingDelay;//定義全局變量,其聲明在stm32f10x_it.c中
  32. voidDelay(u32nTime)//定義延遲函數(shù)
  33. {
  34. TimingDelay=nTime;//將延遲數(shù)賦予全局變量
  35. while(TimingDelay!=0);
  36. }

其中,在stm32f10x_it.c中:

[cpp]view plaincopy
  1. externvolatileu32TimingDelay;//聲明全局變量
  2. voidSysTick_Handler(void)
  3. {
  4. TimingDelay--;
  5. }

3.2結(jié)果

編譯燒入開(kāi)發(fā)板后,LED等以1s的時(shí)間精確交替閃爍。

參考文獻(xiàn):

[1]JosephYiu,宋巖譯.《Cortex-M3權(quán)威指南》[EB/OL].http://ishare.iask.sina.com.cn/f/11378333.html?retcode=0,2010-11-05/2012-09-09.

[2]ST.《STM32固件庫(kù)2.0.3與3.0版本的比較中文版》[EB/OL].http://ishare.iask.sina.com.cn/f/18297257.html?from=like,2011-08-22/2012-09-09.

[3]Xxbing8.STM32_SysTick[EB/OL].http://hi.baidu.com/xxbing8/item/c99ea4f53f996ad042c36ab82012-06-14/2012-09-09.



上一頁(yè) 1 2 3 下一頁(yè)

關(guān)鍵詞: STM32SysTick定時(shí)

評(píng)論


相關(guān)推薦

技術(shù)專(zhuān)區(qū)

關(guān)閉