本帖最后由 jing880311 于 2019-12-20 17:47 编辑
现在是用了两个定时器
TIM5用来发脉冲
TIM3是1ms的定时,每隔1ms去修改TIM5的ARR值,感觉设置的都正确,但是实际测试发的脉冲个数不对,请问是什么原因呢?
TIM3的初始化
void MX_TIM3_Init(void) // 1ms
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 83999;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_ENABLE(&htim3);
}
TIM5的初始化/* 84M*/
void MX_TIM5_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim5.Instance = TIM5;
htim5.Init.Prescaler = 0;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = Tim5_CCR/init_speed-1;
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim5) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_ENABLE(&htim5);
TIM5 ->CR1 |= 0x0080;
}
TIM3中断函数
void TIM3_IRQHandler(void)
{
CPU_IntDis(); //CPU_CRITICAL_ENTER;
OSIntEnter();
HAL_TIM_IRQHandler(&htim3);
TIM3->ARR = 83999;
{
if(speed_up1==1)
{
if(vx1<speed_max)
{
vx1 += (float)slope_val/1000;
if(vx1>speed_max)
{
vx1 = speed_max;
speed_up1 = 0;
}
timer5ARR = Tim5_CCR/vx1;
}
}
if(speed_down1==1)
{
if(vx1>cutoff_speed)
{
vx1 -= (float)slope_val/1000;
if(vx1<=cutoff_speed)
{
vx1 = cutoff_speed;
speed_down1 = 0;
MOTOR_mov_flag = 0;
}
timer5ARR = Tim5_CCR/vx1;
}
}
}
CPU_IntEn();
OSIntExit();
}
TIM5中断函数void TIM5_IRQHandler(void)
{
CPU_IntDis(); //CPU_CRITICAL_ENTER;
OSIntEnter();
{
HAL_TIM_IRQHandler(&htim5);
TIM5->ARR = timer5ARR;
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
turns_pulse_nums++;
if(MOTOR_mov_flag)
{
if(turns_pulse_nums>=distance_R*8)
{
MOTOR_mov_over = 1;
speed_down1 = 1;
MOTOR_mov_flag = 0;
}
}
}
CPU_IntEn();
OSIntExit();
}
后来改成TIM3定时0.1ms,有改善,这样的到底要怎么处理呢? |