在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32TIM模塊產(chǎn)生PWM

            STM32TIM模塊產(chǎn)生PWM

            作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
            這個是STM32的PWM輸出模式,STM32的TIM1模塊是增強型的定時器模塊,天生就是為電機控制而生,可以產(chǎn)生3組6路PWM,同時每組2路PWM為互補,并可以帶有死區(qū),可以用來驅(qū)動H橋。

              下面的代碼,是利用TIM1模塊的1、2通道產(chǎn)生一共4路PWM的代碼例子,類似代碼也可以參考ST的固件庫中相應(yīng)example

            本文引用地址:http://www.biyoush.com/article/201612/325026.htm
            C語言:TIM1模塊產(chǎn)生PWM,帶死區(qū)

            //Step1.開啟TIM和相應(yīng)端口時鐘
            //啟動GPIO
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
            RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,
            ENABLE);
            //啟動AFIO
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
            //啟動TIM1
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
            //Step2. GPIO做相應(yīng)設(shè)置,為AF輸出
            //PA.8/9口設(shè)置為TIM1的OC1輸出口
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIOA, &GPIO_InitStructure);
            //PB.13/14口設(shè)置為TIM1_CH1N和TIM1_CH2N輸出口
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIOB, &GPIO_InitStructure);
            //Step3. TIM模塊初始化
            void TIM_Configuration(void)
            {
            TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
            TIM_OCInitTypeDef TIM_OCInitStructure;
            TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
            //TIM1基本計數(shù)器設(shè)置(設(shè)置PWM頻率)
            //頻率=TIM1_CLK/(ARR+1)
            TIM_BaseInitStructure.TIM_Period = 1000-1;
            TIM_BaseInitStructure.TIM_Prescaler = 72-1;
            TIM_BaseInitStructure.TIM_ClockDivision = 0;
            TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
            TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
            TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
            //啟用ARR的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
            TIM_ARRPreloadConfig(TIM1, ENABLE);
            //TIM1_OC1模塊設(shè)置(設(shè)置1通道占空比)
            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
            TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
            TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
            TIM_OCInitStructure.TIM_Pulse = 120;
            TIM_OC1Init(TIM1, &TIM_OCInitStructure);
            //啟用CCR1寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
            TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
            //TIM2_OC2模塊設(shè)置(設(shè)置2通道占空比)
            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
            TIM_OCInitStructure.TIM_Pulse = 680;
            TIM_OC2Init(TIM1, &TIM_OCInitStructure);
            //啟用CCR2寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
            TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

            //死區(qū)設(shè)置
            TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
            TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
            TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
            TIM_BDTRInitStructure.TIM_DeadTime = 0x90; //這里調(diào)整死區(qū)大小0-0xff
            TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
            TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
            TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
            TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

            //TIM1開啟
            TIM_Cmd(TIM1, ENABLE);
            //TIM1_OC通道輸出PWM(一定要加)
            TIM_CtrlPWMOutputs(TIM1, ENABLE);
            }

              其實,PWM模塊還可以有很多花樣可以玩,比方在異常時(如CPU時鐘有問題),可以緊急關(guān)閉輸出,以免發(fā)生電路燒毀等嚴重事故


            關(guān)鍵詞: STM32TIM模塊產(chǎn)生PW

            評論


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

            關(guān)閉