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

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 通用定時(shí)器的幾種配置方式

          STM32 通用定時(shí)器的幾種配置方式

          作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
          1、普通定時(shí)使用

          本文引用地址:http://www.ex-cimer.com/article/201611/321303.htm

          #include"stm32f10x.h"
          #include"time.h"



          static Time_NVIC_Config(void)
          {
          NVIC_InitTypeDef NVIC_InitStructure;

          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);

          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

          NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

          NVIC_Init(&NVIC_InitStructure);
          }



          static void Time_Config(void)
          {
          TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx,ENABLE);
          //TIM_DeInit(TIM2);

          TIM_TimeBaseInitStructure.TIM_Prescaler =(10000-1); //時(shí)鐘分頻系數(shù)
          TIM_TimeBaseInitStructure.TIM_Period =(7200-1); //自動(dòng)重裝寄存器
          TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上計(jì)數(shù)模式
          TIM_TimeBaseInitStructure.TIM_ClockDivision =0; //時(shí)鐘分割,這里不涉及
          #if 0
          TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
          #endif

          TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置參數(shù)
          TIM_ClearITPendingBit(TIMx,TIM_IT_Update); //清除中斷標(biāo)示位
          TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE); //中斷配置
          TIM_Cmd(TIMx,ENABLE); //啟動(dòng)定時(shí)器
          }



          void Time_Init(void)
          {
          Time_Config(); //定時(shí)器配置
          Time_NVIC_Config(); //嵌套中斷向量初始化

          }

          2、PWM輸出

          #include"stm32f10x.h"
          #include"time.h"


          #if 0

          static Time_NVIC_Config(void)
          {
          NVIC_InitTypeDef NVIC_InitStructure;

          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);

          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

          NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

          NVIC_Init(&NVIC_InitStructure);
          }
          #endif



          void Time_OUT_PWM_GPIO_Config(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;

          RCC_APB2PeriphClockCmd(TIMx_OUT_GPIO_RCC,ENABLE);

          GPIO_InitStructure.GPIO_Pin =TIMx_OUT_Pin;
          GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //復(fù)用推免式輸出
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_Init(TIMx_OUT_Port, &GPIO_InitStructure);
          }



          static void Time_OUT_PWM_Config(void)
          {
          TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
          TIM_OCInitTypeDef TIM_OCInitStructure;

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx,ENABLE); //開啟時(shí)鐘
          TIM_DeInit(TIM2);

          TIM_TimeBaseInitStructure.TIM_Prescaler =(100-1); //時(shí)鐘分頻系數(shù)
          TIM_TimeBaseInitStructure.TIM_Period =(1000-1); //自動(dòng)重裝寄存器 PWM頻率:72M/100/1000=720Hz
          TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上計(jì)數(shù)模式
          TIM_TimeBaseInitStructure.TIM_ClockDivision =0; //時(shí)鐘分頻,這里不涉及
          #ifdef Time1
          TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
          #endif

          TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置參數(shù)
          TIM_ClearITPendingBit(TIMx,TIM_IT_Update); //清除中斷標(biāo)示位



          TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //PWM1模式 OCx電平根據(jù)ARR與CCRx比較結(jié)果發(fā)生反轉(zhuǎn)
          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道1
          #ifdef Time1
          TIM_OCInitStructure.TIM_OutputNState =;
          #endif
          TIM_OCInitStructure.TIM_Pulse =CCR1_VAL; //占空比800/1000
          TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low; //計(jì)數(shù)器值小于CCR值時(shí)輸出高電平
          #ifdef Time1
          TIM_OCInitStructure.TIM_OCNPolarity =;
          TIM_OCInitStructure.TIM_OCIdleState =;
          TIM_OCInitStructure.TIM_OCNIdleState =;
          #endif

          TIM_OC1Init(TIMx,&TIM_OCInitStructure);
          TIM_OC1PreloadConfig(TIMx,TIM_OCPreload_Enable); //使能CCR1預(yù)裝載


          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道2
          TIM_OCInitStructure.TIM_Pulse =CCR2_VAL;

          TIM_OC2Init(TIMx,&TIM_OCInitStructure);
          TIM_OC2PreloadConfig(TIMx,TIM_OCPreload_Enable);


          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道3
          TIM_OCInitStructure.TIM_Pulse =CCR3_VAL;
          TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;

          TIM_OC3Init(TIMx,&TIM_OCInitStructure);
          TIM_OC3PreloadConfig(TIMx,TIM_OCPreload_Enable);


          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道4
          TIM_OCInitStructure.TIM_Pulse =CCR4_VAL;

          TIM_OC4Init(TIMx,&TIM_OCInitStructure);
          TIM_OC4PreloadConfig(TIMx,TIM_OCPreload_Enable);

          TIM_ARRPreloadConfig(TIMx,ENABLE); //開啟ARR預(yù)裝載
          TIM_Cmd(TIMx,ENABLE); //啟動(dòng)定時(shí)器
          }



          void Time_OUT_PWM_Init(void)
          {
          Time_OUT_PWM_GPIO_Config();
          Time_OUT_PWM_Config(); //定時(shí)器配置
          }

          3、輸出比較模式

          #include"stm32f10x.h"
          #include"time.h"



          static Time_NVIC_Config(void)
          {
          NVIC_InitTypeDef NVIC_InitStructure;

          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);

          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

          NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

          NVIC_Init(&NVIC_InitStructure);
          }



          static void Time_OUT_PWM_GPIO_Config(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;

          RCC_APB2PeriphClockCmd(TIMx_OUT_GPIO_RCC,ENABLE);

          GPIO_InitStructure.GPIO_Pin =TIMx_OUT_Pin;
          GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //復(fù)用推免式輸出
          GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;

          GPIO_Init(TIMx_OUT_Port, &GPIO_InitStructure);
          }



          static void Time_GPIO_Config(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;

          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);

          GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
          GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; //通用推免式輸出
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_Init(GPIOD, &GPIO_InitStructure);
          }



          static void Time_OUT_PWM_Config(void)
          {
          TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
          TIM_OCInitTypeDef TIM_OCInitStructure;

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx,ENABLE); //開啟時(shí)鐘
          TIM_DeInit(TIM2);

          TIM_TimeBaseInitStructure.TIM_Prescaler =(100-1); //時(shí)鐘分頻系數(shù)
          TIM_TimeBaseInitStructure.TIM_Period =(1000-1); //自動(dòng)重裝寄存器 PWM頻率:72M/100/1000=720Hz
          TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上計(jì)數(shù)模式
          TIM_TimeBaseInitStructure.TIM_ClockDivision =0; //時(shí)鐘分頻,這里不涉及
          #ifdef Time1
          TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
          #endif

          TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置參數(shù)
          TIM_ClearITPendingBit(TIMx,TIM_IT_Update); //清除中斷標(biāo)示位



          TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Inactive; //ARR與CCRx相同時(shí)強(qiáng)制OCxREF為低
          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道1
          #ifdef Time1
          TIM_OCInitStructure.TIM_OutputNState =;
          #endif
          TIM_OCInitStructure.TIM_Pulse =CCR1_VAL;
          TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //OCx=!OCxREF _High OCx=OCxREF
          #ifdef Time1
          TIM_OCInitStructure.TIM_OCNPolarity =;
          TIM_OCInitStructure.TIM_OCIdleState =;
          TIM_OCInitStructure.TIM_OCNIdleState =;
          #endif

          TIM_OC1Init(TIMx,&TIM_OCInitStructure);
          TIM_OC1PreloadConfig(TIMx,TIM_OCPreload_Enable); //使能CCR1預(yù)裝載
          TIM_ITConfig(TIMx,TIM_IT_CC1,ENABLE); //使能OC1比較中斷


          TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Active; //ARR與CCRx相同時(shí)強(qiáng)制OCxREF為高
          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道2
          TIM_OCInitStructure.TIM_Pulse =CCR2_VAL;
          TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low; //OCx=!OCxREF _High OCx=OCxREF

          TIM_OC2Init(TIMx,&TIM_OCInitStructure);
          TIM_OC2PreloadConfig(TIMx,TIM_OCPreload_Enable);
          TIM_ITConfig(TIMx,TIM_IT_CC2,ENABLE);


          TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Timing; //ARR與CCRx比較結(jié)果對(duì)OCxREF無(wú)效
          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道3
          TIM_OCInitStructure.TIM_Pulse =CCR3_VAL;
          TIM_ITConfig(TIMx,TIM_IT_CC3,ENABLE);

          TIM_OC3Init(TIMx,&TIM_OCInitStructure);
          TIM_OC3PreloadConfig(TIMx,TIM_OCPreload_Enable);


          TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //ARR與CCRx比較結(jié)果對(duì)OCxREF采用PWM1模式
          TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //輸出使能通道4
          TIM_OCInitStructure.TIM_Pulse =CCR4_VAL;
          //TIM_ITConfig(TIMx,TIM_IT_CC4,ENABLE); //PWM就沒有必要設(shè)置中斷 當(dāng)然也可以在中斷里面改變占空比

          TIM_OC4Init(TIMx,&TIM_OCInitStructure);
          TIM_OC4PreloadConfig(TIMx,TIM_OCPreload_Enable);

          TIM_ARRPreloadConfig(TIMx,ENABLE); //開啟ARR預(yù)裝載
          TIM_Cmd(TIMx,ENABLE); //啟動(dòng)定時(shí)器
          }



          void Time_OUT_PWM_Init(void)
          {
          Time_OUT_PWM_GPIO_Config();
          Time_GPIO_Config();
          Time_NVIC_Config();
          Time_OUT_PWM_Config(); //定時(shí)器配置
          }

          4、PWMI模式

          #include"stm32f10x.h"
          #include"time.h"



          static Time_NVIC_Config(void)
          {
          NVIC_InitTypeDef NVIC_InitStructure;

          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);

          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

          NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

          NVIC_Init(&NVIC_InitStructure);
          }


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

          評(píng)論


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