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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32學(xué)習(xí)筆記——利用通用定時器TIM2進行精確延時

          STM32學(xué)習(xí)筆記——利用通用定時器TIM2進行精確延時

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

          TIM_IT值

          TIM_FLAG

          描述

          TIME_FLAG_Update

          TIM中斷源

          ……

          ……

          例:

          [cpp]view plaincopy
          1. TIM_ITConfig(TIM2,TIME_FLAG_Update,ENABLE);

          2.5函數(shù)TIM_Cmd

          函數(shù)名

          TIM_Cmd

          函數(shù)原型

          voidTIM_Cmd(TIM_TypeDef*TIMx,FunctionalStateNewState)

          功能描述

          使能或者失能TIMx外設(shè)

          輸入?yún)?shù)1

          TIMx:x可以是2,3或者4,來選擇TIM外設(shè)

          輸入?yún)?shù)2

          NewState:TIMx中斷的新狀態(tài)

          這個參數(shù)可以取:ENABLE或者DISABLE

          例:

          [cpp]view plaincopy
          1. TIM_Cmd(TIM2,ENABLE);

          3例程程序

          本例程主要使用TIM2進行精準延時并亮滅LED燈,其中NVIC部分暫做了解,后面再繼續(xù)深入。另外,需要注意3.0以后版本的固件庫相比2.0版有所更改,如刪除舊版NVIC部分函數(shù),或移動至misc.c文件中,通道名TIM2_IRQChannel更改為TIM2_IRQn等。完整構(gòu)架:

          完整代碼:

          [cpp]view plaincopy
          1. #include"stm32f10x.h"
          2. voiddelay1ms(u32nTimer);
          3. voidGPIO_Configuration(void);
          4. voidTIM2_IRQHandler(void);
          5. voidTimer2_Configuration(void);
          6. voidNVIC_Configuration(void);
          [cpp]view plaincopy
          1. intmain(void)
          2. {
          3. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能GPIOC時鐘
          4. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能TIM2時鐘
          5. GPIO_Configuration();
          6. NVIC_Configuration();//配置中斷
          7. Timer2_Configuration();//配置定時器
          8. while(1)
          9. {
          10. GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_6);
          11. GPIO_SetBits(GPIOC,GPIO_Pin_9|GPIO_Pin_8);
          12. delay1ms(1000);
          13. GPIO_ResetBits(GPIOC,GPIO_Pin_9|GPIO_Pin_8);
          14. GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_6);
          15. delay1ms(1000);
          16. GPIO_Write(GPIOC,0x0140);
          17. delay1ms(2000);
          18. GPIO_Write(GPIOC,0x0280);
          19. delay1ms(2000);
          20. }
          21. }
          22. voidGPIO_Configuration(void)
          23. {
          24. GPIO_InitTypeDefGPIO_InitStructure;
          25. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
          26. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
          27. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
          28. GPIO_Init(GPIOC,&GPIO_InitStructure);
          29. }
          30. voidTimer2_Configuration(void)
          31. {
          32. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
          33. TIM_DeInit(TIM2);//使用缺省值初始化TIM外設(shè)寄存器
          34. TIM_TimeBaseStructure.TIM_Period=1;//自動重裝載寄存器值為1
          35. TIM_TimeBaseStructure.TIM_Prescaler=(36000-1);//時鐘預(yù)分頻數(shù)為36000
          36. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//采樣分頻倍數(shù)1,未明該語句作用。
          37. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//上升模式
          38. TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
          39. TIM_ClearFlag(TIM2,TIM_FLAG_Update);//清除更新標志位
          40. TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//使能中斷
          41. TIM_Cmd(TIM2,ENABLE);//使能TIM2定時器
          42. }
          43. voidNVIC_Configuration(void)
          44. {
          45. NVIC_InitTypeDefNVIC_InitStructure;
          46. NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);
          47. NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;//3.0版以后的函數(shù)庫將各通道TIM2_IRQChanel改名TIM2_IRQn
          48. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
          49. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
          50. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
          51. NVIC_Init(&NVIC_InitStructure);//NVIC_Init函數(shù)被包含在misc.c文件中。
          52. }
          53. volatileu32gTimer;
          54. voidTIM2_IRQHandler(void)
          55. {
          56. if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)//檢查溢出信號
          57. {
          58. TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除溢出標志
          59. gTimer--;
          60. }
          61. }
          62. voiddelay1ms(u32nTimer)
          63. {
          64. gTimer=nTimer;
          65. while(gTimer);
          66. }

          完成編譯并燒錄后,開發(fā)板上四個LED燈先相鄰兩兩亮滅,亮滅時間延遲1秒,然后交替兩兩亮滅,亮滅時間延遲2秒。

          參考文獻

          [1]Sharkdo.STM32用定時器精確延時的方法[EB/OL].http://www.cnblogs.com/sharkdo/archive/2011/03/23/1993036.html,2011-03-23/2012-10-14

          [2]Cdzlllfe.stm32 通用定時器精確延時程序[EB/OL].

          http://blog.sina.com.cn/s/blog_88534dff01010t1a.html,2011-12-17/2012-10-14

          [3]福州芯達工作室.《STM32入門系列教程——定時器與蜂鳴器》[EB/OL].http://ishare.iask.sina.com.cn/f/10918196.html,2010-10-20/2012-10-14.

          [4]正點電子.《Stm32不完全手冊》[EB/OL].http://www.amobbs.com/forum.php?mod=viewthread&tid=4517523,2011-01-17/2012-10-15

          [5]半壺水,《STM32菜鳥學(xué)習(xí)手冊-羅嗦版》,http://wenku.baidu.com/view/fc7c7d20ccbff121dd3683da.html,2012-08-19.

          [6]ST.《如何從STM32F10xxx固件庫V2.0.3升級為STM32F10xxx標準外設(shè)庫V3.0.0》[EB/OL].http://ishare.iask.sina.com.cn/f/18297257.html?from=like,2011-08-22/2012-09-09.


          上一頁 1 2 3 下一頁

          評論


          技術(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); })();