void PWM_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //关闭复用引脚的其它默认功能(JTDI调试) GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //因为定时器控制引脚,就时复用推挽(一般的是数据寄存器控制的) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //GPIO_Pin_15; 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 分频 TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //固定占空比0-100 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初始值,避免TIM由低到高不兼容(或者就把左右的参数都罗列并赋值) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; //CCR 设置其值 TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_Cmd(TIM2, ENABLE); }View Code
void PWM_SetCompare1(uint16_t Compare) { TIM_SetCompare1(TIM2, Compare); }
通过输出比较,拿到PWM波形
TIM_CtrlPWMOutputs() 在使用高级定时器时,调用这个函数使能主输出
用TIM_SetCompare1()该函数来配置CCR寄存器的值,通过与ARR数值关系,得到PWM输出占空比
通道与GPIO的关系
PSC分频 ARR周期
当需要一个频率为1000 占空比50% 分辨率为1% 带入计算即可 CK_PSC=72*10^6
引脚重映射提供更改一次的机会,但是重映射表里面没有的话就不能更改
使用引脚映射:TIM2_CH1可以从PA0重映射到PA15 (用到AFIO复用)
why?因为GPIO要经过AFIO到定时器再到NVIC才能实现
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); //部分重映射 (上面两句一般情况下就够用了,若是遇到硬骨头,比如正好遇到又是调试端口的就需要下一句解除其身份) GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //关闭复用引脚的其它默认功能(JTDI调试)
再用AFIO将JTAG复用解除
标签:TIM2,TimeBaseInitStructure,ENABLE,TIM,定时器,GPIO,PWM,RCC From: https://www.cnblogs.com/liujinmeng/p/17641547.html