一、输出比较简介
二、输出比较定时器
三、PWM基本结构
红色线是CCR,黄色线是自动重装值。
四、PWM频率计算
CK_PSC为系统时钟,PSC为分频值,ARR为自动重装值。
五、舵机控制
六、直流电机
七、输出比较模块配置函数
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
1.一个函数对应一个单元,TIMX为TIM口,结构体配置参数
void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
2.用来配置强制输出模式。
void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
3.用来配置ccr寄存器的预装功能
void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
4.配置快速使能
void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
5.清除REF信号
void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
6.单独设置输出的极性
void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx);
void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN);
7.修改输出使能参数
void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode);
8.输出比较模式
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);
void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
void TIM_SetCompare3(TIM_TypeDef* TIMx, uint16_t Compare3);
void TIM_SetCompare4(TIM_TypeDef* TIMx, uint16_t Compare4);
9.更改CCR寄存器的值,更改占空比的时候使用。
void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState);
10.高级定时器使用,在输出时调用该函数使能,才能正常输出PWM。
八、通用定时器OC1初始化程序
void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//激活内部时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//初始化IO口,CH1只能用PA0口
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//片上外设控制IO口要用复用输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init (GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);//选择内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//定义一个结构体,用于初始化时基
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//一分频
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up ;//向上计数
TIM_TimeBaseInitStructure.TIM_Period = 100-1;//ARR,分频后计多少个数
TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1;//PSC,对时钟进行多少分频定时频率=Fclk/自动装载寄存器的值/预分频器的值
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//高级计时器独有的,不使用时置零
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);//定时器时基初始化
TIM_OCInitTypeDef TIM_OCInitStructure;//定义结构体初始化OC输出
TIM_OCStructInit(&TIM_OCInitStructure);//先给结构体夫一个初始值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //定义输出模式
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //选择极性为高电平
TIM_OCInitStructure.TIM_OutputState = ENABLE;//输出使能
TIM_OCInitStructure.TIM_Pulse = 10;//输出占空比
TIM_OC1Init(TIM2, &TIM_OCInitStructure);//初始化OC1比较通道
TIM_Cmd(TIM2, ENABLE);//运行控制开启
}