STM32--如何產(chǎn)生8路PWM波
實(shí)驗(yàn)描述:PA0,PA1,PA2,PA3,PA8,PA9,PA10,PA11輸出不同占空比的PWM波
程序清單:
#include"PWM.H"
#include "stm32f10x.h"
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //啟動(dòng)GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //啟動(dòng)AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //啟動(dòng)TIM1,功能復(fù)用IO時(shí)鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能或者失能APB1外設(shè)時(shí)鐘,RCC_APB1Periph_TIM2 為TIM2時(shí)鐘 ,這里的意思是使能TIM4時(shí)鐘
//Step2. GPIO做相應(yīng)設(shè)置,為AF輸出,使能TIM時(shí)鐘
//PA.0,1,2,3,8,9,10,11口設(shè)置為TIM1的OC1輸出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_8 | GPIO_Pin_9|GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//端口速率為50Mhz
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
}
//Step3. TIM模塊初始化
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;//初始化TIM的固定格式, 根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化//TIMx的時(shí)間基數(shù)單位
TIM_OCInitTypeDef TIM_OCInitStructure;// 根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//TIM1基本計(jì)數(shù)器設(shè)置(設(shè)置PWM頻率)
//頻率=TIM1_CLK/(ARR+1) 頻率為1khz
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);
TIM_ARRPreloadConfig(TIM1, ENABLE);//啟用ARR的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM1_OC1模塊設(shè)置(設(shè)置1通道占空比)
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//啟用CCR1寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
//TIM1_OC2模塊設(shè)置(設(shè)置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 300;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);//啟用CCR2寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
//TIM1_OC3模塊設(shè)置(設(shè)置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 600;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);//啟用CCR3寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
//TIM1_OC3模塊設(shè)置(設(shè)置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 900;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);//啟用CCR2寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)
//死區(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基本計(jì)數(shù)器設(shè)置(設(shè)置PWM頻率)
//頻率=TIM1_CLK/(ARR+1) 頻率為1khz
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(TIM2, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //其中,TIM_OCMode選擇定時(shí)器模式,TIM_OCMode_PWM1 ,TIM脈沖寬度調(diào)制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //狀態(tài)使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCPolarity輸出極性,其輸出極性為高
TIM_OCInitStructure.TIM_Pulse =100;//TIM_Pulse設(shè)置了待裝入捕獲比較寄存器的脈沖值
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =300;//TIM_Pulse設(shè)置了待裝入捕獲比較寄存器的脈沖值
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =600;//TIM_Pulse設(shè)置了待裝入捕獲比較寄存器的脈沖值
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =900;//TIM_Pulse設(shè)置了待裝入捕獲比較寄存器的脈沖值
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //TIM_OC1PreloadConfig使能或者失能TIMx在CCR1上的預(yù)裝載寄存器
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR2為捕獲/比較寄存器2
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR3為捕獲/比較寄存器2
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR4為捕獲/比較寄存器2
TIM_Cmd(TIM1, ENABLE); //TIM1,2開(kāi)啟
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE); //TIM1_OC通道輸出PWM(一定要加)
}