本帖最后由 爱奔跑的IT 于 2017-10-19 17:26 编辑
现在用F407控制四个电机,想用一组定时器级联一个产生PWM一个计数实现输出特定数目的PWM波,但是现在遇到输出的PWM波正常但是不能计数到指定的数目后停止产生,看了一天的手册,搞到现在还是没有成功,希望有经验的前辈能指导一下,非常感谢了。
定时器2做主定时器3做从定时器计数:
思路:主定时器产生一个触发信号(脉冲),让从定时器去接收这个触发信号并计数,达到目标的PWM脉冲数后失能两个计数器
参考别人代码如下:
定时器级联参考代码
自己的代码如下:
[mw_shl_code=c,true]void TIM2_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //初始化定时器2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //初始化GPIOA
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2); //定时器2复用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOA,&GPIO_InitStructure); //PA0初始化
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定时器2
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular); /*********只有计数溢出作为更新中断********/
//设置TIM14 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
TIM_OCInitStructure.TIM_Pulse = arr>>1; //50%占空比
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //初始化TIM_OC1
TIM2->CR2 |=0x2<<4; //配置主模式MMS=010
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM2,ENABLE);//ARPE使能
TIM_Cmd(TIM2, ENABLE); //使能TIM2
}
void TIM3_PWM_Init(u32 arr,u32 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///使能TIM3时钟
TIM3->SMCR |= 0x01<<4; //触发选择内部触发1(ITR1)TIM2
TIM3->SMCR |= 0x07<<0; //选中的触发输入为定时器计数时钟
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3
TIM_SelectOnePulseMode(TIM3,TIM_OPMode_Single);/******* 单脉冲模式**********/
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //使能计数器更新中断
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE); //使能定时器3
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Trigger) != RESET) //检查是否产生中断
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Trigger); // 清除中断
TIM_Cmd(TIM2, DISABLE); // 失能定时器2
TIM_Cmd(TIM3, DISABLE); // 失能定时器3
}
}[/mw_shl_code]
一周热门 更多>