芯片031c6 时钟跑48M,测试代码 TIM2_PWM_Init(SystemCoreClock/20000-1,0); // 20k
TIM1_PWM_Init(SystemCoreClock/281000-1,0);
while(1);
把TIM1_PWM_Init 注释掉TIM2为20k正常, 加上TIM1_PWM_Init TIM更新中断频率变为100多K了。。。
以下为TIM1和TIM2初始化代码:
void TIM2_PWM_Init(u16 arr, u16 psc)
{
//GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_2); //TIM2_CH3
//GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_2); //TIM2_CH3
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; //TIM3_CH1
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//TIM_OCStructInit(&TIM_OCInitStructure);
//TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_Pulse = arr/2; //设置待装入捕获比较寄存器的脉冲值
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
//TIM_OC3Init(TIM2, &TIM_OCInitStructure);
//TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
//TIM_OC4Init(TIM2, &TIM_OCInitStructure);
//TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //CH1预装载使能
TIM_ARRPreloadConfig(TIM2, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIM3
// TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
//TIM_CtrlPWMOutputs(TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
void TIM1_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_4); //TIM1_CH3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
//TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_OC1Ref);
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_OC1); //用比较匹配信号来复位TIM3
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
//TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = arr/2; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_OCInitStructure.TIM_Pulse = 40; //10个值大概变化200ns左右
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //同步主定时器输出tro
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM3
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
反复验证发现问题出在TIM1初始化如下两句代码:
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable); //允许同步
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_OC1); //设置同步触发输出源
这两句一去掉,TIM2就变为正常20k了。 一加上有变为100多K了, 搞不明白为啥TIM1
的设置会影响TIM2? TIM2我并没有打开从模式,而且我观察TIM2的周期寄存器及一些
设置寄存器并没有改变,希望原厂帮忙看看。
|