在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,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è)計(jì)應(yīng)用 > 轉(zhuǎn)兩個(gè)stm32定時(shí)器的使用代碼

            轉(zhuǎn)兩個(gè)stm32定時(shí)器的使用代碼

            作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
            具體沒有測試,先收藏,以后去嘗試一下

            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
            TIM_OCInitStructure.TIM_Pulse = Pluse;
            TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
            TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
            TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
            TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;

            本文引用地址:http://www.biyoush.com/article/201611/324048.htm

            TIM_OC1Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
            Pluse = MotorA.TimerPeriod - Pluse;
            TIM_OCInitStructure.TIM_Pulse = Pluse;
            TIM_OC2Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

            #define Fsys 72000000ul // system freq 72MHz
            #define Fpwm 20000 // PWM freq 20K

            void ConfigTimer(void)
            {
            TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
            TIM_OCInitTypeDef TIM_OCInitStructure;
            GPIO_InitTypeDef GPIO_InitStructure;

            RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4,ENABLE);
            RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);

            TIM_DeInit(TIM4);
            TIM_TimeBaseStructure.TIM_Prescaler = 0;
            TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
            TIM_TimeBaseStructure.TIM_Period = (Fsys/2) / Fpwm;
            TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
            TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
            TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);

            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
            TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
            TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
            TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
            TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
            TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * 20) / 100;
            TIM_OC3Init(TIM4,&TIM_OCInitStructure);

            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
            TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * (100-20)) / 100;
            TIM_OC4Init(TIM4,&TIM_OCInitStructure);

            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(GPIOB, &GPIO_InitStructure);

            TIM_CtrlPWMOutputs(TIM4, ENABLE);
            TIM_Cmd(TIM4,ENABLE);
            }

            基本設(shè)置如下:
            1)配置定時(shí)器的計(jì)數(shù)器為中間對齊計(jì)數(shù),即先向上計(jì)數(shù)再向下計(jì)數(shù)。
            2)在該定時(shí)器上選擇2個(gè)通道,并分別配置為輸出比較模式,并配置在比較成功時(shí)翻轉(zhuǎn)對應(yīng)的引腳輸出。
            3)配置自動重裝載寄存器TIMx_ARR為要求輸出頻率的一半。
            4)假定CC1為第一個(gè)輸出信號的通道,再假定第一個(gè)信號的正脈沖寬度對應(yīng)為W1,則配置TIMx_CCR1為TIMx_ARR-W1/2。
            5) 同4),假定CC2為第二個(gè)輸出信號的通道,正脈沖寬度對應(yīng)為W2,配置TIMx_CCR2為W2/2。

            ----------------------------------------------
            下面以一個(gè)例子說明:

            假設(shè)要求輸出的信號頻率為10kHz,占空比為1:3。
            再假設(shè)定時(shí)器的輸入時(shí)鐘為72MHz。

            輸出信號的頻率10kHz,換算為計(jì)數(shù)器的數(shù)值為7200。
            按照上述3),設(shè)置TIMx_ARR=3600

            輸出信號1的高電平時(shí)間W1,換算為計(jì)數(shù)器的數(shù)值為W1=7200/4=1800
            按照上述4),設(shè)置TIMx_CC1=3600 - W1/2=2700

            輸出信號2的高電平時(shí)間W2,換算為計(jì)數(shù)器的數(shù)值為W2=7200/4=1800
            按照上述5),設(shè)置TIMx_CC2=2/2=450

            參照下圖,圖中紅線表示計(jì)數(shù)器的數(shù)值變化:
            ①當(dāng)計(jì)數(shù)器的數(shù)值從0向上計(jì)數(shù),達(dá)到TIMx_CC1時(shí),CC1匹配成功,CC1的輸出電平翻轉(zhuǎn);
            ②計(jì)數(shù)器繼續(xù)向上計(jì)數(shù),達(dá)到TIMx_ARR時(shí)開始調(diào)頭向下計(jì)數(shù);當(dāng)計(jì)數(shù)器的數(shù)值下降到TIMx_CC1時(shí),CC1再次匹配成功,CC1的輸出電平再次翻轉(zhuǎn);
            ③計(jì)數(shù)器繼續(xù)向下計(jì)數(shù),達(dá)到到TIMx_CC2時(shí),CC2匹配成功,CC2的輸出電平翻轉(zhuǎn);
            ④計(jì)數(shù)器繼續(xù)向下計(jì)數(shù),減到0時(shí)開始調(diào)頭向上計(jì)數(shù);當(dāng)計(jì)數(shù)器的數(shù)值上升到TIMx_CC2時(shí),CC2再次匹配成功,CC2的輸出電平再次翻轉(zhuǎn);

            如此循環(huán),得到連續(xù)的相位互為180度的兩路輸出波形。

            注意:上述描述是一個(gè)原理性的說明,但能夠輸出要求的波形并且占空比可調(diào),實(shí)際編程計(jì)算中需要可能需要對某些數(shù)值加1或者減1,以達(dá)到準(zhǔn)確地輸出。



            關(guān)鍵詞: stm32定時(shí)器使用代

            評論


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

            關(guān)閉