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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32高級定時器-PWM簡單學習案例

          STM32高級定時器-PWM簡單學習案例

          作者: 時間:2017-10-11 來源:網絡 收藏

            高級定時器與通用定時器比較類似,下面是一個TIM1 的PWM 程序,TIM1是STM32唯一的高級定時器。共有4個通道有死區(qū)有互補。

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

            先是配置IO腳:

            GPIO_InitTypeDef GPIO_InitStructure;

            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

            GPIO_Init(GPIOA, GPIO_InitStructure);

            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

            GPIO_Init(GPIOB, GPIO_InitStructure);

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

            TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

            TIM_OCInitTypeDef TIM_OCInitStructure;

            void Tim1_Configuration(void)

            {

            TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

            TIM_OCInitTypeDef TIM_OCInitStructure;

            TIM_DeInit(TIM1); //重設為缺省值

            TIM_TimeBaseStructure.TIM_Prescaler = 4000; //預分頻(時鐘分頻)72M/4000=18K

            TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數

            TIM_TimeBaseStructure.TIM_Period = 144; //裝載值 18k/144=125hz 就是說向上加的144便滿了

            TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置了時鐘分割 不懂得不管

            TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //周期計數器值 不懂得不管

            TIM_TimeBaseInit(TIM1,TIM_TimeBaseStructure); //初始化TIMx的時間基數單位

            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2

            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效 PA8

            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //反向通道也有效 PB13

            TIM_OCInitStructure.TIM_Pulse = 40; //占空時間 144 中有40的時間為高,互補的輸出正好相反

            TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //輸出極性

            TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //互補端的極性

            TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空閑狀態(tài)下的非工作狀態(tài)不管

            TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //先不管

            TIM_OC1Init(TIM1,TIM_OCInitStructure); //數初始化外設TIMx通道1這里2.0庫為TIM_OCInit

            TIM_Cmd(TIM1,ENABLE);

            TIM_CtrlPWMOutputs(TIM1,ENABLE);

            }

            //設置捕獲寄存器1

            void SetT1Pwm1(u16 pulse)

            {

            TIM1-》CCR1=pulse;

            }

            //在MAIN 中加點鍵盤掃描啥的用來改變占空比

            #include “STM32Lib\f10x.h”

            #include “hal.h”

            extern void SetT1Pwm1(u16 pulse);

            int main(void)

            {

            u16 pulse=40;

            ChipHalInit(); //片內硬件初始化

            ChipOutHalInit(); //片外硬件初始化

            for(;;)

            {

            if(GET_UP())

            {

            while(GET_UP());

            if(pulse《=144)

            {

            pulse+=5;

            SetT1Pwm1(pulse);

            }

            }

            if(GET_DOWN())

            {

            while(GET_DOWN());

            if(pulse》30)

            {

            pulse-=5;

            SetT1Pwm1(pulse);

            }

            }

            }

            }

            



          關鍵詞: stm32

          評論


          相關推薦

          技術專區(qū)

          關閉
          看屁屁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); })();