一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

STM32F4 定時(shí)器TIM1輸出七路PWM信號(hào)【使用庫函數(shù)】

 iamlijin 2019-07-12

【實(shí)驗(yàn)?zāi)康摹?/p>

輸出7路占空比不同的PWM信號(hào)是各個(gè)版本ST庫必備的例子。本實(shí)驗(yàn)的主要目的不是表現(xiàn)ST芯片PWM功能的強(qiáng)大,而是要完成輸出的精確計(jì)算。

【實(shí)驗(yàn)內(nèi)容】

輸出7路PWM信號(hào),并用示波器測(cè)量輸出。

【實(shí)驗(yàn)原理】

1、時(shí)基單元初始化

TIM1和TIM8使用內(nèi)部時(shí)鐘時(shí),時(shí)鐘由APB2提供。但是定時(shí)器的時(shí)鐘并不是直接由APB2提供,而是來自于輸入為APB2的一個(gè)倍頻器。當(dāng)APB2的與分頻系數(shù)為1時(shí),這個(gè)倍頻器不起作用,定時(shí)器時(shí)鐘頻率等于APB2時(shí)鐘。當(dāng)APB2預(yù)分頻系數(shù)為其他時(shí)這個(gè)倍頻器起作用。定時(shí)器的輸入頻率等于APB2的2倍。本實(shí)驗(yàn)中,APB2時(shí)鐘被設(shè)置成了84M是對(duì)系統(tǒng)時(shí)鐘進(jìn)行2分頻。因此定時(shí)器的輸入時(shí)鐘是84M×2 = 168M = SYSCLK。(PS:這個(gè)倍頻我在ST的手冊(cè)上邊沒有找到,是網(wǎng)上搜索得到的結(jié)果,與實(shí)際結(jié)果對(duì)比是正確的)

TIM_Prescaler 為預(yù)分頻值,為0時(shí)分頻系數(shù)為1.

TIM_Period 為每個(gè)周期計(jì)數(shù)值,從0開始計(jì)數(shù)所以其值應(yīng)為計(jì)數(shù)次數(shù)減去1。

TIM_RepetitionCounter是F4新增的一個(gè)東西,只有高級(jí)定時(shí)器TIM1和TIM8有效,對(duì)應(yīng)寄存器RCR。意思就是每TIM_RepetitionCounter+1個(gè)技術(shù)周期產(chǎn)生一次中斷。

我定義的時(shí)基如下,將產(chǎn)生頻率為20K的即使基準(zhǔn):

TimerPeriod = (SystemCoreClock / 20000 ) - 1;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
//時(shí)基初始化
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //死區(qū)控制用。
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  //計(jì)數(shù)器方向
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;   //Timer clock = sysclock /(TIM_Prescaler+1) = 168M
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInitStructure.TIM_Period = TimerPeriod - 1;    //Period = (TIM counter clock / TIM output clock) - 1 = 20K
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);

2、計(jì)時(shí)輸出

ccr1、2、3、4為各個(gè)技術(shù)周期的TIM_Pulse。即每當(dāng)計(jì)數(shù)到這些個(gè)值的時(shí)候,PWM波形就會(huì)反轉(zhuǎn)。

ccr1 = TimerPeriod / 2;  //占空比1/2 = 50%
ccr2 = TimerPeriod / 3;  //占空比1/3 = 33%
ccr3 = TimerPeriod / 4;  //占空比1/4 = 25%
ccr4 = TimerPeriod / 5;  //占空比1/5 = 20%

定義輸出部分:

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 = ccr1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;//輸出同相,TIM_OCNPolarity_High時(shí)輸出反相
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

TIM_OC1Init(TIM1,&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_Pulse = ccr2;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_Pulse = ccr3;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_Pulse = ccr4;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);

TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);

3、到這里就完成了定時(shí)器的配置,下邊是GPIO引腳的配置

使用GPIOE的8、9、10、11、12、13、14引腳進(jìn)行PWM輸出。配置如下:

