实验描述: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); //启动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //启动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //启动TIM1,功能复用IO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能或者失能APB1外设时钟,RCC_APB1Periph_TIM2 为TIM2时钟 ,这里的意思是使能TIM4时钟
//Step2. GPIO做相应设置,为AF输出,使能TIM时钟
//PA.0,1,2,3,8,9,10,11口设置为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;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//端口速率为50Mhz
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
}
//Step3. TIM模块初始化
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;//初始化TIM的固定格式, 根据TIM_TimeBaseInitStruct中指定的参数初始化//TIMx的时间基数单位
TIM_OCInitTypeDef TIM_OCInitStructure;// 根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//TIM1基本计数器设置(设置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的影子寄存器(直到产生更新事件才更改设置)
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模块设置(设置1通道占空比)
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM1_OC2模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 300;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM1_OC3模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 600;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);//启用CCR3寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM1_OC3模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_Pulse = 900;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
//死区设置
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; //这里调整死区大小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基本计数器设置(设置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选择定时器模式,TIM_OCMode_PWM1 ,TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //状态使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCPolarity输出极性,其输出极性为高
TIM_OCInitStructure.TIM_Pulse =100;//TIM_Pulse设置了待装入捕获比较寄存器的脉冲值
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =300;//TIM_Pulse设置了待装入捕获比较寄存器的脉冲值
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =600;//TIM_Pulse设置了待装入捕获比较寄存器的脉冲值
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =900;//TIM_Pulse设置了待装入捕获比较寄存器的脉冲值
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //TIM_OC1PreloadConfig使能或者失能TIMx在CCR1上的预装载寄存器
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开启
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE); //TIM1_OC通道输出PWM(一定要加)
}
上一篇:STM32-高级定时器TIM1的应用
下一篇:STM32 串口的简单配置
推荐阅读最新更新时间:2024-03-16 15:41