资深版主
 
- 积分
- 10565
- 金钱
- 10565
- 注册时间
- 2017-2-18
- 在线时间
- 1913 小时
|
利用定时器各个通道的CCRx和ARR之比来实现不同通道输出不同频率
实现原理参考这个帖子
http://www.openedv.com/forum.php?mod=viewthread&tid=289128
在输入捕获中断中向各个通道的CCRx的写入数值来改变CCrx与ARR之比
具体代码如下
[mw_shl_code=c,true]//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能
//设置该引脚为复用输出功能,输出TIM1 CHx的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //TIM1_CHx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //翻转触发模式,当计数值与比较/捕获寄存器值相同时,翻转输出引脚的电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值,可设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable); //不使能CCMR1预装载功能,新的CCRx值随时写入到CCR1寄存器
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable); //不使能CCMR2预装载功能,新的CCRx值随时写入到CCR2寄存器
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable); //不使能CCMR3预装载功能,新的CCRx值随时写入到CCR3寄存器
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable); //不使能CCMR4预装载功能,新的CCRx值随时写入到CCR4寄存器
TIM_ClearFlag(TIM1, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4); //清除相关中断标志
TIM_ITConfig(TIM1,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE); //使能相关中断标志
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
void TIM1_CC_IRQHandler(void)
{
uint32_t value;
if (TIM_GetITStatus(TIM1, TIM_IT_CC1)!=RESET)
{
value = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1,(value+200)%1000); //修改占空比 频率为2.5K
TIM_ClearITPendingBit(TIM1 , TIM_IT_CC1);
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) !=RESET)
{
value = TIM_GetCapture2(TIM1);
TIM_SetCompare2(TIM1,(value+400)%1000); //修改占空比 频率为1.25K
TIM_ClearITPendingBit(TIM1 , TIM_IT_CC2);
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) !=RESET)
{
value = TIM_GetCapture3(TIM1);
TIM_SetCompare3(TIM1,(value+600)%1000); //修改占空比 频率为833HZ
TIM_ClearITPendingBit(TIM1 , TIM_IT_CC3);
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) !=RESET)
{
value = TIM_GetCapture4(TIM1);
TIM_SetCompare4(TIM1,(value+800)%1000); //修改占空比 频率为625HZ
TIM_ClearITPendingBit(TIM1 , TIM_IT_CC4);
}
}[/mw_shl_code]
|
|