STM32设置PWM输出是时候少一下波形
//配置成PWM输出
void TIM3_Mode_Config(u16 f,u16 p)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//初始化TIM3的时间基数单位
TIM_OCInitTypeDef TIM_OCInitStructure;//初始化TIM3的外设
NVIC_InitTypeDef NVIC_InitStructure;
//打开TIM3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//TIM3的时间基数单位设置(如计数终止值:999,从0开始 ;计数方式:向上计数)
TIM_TimeBaseStructure.TIM_Prescaler= (48 - 1); //时钟预分频数,时钟频率=72MHZ/(时钟预分频+1) 之后是12M
TIM_TimeBaseStructure.TIM_Period=STARTSPEED-1; //自动重装载寄存器的值/1ms 12KHZ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //优先级分组(0~4), 抢占式为0,没嵌套
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //制定IRQ通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;//从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//TIM3的外设的设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//这个暂时不知道,stm32固件库里没有搜到。应该是定时器输出声明使能的意思
TIM_OCInitStructure.TIM_Pulse = STARTSPEED/2;//设置了待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM输出比较极性高
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能或者失能TIMx在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIM3重载寄存器ARR
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清除溢出中断标志
//TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3, ENABLE);//使能TIM3
}
|