STM8S003定时器1中断服务程序(PWM)中开启定时器2定时定时功能,那么在定时器1中断服务程序退出时,立即就产生了定时器2更新中断,但是轴定时器2中断服务程序中(PWM),开启定时器2定时功能,却工作正常,何故?
这是定时器1中断服务程序的开启定时器2的代码:
//TIM2_Cmd(DISABLE);
TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN);
//TIM1_Cmd(DISABLE);
TIM1->CR1 &= (uint8_t)(~TIM1_CR1_CEN);
gsMOTOR.ucPulseCnt = 0;
TIM2_CNTR = 0;
TIM1_CNTR = 0;
TIM2_OC2PreloadConfig(DISABLE);
//TIM1_ForcedOC2Config(TIM1_FORCEDACTION_ACTIVE);
//TIM1_ForcedOC3Config(TIM1_FORCEDACTION_ACTIVE);
// TIM2_ForcedOC1Config(TIM2_FORCEDACTION_ACTIVE);
// TIM1_ForcedOC1Config(TIM1_FORCEDACTION_ACTIVE);
TIM1_CCMR1 = 0x0;
TIM1_CCMR2 = 0x58;
TIM1_CCMR3 = 0x50;
TIM2_CCMR1 = 0x58;
TIM2_CCMR2 = 0x00;
TIM2_CCER1 = 0x01;
TIM2_CR1 = 0x0C;
//TIM2_TimeBaseInit(TIM2_PRESCALER_2, gsMOTOR.uiPulseDelay);
TIM2_PSCR = TIM2_PRESCALER_2;
TIM2_ARR = gsMOTOR.uiPulseDelay << gsMOTOR.ucShifa;
//TIM2_OC2Init(TIM2_OCMODE_TIMING, TIM2_OUTPUTSTATE_DISABLE, 0xFF00, TIM2_OCPOLARITY_HIGH); //延时
//TIM2_OC1Init(TIM2_OCMODE_TIMING, TIM2_OUTPUTSTATE_DISABLE, 0xFF00, TIM2_OCPOLARITY_HIGH); //延时
//TIM2_OC3Init(TIM2_OCMODE_TIMING, TIM2_OUTPUTSTATE_DISABLE, 0xFF00, TIM2_OCPOLARITY_HIGH); //延时
//TIM2_CCR2 = 0xFFFF;
//TIM2_CCR1 = 0xFFFF;
//TIM2_CCR3 = 0xFFFF;
//TIM2->CCMR2 = 0x00;
//TIM2->CCER1 = 0x03;
gsMOTOR.ucTIM2Mode = TIM2_MODE_DELAY;
//TIM1_Cmd(ENABLE);
//TIM2_GenerateEvent(TIM2_EVENTSOURCE_UPDATE);
//TIM2_EGR = 1;
//TIM2_ClearFlag(TIM2_FLAG_UPDATE);
TIM2_SR1 = 0;
TIM2_SR2 = 0;
TIM2_SR1 = 0;
TIM2_SR2 = 0;
//TIM2_Cmd(ENABLE);
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN;
这是定时器2中断服务程序中开启定时2定时功能的代码:
TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN);
//TIM1_Cmd(DISABLE);
TIM1->CR1 &= (uint8_t)(~TIM1_CR1_CEN);
gsMOTOR.ucPulseCnt = 0;
TIM2_CNTR = 0;
//TIM2_ARRPreloadConfig(DISABLE);
TIM2_CR1 = 0x0C;
TIM2_OC2PreloadConfig(DISABLE);
TIM2_OC1PreloadConfig(DISABLE);
TIM2_OC3PreloadConfig(DISABLE);
//TIM1_ForcedOC2Config(TIM1_FORCEDACTION_ACTIVE);
//TIM1_ForcedOC3Config(TIM1_FORCEDACTION_ACTIVE);
//TIM2_ForcedOC1Config(TIM2_FORCEDACTION_ACTIVE);
//TIM1_ForcedOC1Config(TIM1_FORCEDACTION_ACTIVE);
TIM1_CCMR1 = 0x58;
TIM1_CCMR2 = 0x58;
TIM1_CCMR3 = 0x50;
TIM2_CCMR1 = 0x58;
TIM2_CCMR2 = 0x00;
//TIM2_TimeBaseInit(TIM2_PRESCALER_1, gsMOTOR.uiPulseDelay);
TIM2_PSCR = TIM2_PRESCALER_2;
TIM2_ARR = gsMOTOR.uiPulseDelay << gsMOTOR.ucShifb;
//TIM2_SetCompare1(0xFFFF); //通道二也会一直在比较,只是不输出而已,因此需要将其设为最大值
TIM2_CCR1 = 0xFFFF;
TIM2_CCR3 = 0xFFFF;
TIM2_OC2Init(TIM2_OCMODE_TIMING, TIM2_OUTPUTSTATE_DISABLE, 0xFFFF, TIM2_OCPOLARITY_HIGH); //延时
gsMOTOR.ucTIM2Mode = TIM2_MODE_DELAY;
//TIM1_Cmd(ENABLE);
TIM2_GenerateEvent(TIM2_EVENTSOURCE_UPDATE);
//TIM2_EGR = 0;
//TIM2_ClearFlag(TIM2_FLAG_UPDATE);
TIM2_SR1 = 0;
TIM2_SR2 = 0;
TIM2_SR1 = 0;
TIM2_SR2 = 0;
//TIM2_Cmd(ENABLE);
TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN; |