底层库:基于原子的标准库函数
适用芯片:STM32F103系列型号芯片,其他型号未测试(注:已测型号:C8T6、CBT6、RCT6、RET6、ZET6)
所用到的定时器:TIM1、TIM2、TIM3、TIM4
所用的IO口:TIM1:PA8 PA9 PA10 PA11;TIM2:PA0 PA1 PA2 PA3 ;TIM3:PA6 PA7 PB0 PB1 TIM4:PB6 PB7 PB8 PB9
输出:16路PWM,占空比0-100可调,频率1KHz
**注意!!! 高级定时器必须加上在初始化末尾加上:TIM_CtrlPWMOutputs(TIM1, ENABLE);
//TIM1 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //IO 复用
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE); //TIM1的部分重映射,加这句,并修改IO配置
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //TIM1的完全重映射,加这句,并修改IO配置
//没有重映射时,TIM1的四个通道CH1,CH2,CH3,CH4分别对应 PA8 PA9 PA10 PA11
//部分重映射时,TIM1的四个通道CH1,CH2,CH3,CH4分别对应 PA8 PA9 PA10 PA11
//完全重映射时,TIM1的四个通道CH1,CH2,CH3,CH4分别对应 PE7 PE9 PE11 PE13
GPIO_InitStructure.GPIO_Pin = 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;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//初始化TIM1
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_Pulse = 0;
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1, ENABLE); //使能TIM3
TIM_CtrlPWMOutputs(TIM1, ENABLE); //高级定时器必须使能 pwm输出
TIM_SetCompare1(TIM1,0); //占空比 = 0 / TIM_Period * 100%
TIM_SetCompare2(TIM1,0);
TIM_SetCompare3(TIM1,0);
TIM_SetCompare4(TIM1,0);
}
//TIM2 PWM部分初始化
//PWM输出初始化
//arr:自动重装值 占空比 = 0 / TIM_Period * 100%
//psc:时钟预分频数
void TIM2_PWM_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //IO 复用
//GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); //TIM2的部分重映射 1,加这句,并修改IO配置
//GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE); //TIM2的部分重映射 2,加这句,并修改IO配置
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //TIM2的完全重映射 ,加这句,并修改IO配置
//没有重映射,TIM2的四个通道CH1,CH2,CH3,CH4分别对应 PA0 PA1 PA2 PA3
//部分重映射 1,TIM2的四个通道CH1,CH2,CH3,CH4分别对应 PA15 PB3 PA2 PA3
//部分重映射 2,TIM2的四个通道CH1,CH2,CH3,CH4分别对应 PA0 PA1 PB10 PB11
//完全重映射,TIM2的四个通道CH1,CH2,CH3,CH4分别对应 PA15 PB3 PB10 PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//初始化TIM1
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE); //使能TIM2
TIM_SetCompare1(TIM2,0); //占空比 = 0 / TIM_Period * 100%
TIM_SetCompare2(TIM2,0);
TIM_SetCompare3(TIM2,0);
TIM_SetCompare4(TIM2,0);
}
//TIM3 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM3_PWM_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //IO 复用
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //TIM3的部分重映射,加这句,并修改IO配置
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //TIM3的完全重映射,加这句,并修改IO配置
//当没有重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应 PA6 PA7 PB0 PB1
//当部分重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应 PB4 PB5 PB0 PB1
//当完全重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应 PC6 PC7 PC8 PC9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_Pulse = 0;
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE); //使能TIM3
TIM_SetCompare1(TIM3,0); //占空比 = 0 / TIM_Period * 100%
TIM_SetCompare2(TIM3,0);
TIM_SetCompare3(TIM3,0);
TIM_SetCompare4(TIM3,0);
//TIM4 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM4_PWM_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //IO 复用
//GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //TIM4的完全重映射,加这句,并修改IO配置
//当没有重映射时,TIM4的四个通道CH1,CH2,CH3,CH4分别对应 PB6 PB7 PB8 PB9
//当完全重映射时,TIM4的四个通道CH1,CH2,CH3,CH4分别对应 PD12 PD13 PD14 PD15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|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);//初始化GPIO
//初始化TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_Pulse = 0;
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_Cmd(TIM4, ENABLE); //使能TIM3
TIM_SetCompare1(TIM4,0); //占空比 = 0 / TIM_Period * 100%
TIM_SetCompare2(TIM4,0);
TIM_SetCompare3(TIM4,0);
TIM_SetCompare4(TIM4,0);
TIM1_PWM_Init(100-1,720-1); //PWM频率=72M/(100*720)=1Khz
TIM2_PWM_Init(100-1,720-1); //PWM频率=72M/(100*720)=1Khz
TIM3_PWM_Init(100-1,720-1); //PWM频率=72M/(100*720)=1Khz
TIM4_PWM_Init(100-1,720-1); //PWM频率=72M/(100*720)=1Khz
1—TIM1为高级定时器,初始化时需要加上:TIM_CtrlPWMOutputs(TIM1, ENABLE);
2—TIM2部分重映射有两组
3—TIM4不区分部分和完全重映射,统为重映射,重映射配置时参数为GPIO_Remap_TIM4
4—在PWM输出频率为1KHz时,对于大部分隔离芯片会有意想不到的优势