void TIM1_GPIO_Config(void)
{
  //PE 8 9 10 11 12 13 14輸出
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                                | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOE,&GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1);
}

輸出波形圖:

同相輸出時(shí)候:

OC1/OC1N

IMAG2200

OC2/OC2N

IMAG2201

OC3/OC3/N

IMAG2202

OC4

IMAG2203

反相輸出

OC1/OC1N

IMAG2204

OC2/OC2N

IMAG2205

OC3/OC3/N

IMAG2206

OC4

IMAG2207

完整的應(yīng)用代碼:

使用時(shí)只主要兩行即可

//主函數(shù)調(diào)用

   TIM1_GPIO_Config();
   Tim1_Config();

//定時(shí)器輸出引腳初始化

void TIM1_GPIO_Config(void)
{
  //PE 8 9 10 11 12 13 14輸出
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                                | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOE,&GPIO_InitStructure);
 
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1);
   
}

//TIM1做PWM輸出
void Tim1_Config(void)
{
  TimerPeriod =  (SystemCoreClock / 20000 ) - 1;
  ccr1 = TimerPeriod / 2;  //占空比1/2 = 50%
  ccr2 = TimerPeriod / 3;  //占空比1/3 = 33%
  ccr3 = TimerPeriod / 4;  //占空比1/4 = 25%
  ccr4 = TimerPeriod / 5;  //占空比1/5 = 20%
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
  //時(shí)基初始化
  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //死區(qū)控制用。
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  //計(jì)數(shù)器方向
  TIM_TimeBaseInitStructure.TIM_Prescaler = 0;   //Timer clock = sysclock /(TIM_Prescaler+1) = 168M
  TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInitStructure.TIM_Period = TimerPeriod - 1;    //Period = (TIM counter clock / TIM output clock) - 1 = 20K
  TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);

 
  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 = ccr1;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
 
  TIM_OC1Init(TIM1,&TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = ccr2;
  TIM_OC2Init(TIM1,&TIM_OCInitStructure);
 
  TIM_OCInitStructure.TIM_Pulse = ccr3;
  TIM_OC3Init(TIM1,&TIM_OCInitStructure);
 
  TIM_OCInitStructure.TIM_Pulse = ccr4;
  TIM_OC4Init(TIM1,&TIM_OCInitStructure);
 
  TIM_Cmd(TIM1,ENABLE);
  TIM_CtrlPWMOutputs(TIM1,ENABLE);
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    來自:

    麻豆视传媒短视频免费观看| 富婆又大又白又丰满又紧又硬| 亚洲国产av国产av| 欧美日韩精品久久亚洲区熟妇人| 风间中文字幕亚洲一区| 精品精品国产欧美在线| 国产三级黄片在线免费看| 国产日韩熟女中文字幕| 国产欧美日韩精品自拍| 日韩中文字幕有码午夜美女| 日本午夜福利视频免费观看| 国产精品午夜小视频观看| 国产又大又硬又粗又黄| 成人日韩在线播放视频| 欧美日韩亚洲国产av| 午夜福利网午夜福利网| 久久香蕉综合网精品视频| 欧洲一级片一区二区三区| 日本欧美一区二区三区就| 日本精品视频一二三区| 风间中文字幕亚洲一区| 午夜福利国产精品不卡| 中文字幕有码视频熟女| 亚洲超碰成人天堂涩涩| 国产成人精品一区二三区在线观看| 国产亚洲精品俞拍视频福利区| 中文文精品字幕一区二区| 国产精品99一区二区三区| 欧美亚洲综合另类色妞| 五月婷日韩中文字幕四虎| 国产又粗又硬又大又爽的视频| 亚洲国产精品国自产拍社区| 人妻内射在线二区一区| 老富婆找帅哥按摩抠逼视频| 黄色激情视频中文字幕| 高清一区二区三区不卡免费| 东京热加勒比一区二区三区| 日韩成人免费性生活视频| 91午夜少妇极品福利| 91亚洲国产日韩在线| 国产永久免费高清在线精品|