示例#1
0
/**
  * @brief  This function handles TIM2 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    /* Pin PC.00 toggling with frequency = 390 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_0)));
    capture = TIM_GetCapture1(TIM2);
    TIM_SetCompare1(TIM2, capture + CCR1_Val);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

    /* Pin PC.01 toggling with frequency = 585 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_1)));
    capture = TIM_GetCapture2(TIM2);
    TIM_SetCompare2(TIM2, capture + CCR2_Val);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

    /* Pin PC.02 toggling with frequency = 1.17 KHz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_2, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_2)));
    capture = TIM_GetCapture3(TIM2);
    TIM_SetCompare3(TIM2, capture + CCR3_Val);
  }
  else
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

    /* Pin PC.03 toggling with frequency = 2.34 KHz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_3, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_3)));
    capture = TIM_GetCapture4(TIM2);
    TIM_SetCompare4(TIM2, capture + CCR4_Val);
  }
}
示例#2
0
static void timCCxHandler(TIM_TypeDef *tim)
{
    uint16_t capture;
    timerConfig_t *timerConfig;

    uint8_t channelIndex = 0;
    for (channelIndex = 0; channelIndex < CC_CHANNELS_PER_TIMER; channelIndex++) {
        uint8_t channel = channels[channelIndex];

        if (channel == TIM_Channel_1 && TIM_GetITStatus(tim, TIM_IT_CC1) == SET) {
            TIM_ClearITPendingBit(tim, TIM_IT_CC1);

            timerConfig = findTimerConfig(tim, TIM_Channel_1);
            capture = TIM_GetCapture1(tim);
        } else if (channel == TIM_Channel_2 && TIM_GetITStatus(tim, TIM_IT_CC2) == SET) {
            TIM_ClearITPendingBit(tim, TIM_IT_CC2);

            timerConfig = findTimerConfig(tim, TIM_Channel_2);
            capture = TIM_GetCapture2(tim);
        } else if (channel == TIM_Channel_3 && TIM_GetITStatus(tim, TIM_IT_CC3) == SET) {
            TIM_ClearITPendingBit(tim, TIM_IT_CC3);

            timerConfig = findTimerConfig(tim, TIM_Channel_3);
            capture = TIM_GetCapture3(tim);
        } else if (channel == TIM_Channel_4 && TIM_GetITStatus(tim, TIM_IT_CC4) == SET) {
            TIM_ClearITPendingBit(tim, TIM_IT_CC4);

            timerConfig = findTimerConfig(tim, TIM_Channel_4);
            capture = TIM_GetCapture4(tim);
        } else {
            continue; // avoid uninitialised variable dereference
        }

        if (!timerConfig->callback) {
            continue;
        }
        timerConfig->callback(timerConfig->reference, capture);
    }
}
示例#3
0
/**
  * @brief  This function handles TIM2 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    /* Pin PC.06 toggling with frequency = 73.24 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
    capture = TIM_GetCapture1(TIM2);
    TIM_SetCompare1(TIM2, capture + CCR1_Val);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

    /* Pin PC.07 toggling with frequency = 109.8 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
    capture = TIM_GetCapture2(TIM2);
    TIM_SetCompare2(TIM2, capture + CCR2_Val);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

    /* Pin PC.08 toggling with frequency = 219.7 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_8)));
    capture = TIM_GetCapture3(TIM2);
    TIM_SetCompare3(TIM2, capture + CCR3_Val);
  }
  else
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

    /* Pin PC.09 toggling with frequency = 439.4 Hz */
    GPIO_WriteBit(GPIOC, GPIO_Pin_9, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9)));
    capture = TIM_GetCapture4(TIM2);
    TIM_SetCompare4(TIM2, capture + CCR4_Val);
  }
}
示例#4
0
/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

    /* LED1 toggling with frequency = 73.24 Hz */
    STM_EVAL_LEDToggle(LED1);
	capture = TIM_GetCapture1(TIM3);
    TIM_SetCompare1(TIM3, capture + CCR1_Val);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

    /* LED2 toggling with frequency = 109.8 Hz */
    STM_EVAL_LEDToggle(LED2);
    capture = TIM_GetCapture2(TIM3);
    TIM_SetCompare2(TIM3, capture + CCR2_Val);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);

    /* LED3 toggling with frequency = 219.7 Hz */
    STM_EVAL_LEDToggle(LED3);
    capture = TIM_GetCapture3(TIM3);
    TIM_SetCompare3(TIM3, capture + CCR3_Val);
  }
  else
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);

    /* LED4 toggling with frequency = 439.4 Hz */
    STM_EVAL_LEDToggle(LED4);
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + CCR4_Val);
  }
}
/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

    /* LED4 toggling with frequency = 4.57 Hz */
    STM_EVAL_LEDToggle(LED4);
	capture = TIM_GetCapture1(TIM3);
    TIM_SetCompare1(TIM3, capture + CCR1_Val);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

    /* LED3 toggling with frequency = 9.15 Hz */
    STM_EVAL_LEDToggle(LED3);
    capture = TIM_GetCapture2(TIM3);
    TIM_SetCompare2(TIM3, capture + CCR2_Val);
  }
  else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);

    /* LED5 toggling with frequency = 18.31 Hz */
    STM_EVAL_LEDToggle(LED5);
    capture = TIM_GetCapture3(TIM3);
    TIM_SetCompare3(TIM3, capture + CCR3_Val);
  }
  else
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);

    /* LED6 toggling with frequency = 36.62 Hz */
    STM_EVAL_LEDToggle(LED6);
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + CCR4_Val);
  }
}
示例#6
0
/**
  * @brief  This function handles TIM5 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM5_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)
  {  
    /* Clear CC4 Interrupt pending bit */
    TIM_ClearITPendingBit(TIM5, TIM_IT_CC4);
    if (CaptureNumber >= 2)
        return;
   
    /* Get the Input Capture value */
    tmpCC4[CaptureNumber++] = TIM_GetCapture4(TIM5);
 
    if (CaptureNumber == 2)
    {
      /* Compute the period length */
      PeriodValue = (uint16_t)(0xFFFF - tmpCC4[0] + tmpCC4[1] + 1);
      if(_measureLSIComplete_SEM != NULL){
        mico_rtos_set_semaphore(&_measureLSIComplete_SEM);
      }
    }
  }
}
void LHL_0_execute(uint16_t time) // time = time*0.1 (ms)
{
	static uint16_t ticker = 0;
	if ((uint16_t)(ticker+time)==GetTicker()) 
	{
		ticker = GetTicker();
		
		CCR_now=TIM_GetCapture4(TIM2);
		if (phi_LHL_0>CCR_now)
		{
			CCR_now++;
			if ((SysTicker % k_LHL_0) ==0)
				TIM_SetCompare4(TIM2, CCR_now);
		}
		else if (phi_LHL_0<CCR_now)
		{
			CCR_now--;
			if ((SysTicker % k_LHL_0) ==0)
				TIM_SetCompare4(TIM2, CCR_now);
		}
	}
}
示例#8
0
static void pwmTIMxHandler(TIM_TypeDef *tim, uint8_t portBase)
{
    int8_t port;

    // Generic CC handler for TIM2,3,4
    if (TIM_GetITStatus(tim, TIM_IT_CC1) == SET) {
        port = portBase + 0;
        TIM_ClearITPendingBit(tim, TIM_IT_CC1);
        pwmPorts[port].callback(port, TIM_GetCapture1(tim));
    } else if (TIM_GetITStatus(tim, TIM_IT_CC2) == SET) {
        port = portBase + 1;
        TIM_ClearITPendingBit(tim, TIM_IT_CC2);
        pwmPorts[port].callback(port, TIM_GetCapture2(tim));
    } else if (TIM_GetITStatus(tim, TIM_IT_CC3) == SET) {
        port = portBase + 2;
        TIM_ClearITPendingBit(tim, TIM_IT_CC3);
        pwmPorts[port].callback(port, TIM_GetCapture3(tim));
    } else if (TIM_GetITStatus(tim, TIM_IT_CC4) == SET) {
        port = portBase + 3;
        TIM_ClearITPendingBit(tim, TIM_IT_CC4);
        pwmPorts[port].callback(port, TIM_GetCapture4(tim));
    }
}
// Systick subroutines
void feedback (uint16_t time)
{
	static uint16_t ticker = 0;
	if ((uint16_t)(ticker+time)==GetTicker()) 
	{
		ticker = GetTicker();
		
		fb_LFL_2 = +((int16_t)TIM_GetCapture1(TIM5)-phi0_LFL_2)/angle_step;
		fb_LFL_1 = -((int16_t)TIM_GetCapture2(TIM5)-phi0_LFL_1)/angle_step;
		fb_LFL_0 = -((int16_t)TIM_GetCapture3(TIM5)-phi0_LFL_0)/angle_step;
		
		fb_LHL_2 = -((int16_t)TIM_GetCapture2(TIM2)-phi0_LHL_2)/angle_step;
		fb_LHL_1 = +((int16_t)TIM_GetCapture3(TIM2)-phi0_LHL_1)/angle_step;
		fb_LHL_0 = -((int16_t)TIM_GetCapture4(TIM2)-phi0_LHL_0)/angle_step;
		
		fb_RFL_2 = -((int16_t)TIM_GetCapture1(TIM3)-phi0_RFL_2)/angle_step;
		fb_RFL_1 = +((int16_t)TIM_GetCapture2(TIM3)-phi0_RFL_1)/angle_step;
		fb_RFL_0 = -((int16_t)TIM_GetCapture3(TIM3)-phi0_RFL_0)/angle_step;
		
		fb_RHL_2 = +((int16_t)TIM_GetCapture1(TIM4)-phi0_RHL_2)/angle_step;
		fb_RHL_1 = -((int16_t)TIM_GetCapture2(TIM4)-phi0_RHL_1)/angle_step;
		fb_RHL_0 = -((int16_t)TIM_GetCapture3(TIM4)-phi0_RHL_0)/angle_step;
	}
}
示例#10
0
uint16_t HAL_Servo_Read_Pulse_Width(uint16_t pin)
{
  uint16_t SERVO_TIM_CCR = 0x0000;

  if(PIN_MAP[pin].timer_ch == TIM_Channel_1)
  {
    SERVO_TIM_CCR = TIM_GetCapture1(PIN_MAP[pin].timer_peripheral);
  }
  else if(PIN_MAP[pin].timer_ch == TIM_Channel_2)
  {
    SERVO_TIM_CCR = TIM_GetCapture2(PIN_MAP[pin].timer_peripheral);
  }
  else if(PIN_MAP[pin].timer_ch == TIM_Channel_3)
  {
    SERVO_TIM_CCR = TIM_GetCapture3(PIN_MAP[pin].timer_peripheral);
  }
  else if(PIN_MAP[pin].timer_ch == TIM_Channel_4)
  {
    SERVO_TIM_CCR = TIM_GetCapture4(PIN_MAP[pin].timer_peripheral);
  }

  //pulseWidth = (SERVO_TIM_CCR * 1000000) / ((SERVO_TIM_ARR + 1) * SERVO_TIM_PWM_FREQ);
  return SERVO_TIM_CCR;
}
/*********************************************************************************
  *Function      : void TIM2_Tone_Interrupt_Handler(void)
  *Description  : set duty cycle
  *Input          : none
  *Output        : none
  *Return        : none
  *author        : lz
  *date           : 6-December-2013
  *Others        :         
**********************************************************************************/
void TIM2_Tone_Interrupt_Handler(void)
{
    uint16_t capture;

    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );
        capture = TIM_GetCapture1(TIM2);
        TIM_SetCompare1(TIM2, capture + PIN_MAP[30].timer_ccr);
        if(PIN_MAP[30].user_property != -1)
        {
            if (PIN_MAP[30].user_property > 0)
            {
                PIN_MAP[30].user_property -= 1;
            }
            else
            {
                noTone(30);
            }
        }
    }

    if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
        capture = TIM_GetCapture2(TIM2);
        TIM_SetCompare2(TIM2, capture + PIN_MAP[31].timer_ccr);
        if(PIN_MAP[31].user_property != -1)
        {
            if (PIN_MAP[31].user_property > 0)
            {
                PIN_MAP[31].user_property -= 1;
            }
            else
            {
                noTone(31);
            }
        }
    }

    if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
        capture = TIM_GetCapture3(TIM2);
        TIM_SetCompare3(TIM2, capture + PIN_MAP[33].timer_ccr);
        if(PIN_MAP[33].user_property != -1)
        {
            if (PIN_MAP[33].user_property > 0)
            {
                PIN_MAP[33].user_property -= 1;
            }
            else
            {
                noTone(33);
            }
        }
    }

    if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
        capture = TIM_GetCapture4(TIM2);
        TIM_SetCompare4(TIM2, capture + PIN_MAP[32].timer_ccr);
        if(PIN_MAP[32].user_property != -1)
        {
            if (PIN_MAP[32].user_property > 0)
            {
                PIN_MAP[32].user_property -= 1;
            }
            else
            {
                noTone(32);
            }
        }
    }
}
void TIM3_IRQHandler(void){
    if(TIM_GetITStatus(TIM3,TIM_IT_CC1)==SET){
        if(CH1CaptureNumber == 0){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH1ReadValue1=TIM_GetCapture1(TIM3);
            CH1CaptureNumber=1;

        }
        else if(CH1CaptureNumber ==1){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH1ReadValue2=TIM_GetCapture1(TIM3);
            CH1Distance[0]=CH1Distance[1];
            if(IC3CH1ReadValue2>IC3CH1ReadValue1){
                CH1Distance[1] = (IC3CH1ReadValue2 - IC3CH1ReadValue1); 
            }
            else{
                CH1Distance[1] = (0xFFFF-IC3CH1ReadValue1) + IC3CH1ReadValue2; 
            }
            CH1Distance[1]/=58;
            CH1CaptureNumber=0;

        }
        //TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
    }
    else if(TIM_GetITStatus(TIM3,TIM_IT_CC2)==SET){
        if(CH2CaptureNumber == 0){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH2ReadValue1=TIM_GetCapture2(TIM3);
            CH2CaptureNumber=1;

        }
        else if(CH2CaptureNumber ==1){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH2ReadValue2=TIM_GetCapture2(TIM3);
            CH2Distance[0]=CH2Distance[1];
            if(IC3CH2ReadValue2>IC3CH2ReadValue1){
                CH2Distance[1]= (IC3CH2ReadValue2 - IC3CH2ReadValue1); 
            }
            else{
                CH2Distance[1] = (0xFFFF-IC3CH2ReadValue1) + IC3CH2ReadValue2; 
            }
            CH2Distance[1]/=58;
            CH2CaptureNumber=0;

        }

        //TIM_ITConfig(TIM3, TIM_IT_CC2, DISABLE);
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    }
    else if(TIM_GetITStatus(TIM3,TIM_IT_CC3)==SET){
        if(CH3CaptureNumber == 0){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH3ReadValue1=TIM_GetCapture3(TIM3);
            CH3CaptureNumber=1;

        }
        else if(CH3CaptureNumber ==1){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH3ReadValue2=TIM_GetCapture3(TIM3);
            CH3Distance[0]=CH3Distance[1];
            if(IC3CH3ReadValue2>IC3CH3ReadValue1){
                CH3Distance[1]= (IC3CH3ReadValue2 - IC3CH3ReadValue1); 
            }
            else{
                CH3Distance[1] = (0xFFFF-IC3CH3ReadValue1) + IC3CH3ReadValue2; 
            }
            CH3Distance[1]/=58;
            CH3CaptureNumber=0;

        }
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    }
    else if(TIM_GetITStatus(TIM3,TIM_IT_CC4)==SET){
        if(CH4CaptureNumber == 0){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH4ReadValue1=TIM_GetCapture4(TIM3);
            CH4CaptureNumber=1;

        }
        else if(CH4CaptureNumber ==1){
            TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
            TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
            TIM_ICInit(TIM3,&TIM_ICInitStructure);
            IC3CH4ReadValue2=TIM_GetCapture4(TIM3);
            CH4Distance[0]=CH4Distance[1];
            if(IC3CH4ReadValue2>IC3CH4ReadValue1){
                CH4Distance[1]= (IC3CH4ReadValue2 - IC3CH4ReadValue1); 
            }
            else{
                CH4Distance[1] = (0xFFFF-IC3CH4ReadValue1) + IC3CH4ReadValue2; 
            }
            CH4Distance[1]/=58;
            CH4CaptureNumber=0;

        }
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
    }



}
示例#13
0
void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1)
		{
			//CC1P=2 设置为下降沿捕获
			TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Falling);
			pwm_rise[0] = TIM_GetCapture1(TIM2);
		}
		else
		{
			//CC1P=0 设置为上升沿捕获
			TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Rising);
			pwm_drop[0] = TIM_GetCapture1(TIM2);
			if (pwm_rise[0] > pwm_drop[0])
			{
				pwm_in[0] = 65535 - pwm_rise[0] + pwm_drop[0];
			}
			else
			{
				pwm_in[0] = pwm_drop[0] - pwm_rise[0];
			}
		}
	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1)
		{
			TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Falling);
			pwm_rise[1] = TIM_GetCapture2(TIM2);
		}
		else
		{
			TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Rising);
			pwm_drop[1] = TIM_GetCapture2(TIM2);
			if (pwm_rise[1] > pwm_drop[1])
			{
				pwm_in[1] = 65535 - pwm_rise[1] + pwm_drop[1];
			}
			else
			{
				pwm_in[1] = pwm_drop[1] - pwm_rise[1];
			}
		}
	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 1)
		{
			TIM_OC3PolarityConfig(TIM2, TIM_ICPolarity_Falling);
			pwm_rise[2] = TIM_GetCapture3(TIM2);
		}
		else
		{
			TIM_OC3PolarityConfig(TIM2, TIM_ICPolarity_Rising);
			pwm_drop[2] = TIM_GetCapture3(TIM2);
			if (pwm_rise[2] > pwm_drop[2])
			{
				pwm_in[2] = 65535 - pwm_rise[2] + pwm_drop[2];
			}
			else
			{
				pwm_in[2] = pwm_drop[2] - pwm_rise[2];
			}
		}
	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1)
		{
			TIM_OC4PolarityConfig(TIM2, TIM_ICPolarity_Falling);
			pwm_rise[3] = TIM_GetCapture4(TIM2);
		}
		else
		{
			TIM_OC4PolarityConfig(TIM2, TIM_ICPolarity_Rising);
			pwm_drop[3] = TIM_GetCapture4(TIM2);
			if (pwm_rise[3] > pwm_drop[3])
			{
				pwm_in[3] = 65535 - pwm_rise[3] + pwm_drop[3];
			}
			else
			{
				pwm_in[3] = pwm_drop[3] - pwm_rise[3];
			}
			pwm_in_error_count = 0;
		}
	}
}
示例#14
0
void TIMER_INTERRUPT_FCN(void)
{
  #if TIMEBASE_TIMER == 6 || TIMEBASE_TIMER == 7 || TIMEBASE_TIMER == 18
  if (TIM_GetITStatus(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_Update);
    timerCallback1();
  }
  #endif //TIMEBASE_TIMER == 6 || TIMEBASE_TIMER == 7 || TIMEBASE_TIMER == 18
  
  #if TIMEBASE_TIMER != 6 && TIMEBASE_TIMER != 7 && TIMEBASE_TIMER != 18
  #ifdef TIMEBASE_SIZE
  TIMEBASE_SIZE capture_val = 0;
  TIMEBASE_SIZE next_capture_val = 0;
  TIMEBASE_SIZE clock_val = 0;
  TIMEBASE_SIZE gap_val = 0;
  #endif
  
  if (TIM_GetITStatus(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC1);
    timerCallback1();
	  capture_val = TIM_GetCapture1(MAKENAME(TIM,TIMEBASE_TIMER));
    clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    gap_val = (TIMEBASE_SIZE) (SystemCoreClock/TIMEBASE_CLOCK_PSC  / TIMEBASE_INT1_FREQ - 1);
    next_capture_val = capture_val + gap_val;
    while( (((clock_val > next_capture_val) || (clock_val < capture_val)) && (capture_val < next_capture_val)) || ((clock_val > next_capture_val) && (clock_val < capture_val)) )
    {
      //increment capture and next_capture by gap_val
      capture_val += gap_val;
      next_capture_val = capture_val + gap_val;
      clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    }
    TIM_SetCompare1(MAKENAME(TIM,TIMEBASE_TIMER), next_capture_val);
  }
  #if defined(TIMEBASE_INT2_FREQ) && TIMEBASE_TIMER != 13 && TIMEBASE_TIMER != 14 && TIMEBASE_TIMER != 16 && TIMEBASE_TIMER != 17
  if (TIM_GetITStatus(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC2);
    timerCallback2();
    capture_val = TIM_GetCapture2(MAKENAME(TIM,TIMEBASE_TIMER));
    clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    gap_val = (TIMEBASE_SIZE) (SystemCoreClock/TIMEBASE_CLOCK_PSC  / TIMEBASE_INT2_FREQ - 1);
    next_capture_val = capture_val + gap_val;
    while( (((clock_val > next_capture_val) || (clock_val < capture_val)) && (capture_val < next_capture_val)) || ((clock_val > next_capture_val) && (clock_val < capture_val)) )
    {
      //increment capture and next_capture by gap_val
      capture_val += gap_val;
      next_capture_val = capture_val + gap_val;
      clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    }
    TIM_SetCompare2(MAKENAME(TIM,TIMEBASE_TIMER), next_capture_val);
  }
  #if defined(TIMEBASE_INT3_FREQ) && TIMEBASE_TIMER != 12 && TIMEBASE_TIMER != 15
  if (TIM_GetITStatus(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC3);
    timerCallback3();
    capture_val = TIM_GetCapture3(MAKENAME(TIM,TIMEBASE_TIMER));
    clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    gap_val = (TIMEBASE_SIZE) (SystemCoreClock/TIMEBASE_CLOCK_PSC  / TIMEBASE_INT3_FREQ - 1);
    next_capture_val = capture_val + gap_val;
    while( (((clock_val > next_capture_val) || (clock_val < capture_val)) && (capture_val < next_capture_val)) || ((clock_val > next_capture_val) && (clock_val < capture_val)) )
    {
      //increment capture and next_capture by gap_val
      capture_val += gap_val;
      next_capture_val = capture_val + gap_val;
      clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    }
    TIM_SetCompare3(MAKENAME(TIM,TIMEBASE_TIMER), next_capture_val);
  }
  #ifdef TIMEBASE_INT4_FREQ
  if (TIM_GetITStatus(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC4) != RESET)
  {
    TIM_ClearITPendingBit(MAKENAME(TIM,TIMEBASE_TIMER), TIM_IT_CC4);
    timerCallback4();
    capture_val = TIM_GetCapture4(MAKENAME(TIM,TIMEBASE_TIMER));
    clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    gap_val = (TIMEBASE_SIZE) (SystemCoreClock/TIMEBASE_CLOCK_PSC  / TIMEBASE_INT4_FREQ - 1);
    next_capture_val = capture_val + gap_val;
    while( (((clock_val > next_capture_val) || (clock_val < capture_val)) && (capture_val < next_capture_val)) || ((clock_val > next_capture_val) && (clock_val < capture_val)) )
    {
      //increment capture and next_capture by gap_val
      capture_val += gap_val;
      next_capture_val = capture_val + gap_val;
      clock_val = TIM_GetCounter(MAKENAME(TIM,TIMEBASE_TIMER));
    }
    TIM_SetCompare4(MAKENAME(TIM,TIMEBASE_TIMER), next_capture_val);
  }
  #endif //TIMEBASE_INT4_FREQ
  #endif //defined(TIMEBASE_INT3_FREQ) && TIMEBASE_TIMER != 12 && TIMEBASE_TIMER != 15
  #endif //defined(TIMEBASE_INT2_FREQ) && TIMEBASE_TIMER != 13 && TIMEBASE_TIMER != 14 && TIMEBASE_TIMER != 16 && TIMEBASE_TIMER != 17
  #endif //TIMEBASE_TIMER != 6 || TIMEBASE_TIMER != 7 || TIMEBASE_TIMER != 18

}
示例#15
0
void TIM3_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)   //捕获1发生捕获事件
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
		{
			TIM_OC1PolarityConfig(TIM3, TIM_ICPolarity_Falling);
			pwm_rise[4] = TIM_GetCapture1(TIM3);
		}
		else
		{
			TIM_OC1PolarityConfig(TIM3, TIM_ICPolarity_Rising);
			pwm_drop[4] = TIM_GetCapture1(TIM3);
			if (pwm_rise[4] > pwm_drop[4])
			{
				pwm_in[4] = 65535 - pwm_rise[4] + pwm_drop[4];
			}
			else
			{
				pwm_in[4] = pwm_drop[4] - pwm_rise[4];
			}
		}
	}

	if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
		{
			TIM_OC2PolarityConfig(TIM3, TIM_ICPolarity_Falling);
			pwm_rise[5] = TIM_GetCapture2(TIM3);
		}
		else
		{
			TIM_OC2PolarityConfig(TIM3, TIM_ICPolarity_Rising);
			pwm_drop[5] = TIM_GetCapture2(TIM3);
			if (pwm_rise[5] > pwm_drop[5])
			{
				pwm_in[5] = 65535 - pwm_rise[5] + pwm_drop[5];
			}
			else
			{
				pwm_in[5] = pwm_drop[5] - pwm_rise[5];
			}
		}
	}

	if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 1)
		{
			TIM_OC3PolarityConfig(TIM3, TIM_ICPolarity_Falling);
			pwm_rise[6] = TIM_GetCapture3(TIM3);
		}
		else
		{
			TIM_OC3PolarityConfig(TIM3, TIM_ICPolarity_Rising);
			pwm_drop[6] = TIM_GetCapture3(TIM3);
			if (pwm_rise[6] > pwm_drop[6])
			{
				pwm_in[6] = 65535 - pwm_rise[6] + pwm_drop[6];
			}
			else
			{
				pwm_in[6] = pwm_drop[6] - pwm_rise[6];
			}
		}
	}

	if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 1)
		{
			TIM_OC4PolarityConfig(TIM3, TIM_ICPolarity_Falling);
			pwm_rise[7] = TIM_GetCapture4(TIM3);
		}
		else
		{
			TIM_OC4PolarityConfig(TIM3, TIM_ICPolarity_Rising);
			pwm_drop[7] = TIM_GetCapture4(TIM3);
			if (pwm_rise[7] > pwm_drop[7])
			{
				pwm_in[7] = 65535 - pwm_rise[7] + pwm_drop[7];
			}
			else
			{
				pwm_in[7] = pwm_drop[7] - pwm_rise[7];
			}
		}
	}
}
//定时器4中断服务程序,用CH1作为超声波转换的通道 即PB6端口
void TIM4_IRQHandler(void)
{
	if ((TIM4CH1_CAPTURE_STA & 0X80) == 0) 		//还未成功捕获	
	{
		if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) 		//捕获1发生捕获事件
		{
			TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); 		//清除中断标志位
			if (TIM4CH1_CAPTURE_STA & 0X40)		//捕获到一个下降沿
			{
				TIM4CH1_CAPTURE_DOWNVAL = TIM_GetCapture1(TIM4);//记录下此时的定时器计数值
				if (TIM4CH1_CAPTURE_DOWNVAL < TIM4CH1_CAPTURE_UPVAL)
				{/* 如果计数器初始值大于末尾值,说明计数器有溢出 */
					tim4_T1 = 65535;
				}
				else
				tim4_T1 = 0;  
				tempup1 = TIM4CH1_CAPTURE_DOWNVAL - TIM4CH1_CAPTURE_UPVAL
						+ tim4_T1;		//得到总的高电平的时间
				//pwmout1 = tempup1;		//总的高电平的时间
				tempup1 =tempup1 *17/1000;//计算距离&&UltrasonicWave_Distance<85
			
				
				
				TIM4CH1_CAPTURE_STA = 0;		//捕获标志位清零,这一步很重要!
				TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获		  
			}
			else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
			{
				TIM4CH1_CAPTURE_UPVAL = TIM_GetCapture1(TIM4);		//获取上升沿数据
				TIM4CH1_CAPTURE_STA |= 0X40;		//标记已捕获到上升沿
				TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Falling);//设置为下降沿捕获
			}
		}
	}

	if ((TIM4CH2_CAPTURE_STA & 0X80) == 0)		//还未成功捕获	
	{
		if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)		//捕获2发生捕获事件
		{
			TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);		//清除中断标志位
			if (TIM4CH2_CAPTURE_STA & 0X40)		//捕获到一个下降沿
			{
				TIM4CH2_CAPTURE_DOWNVAL = TIM_GetCapture2(TIM4);//记录下此时的定时器计数值
				if (TIM4CH2_CAPTURE_DOWNVAL < TIM4CH2_CAPTURE_UPVAL)
				{
					tim4_T2 = 65535;
				}
				else
					tim4_T2 = 0;
				tempup2 = TIM4CH2_CAPTURE_DOWNVAL - TIM4CH2_CAPTURE_UPVAL
						+ tim4_T2;		//得到总的高电平的时间
				pwmout2 = tempup2;		//总的高电平的时间
				TIM4CH2_CAPTURE_STA = 0;		//捕获标志位清零
				TIM_OC2PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获		  
			}
			else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
			{
				TIM4CH2_CAPTURE_UPVAL = TIM_GetCapture2(TIM4);		//获取上升沿数据
				TIM4CH2_CAPTURE_STA |= 0X40;		//标记已捕获到上升沿
				TIM_OC2PolarityConfig(TIM4, TIM_ICPolarity_Falling);//设置为下降沿捕获
			}
		}
	}

	if ((TIM4CH3_CAPTURE_STA & 0X80) == 0)		//还未成功捕获	
	{
		if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)		//捕获3发生捕获事件
		{
			TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);		//清除中断标志位
			if (TIM4CH3_CAPTURE_STA & 0X40)		//捕获到一个下降沿
			{
				TIM4CH3_CAPTURE_DOWNVAL = TIM_GetCapture3(TIM4);//记录下此时的定时器计数值
				if (TIM4CH3_CAPTURE_DOWNVAL < TIM4CH3_CAPTURE_UPVAL)
				{
					tim4_T3 = 65535;
				}
				else
					tim4_T3 = 0;
				tempup3 = TIM4CH3_CAPTURE_DOWNVAL - TIM4CH3_CAPTURE_UPVAL
						+ tim4_T3;		//得到总的高电平的时间
				pwmout3 = tempup3;		//总的高电平的时间
				TIM4CH3_CAPTURE_STA = 0;		//捕获标志位清零
				TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获		  
			}
			else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
			{
				TIM4CH3_CAPTURE_UPVAL = TIM_GetCapture3(TIM4);		//获取上升沿数据
				TIM4CH3_CAPTURE_STA |= 0X40;		//标记已捕获到上升沿
				TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Falling);//设置为下降沿捕获
			}
		}
	}

	if ((TIM4CH4_CAPTURE_STA & 0X80) == 0)		//还未成功捕获	
	{
		if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)		//捕获4发生捕获事件
		{
			TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);		//清除中断标志位
			if (TIM4CH4_CAPTURE_STA & 0X40)		//捕获到一个下降沿
			{
				TIM4CH4_CAPTURE_DOWNVAL = TIM_GetCapture4(TIM4);//记录下此时的定时器计数值
				if (TIM4CH4_CAPTURE_DOWNVAL < TIM4CH4_CAPTURE_UPVAL)
				{
					tim4_T4 = 65535;
				}
				else
					tim4_T4 = 0;
				tempup4 = TIM4CH4_CAPTURE_DOWNVAL - TIM4CH4_CAPTURE_UPVAL
						+ tim4_T4;		//得到总的高电平的时间
				pwmout4 = tempup4;		//总的高电平的时间
				TIM4CH4_CAPTURE_STA = 0;		//捕获标志位清零
				TIM_OC4PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获		  
			}
			else //发生捕获时间但不是下降沿,第一次捕获到上升沿,记录此时的定时器计数值
			{
				TIM4CH4_CAPTURE_UPVAL = TIM_GetCapture4(TIM4);		//获取上升沿数据
				TIM4CH4_CAPTURE_STA |= 0X40;		//标记已捕获到上升沿
				TIM_OC4PolarityConfig(TIM4, TIM_ICPolarity_Falling);//设置为下降沿捕获
			}
		}
	}
}
示例#17
0
文件: pios_tim.c 项目: Gussy/TauLabs
static void PIOS_TIM_generic_irq_handler(TIM_TypeDef * timer)
{
	/* Iterate over all registered clients of the TIM layer to find channels on this timer */
	for (uint8_t i = 0; i < pios_tim_num_devs; i++) {
		const struct pios_tim_dev * tim_dev = &pios_tim_devs[i];

		if (!tim_dev->channels || tim_dev->num_channels == 0) {
			/* No channels to process on this client */
			continue;
		}

		/* Check for an overflow event on this timer */
		bool overflow_event;
		uint16_t overflow_count;
		if (TIM_GetITStatus(timer, TIM_IT_Update) == SET) {
			TIM_ClearITPendingBit(timer, TIM_IT_Update);
			overflow_count = timer->ARR;
			overflow_event = true;
		} else {
			overflow_count = 0;
			overflow_event = false;
		}

		for (uint8_t j = 0; j < tim_dev->num_channels; j++) {
			const struct pios_tim_channel * chan = &tim_dev->channels[j];

			if (chan->timer != timer) {
				/* channel is not on this timer */
				continue;
			}

			/* Figure out which interrupt bit we should be looking at */
			uint16_t timer_it;
			switch (chan->timer_chan) {
			case TIM_Channel_1:
				timer_it = TIM_IT_CC1;
				break;
			case TIM_Channel_2:
				timer_it = TIM_IT_CC2;
				break;
			case TIM_Channel_3:
				timer_it = TIM_IT_CC3;
				break;
			case TIM_Channel_4:
				timer_it = TIM_IT_CC4;
				break;
			default:
				PIOS_Assert(0);
				break;
			}

			bool edge_event;
			uint16_t edge_count;
			if (TIM_GetITStatus(chan->timer, timer_it) == SET) {
				TIM_ClearITPendingBit(chan->timer, timer_it);

				/* Read the current counter */
				switch(chan->timer_chan) {
				case TIM_Channel_1:
					edge_count = TIM_GetCapture1(chan->timer);
					break;
				case TIM_Channel_2:
					edge_count = TIM_GetCapture2(chan->timer);
					break;
				case TIM_Channel_3:
					edge_count = TIM_GetCapture3(chan->timer);
					break;
				case TIM_Channel_4:
					edge_count = TIM_GetCapture4(chan->timer);
					break;
				default:
					PIOS_Assert(0);
					break;
				}
				edge_event = true;
			} else {
				edge_event = false;
				edge_count = 0;
			}

			if (!tim_dev->callbacks) {
				/* No callbacks registered, we're done with this channel */
				continue;
			}

			/* Generate the appropriate callbacks */
			if (overflow_event & edge_event) {
				/*
				 * When both edge and overflow happen in the same interrupt, we
				 * need a heuristic to determine the order of the edge and overflow
				 * events so that the callbacks happen in the right order.  If we
				 * get the order wrong, our pulse width calculations could be off by up
				 * to ARR ticks.  That could be bad.
				 *
				 * Heuristic: If the edge_count is < 16 ticks above zero then we assume the
				 *            edge happened just after the overflow.
				 */

				if (edge_count < 16) {
					/* Call the overflow callback first */
					if (tim_dev->callbacks->overflow) {
						(*tim_dev->callbacks->overflow)((uintptr_t)tim_dev,
									tim_dev->context,
									j,
									overflow_count);
					}
					/* Call the edge callback second */
					if (tim_dev->callbacks->edge) {
						(*tim_dev->callbacks->edge)((uintptr_t)tim_dev,
									tim_dev->context,
									j,
									edge_count);
					}
				} else {
					/* Call the edge callback first */
					if (tim_dev->callbacks->edge) {
						(*tim_dev->callbacks->edge)((uintptr_t)tim_dev,
									tim_dev->context,
									j,
									edge_count);
					}
					/* Call the overflow callback second */
					if (tim_dev->callbacks->overflow) {
						(*tim_dev->callbacks->overflow)((uintptr_t)tim_dev,
									tim_dev->context,
									j,
									overflow_count);
					}
				}
			} else if (overflow_event && tim_dev->callbacks->overflow) {
				(*tim_dev->callbacks->overflow)((uintptr_t)tim_dev,
								tim_dev->context,
								j,
								overflow_count);
			} else if (edge_event && tim_dev->callbacks->edge) {
				(*tim_dev->callbacks->edge)((uintptr_t)tim_dev,
							tim_dev->context,
							j,
							edge_count);
			}
		}
	}
}
示例#18
0
static void pwmIRQHandler(TIM_TypeDef *tim)
{
    uint8_t i;
    uint16_t val = 0;

    for (i = 0; i < 8; i++) {
        struct TIM_Channel channel = Channels[i];
        struct PWM_State *state = &Inputs[i];

        if (channel.tim == tim && (TIM_GetITStatus(tim, channel.cc) == SET)) {
            TIM_ClearITPendingBit(channel.tim, channel.cc);
            if (i == 0)
                rcActive = true;

            switch (channel.channel) {
                case TIM_Channel_1:
                    val = TIM_GetCapture1(channel.tim);
                    break;
                case TIM_Channel_2:
                    val = TIM_GetCapture2(channel.tim);
                    break;
                case TIM_Channel_3:
                    val = TIM_GetCapture3(channel.tim);
                    break;
                case TIM_Channel_4:
                    val = TIM_GetCapture4(channel.tim);
                    break;
            }

            if (state->state == 0)
                state->rise = val;
            else
                state->fall = val;

            if (state->state == 0) {
                // switch states
                state->state = 1;

                TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
                TIM_ICInitStructure.TIM_Channel = channel.channel;
                TIM_ICInit(channel.tim, &TIM_ICInitStructure);
            } else {
                // compute capture
                if (state->fall > state->rise)
                    state->capture = (state->fall - state->rise);
                else
                    state->capture = ((0xffff - state->rise) + state->fall);

                // switch state
                state->state = 0;

                // ping failsafe
                failsafeCnt = 0;

                TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
                TIM_ICInitStructure.TIM_Channel = channel.channel;
                TIM_ICInit(channel.tim, &TIM_ICInitStructure);
            }
        }
    }
}
示例#19
0
/**************************************************************** 
*	函数名称: 
*	功    能: 2015-4-28新编中断服务函数。
*						2016-06-08 lea比较其开始后捕获第二次中断为相位起始和结束标志
*						当A相电压大于100V,在开启了比较器、输入捕获之后,捕获到A相经过比较器之后的上升沿,则定时器开始计数。
						关闭比较器,使无比较输出,即无捕获输入。
*						这时,同时需要判断再次检测到的电压要大于100V,再开启捕获功能,记录下再次捕获到的另一相的上升沿时的计数值。
*						供相序判断。
*	参    数: 无 
*	返 回 值: 无  
*****************************************************************/
void TIM5_IRQHandler(void)
{
	if(phasestatus != state0)//相序测量状态下比较用于触发相序的时间间隔测量
	{
		if((TIM5CH4_CAPTURE_STA&0x8000)==0)//还未成功捕获A相与B相orC相之间的时间
		{
//			if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
//			{
//				if(TIM5CH4_CAPTURE_STA&0x4000)//已经捕获到A相的上升沿了
//				{
//					if((TIM5CH4_CAPTURE_STA&0x3FFF)==0x3FFF)//两次捕获A相与B相或C相的时间太长了,0x3F=4.194304s(1MHz计数0xFFFF次,再累加0x3F次)
//					{
//						TIM5CH4_CAPTURE_STA|=0x8000;//标记成功捕获了一次
//						TIM5CH4_CAPTURE_VAL=0xFFFF;
//	//					COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器
//					}
//					else TIM5CH4_CAPTURE_STA++;
//				}
//			}
			if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)//捕获4发生捕获事件
			{
				if(TIM5CH4_CAPTURE_STA&0x4000)		//已经捕获到一次,再次捕获到一个上升沿 		
				{				
					/**************测试两次读取判断第二相线频率脉冲计数个数*/
					//20160607lea 两相线上的起始脉冲均从第二个开始,因为刚打开比较器时若此时输入脉冲处于正半周则会立即触发输入捕捉中断
					//所以从第二个脉冲开始判定。相序测量功能 相位差的测量基本正确。
					if(capstats==0)
					{						
						capstats=1;
					}
					else if(capstats==1)
					{
						TIM2_cap1 = TIM2->CNT;
						TIM5_cap1 = TIM_GetCapture4(TIM5);
						capstats=2;
					}
					else
					{
						TIM5CH4_CAPTURE_VAL = TIM_GetCapture4(TIM5);
						TIM5_cap2 = TIM5CH4_CAPTURE_VAL;
						TIM_Cmd(TIM5, DISABLE);
						
						TIM2_cap2 = TIM2->CNT;//TIM_GetCapture4(TIM2);
						TIM_Cmd(TIM2, DISABLE);
						TIM5CH4_CAPTURE_STA|=0x8000;		//标记成功捕获到B或C相的上升沿
						COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器
		
						TIM_Cmd(TIM14, DISABLE);//0.5s定时关闭
						
						TIM5_cap3=TIM5_cap2-TIM5_cap1;
						//一个周期的计数个数 用于对总计数个数取余后求相位差,确定第二相相序
						CCR4_Val = TIM5_cap3;
						
						TIM2_cap3=TIM2_cap2-TIM2_cap1;
						capstats=0;
						//BUZZER_Open(0);//蜂鸣一声						
					}
					/****************/
				}
				else  								//已经检测到A相电压大于100V,开始比较器和定时器后,第一次捕获上升沿
				{
					if(capstats==0)
					{
						capstats=1;
					}
					else
					{
						TIM5CH4_CAPTURE_STA=0;			//清空,此标志除了用来0x4000,0x8000做A、B/C两次的标记外,还用来定时器溢出计时
						TIM5CH4_CAPTURE_VAL=0;
						TIM_SetCounter(TIM5,0);
						TIM_SetCounter(TIM2,0);
						TIM5CH4_CAPTURE_STA|=0x4000;		//标记捕获到了A相经过比较器之后的上升沿
						
						TIM_Cmd(TIM5, ENABLE);//开启定时器计数
						
						TIM_Cmd(TIM2, ENABLE);//测试 在捕捉到A相上升沿后打开,输出一个脉冲对比待测的相位差
	 
						COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器,使无比较输出,即无捕获输入。等待下次检测到100V以上电压,再开启
						BUZZER_Open(0);//蜂鸣一声
						capstats=0;
					}
					
				}
			}
		}
	}
	else//正常模式下,比较器用于过零点触发交流的采样
	{
		if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET) 
		{
			/* Clear TIM2 Capture compare interrupt pending bit */
			TIM_ClearITPendingBit(TIM5, TIM_IT_CC4);			
			
			if(VadENA!=1)
			{
				VadENA=1;
				TIM_Cmd(TIM19, ENABLE);
				//printf("*converting!*\r\n");
			}
				
		}

	}
		

	TIM_ClearITPendingBit(TIM5, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
}
/*********************************************************************************
  *Function      : void TIM3_Tone_Interrupt_Handler(void)
  *Description  : set duty cycle
  *Input          : none
  *Output        : none
  *Return        : none
  *author        : lz
  *date           : 6-December-2013
  *Others        :         
**********************************************************************************/
void TIM3_Tone_Interrupt_Handler(void)
{
    uint16_t capture;

    if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
        capture = TIM_GetCapture1(TIM3);
        TIM_SetCompare1(TIM3, capture + PIN_MAP[36].timer_ccr);
        if(PIN_MAP[36].user_property != -1)
        {
            if (PIN_MAP[36].user_property > 0)
            {
                PIN_MAP[36].user_property -= 1;
            }
            else
            {
                noTone(36);
            }
        }
    }

    if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
        capture = TIM_GetCapture2(TIM3);
        TIM_SetCompare2(TIM3, capture + PIN_MAP[37].timer_ccr);
        if(PIN_MAP[37].user_property != -1)
        {
            if (PIN_MAP[37].user_property > 0)
            {
                PIN_MAP[37].user_property -= 1;
            }
            else
            {
                noTone(37);
            }
        }
    }

    if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
        capture = TIM_GetCapture3(TIM3);
        TIM_SetCompare3(TIM3, capture + PIN_MAP[38].timer_ccr);
        if(PIN_MAP[38].user_property != -1)
        {
            if (PIN_MAP[38].user_property > 0)
            {
                PIN_MAP[38].user_property -= 1;
            }
            else
            {
                noTone(38);
            }
        }
    }

    if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
        capture = TIM_GetCapture4(TIM3);
        TIM_SetCompare4(TIM3, capture + PIN_MAP[39].timer_ccr);
        if(PIN_MAP[39].user_property != -1)
        {
            if (PIN_MAP[39].user_property > 0)
            {
                PIN_MAP[39].user_property -= 1;
            }
            else
            {
                noTone(39);
            }
        }
    }
}
示例#21
0
void TIM3_IRQHandler()
{
	
//	int i;
//	i++;
//	TIM_ClearITPendingBit(TIM3, TIM3_IRQn);
//	TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
//	TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
//	TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
//	TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
//	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1))
//	{i++;}
//	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2))
//	{i++;}
//	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC3))
//	{i++;}
//	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4))
//	{i++;}
	
	if (TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1))//проверяем что прерывание вызвал канал 1
	{
		if ( TIM3->CCER & TIM_CCER_CC1P)
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_FallCounter[CH_1]== MEASURE_BUFFER - 1)
				modCounter_FallCounter[CH_1]=0;
			else
				modCounter_FallCounter[CH_1]++;

			modCounter_Falls[CH_1][modCounter_FallCounter[CH_1]] = TIM_GetCapture1(TIM3);//записываем значение захвата
			
			modCounter_WritePending |= CH_1_DOWN;//указываем что произошел новый спад
			TIM3->CCER &= ~(TIM_CCER_CC1P);// перенастройка канала на захват по нарастающему фронту
		}
		else
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_RiseCounter[CH_1]== MEASURE_BUFFER -1)
				modCounter_RiseCounter[CH_1]=0;
			else
				modCounter_RiseCounter[CH_1]++;
			
			modCounter_Rises[CH_1][modCounter_RiseCounter[CH_1]] = TIM_GetCapture1(TIM3);//записываем значение захвата

			modCounter_WritePending |= CH_1_UP;//указываем что произошел подьем
			TIM3->CCER |= TIM_CCER_CC1P;//перенастройка канала на захват по спадающему фронту
		}
		TIM_ClearITPendingBit(TIM3, TIM_Channel_1);
	}
	
	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2))//проверяем что прерывание вызвал канал 2
	{
		if ( TIM3->CCER & TIM_CCER_CC2P)
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_FallCounter[CH_2]== MEASURE_BUFFER - 1)
				modCounter_FallCounter[CH_2]=0;
			else
				modCounter_FallCounter[CH_2]++;

			modCounter_Falls[CH_2][modCounter_FallCounter[CH_2]] = TIM_GetCapture2(TIM3);//записываем значение захвата
			
			modCounter_WritePending |= CH_2_DOWN;//указываем что произошел новый спад
			TIM3->CCER &= ~(TIM_CCER_CC2P);// перенастройка канала на захват по нарастающему фронту
		}
		else
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_RiseCounter[CH_2] == MEASURE_BUFFER -1)
				modCounter_RiseCounter[CH_2]=0;
			else
				modCounter_RiseCounter[CH_2]++;
			
			modCounter_Rises[CH_2][modCounter_RiseCounter[CH_2]] = TIM_GetCapture2(TIM3);//записываем значение захвата

			modCounter_WritePending |= CH_2_UP;//указываем что произошел подьем
			TIM3->CCER |= TIM_CCER_CC2P;//перенастройка канала на захват по спадающему фронту
		}
		TIM_ClearITPendingBit(TIM3, TIM_Channel_2);
	}
	
	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC3))//проверяем что прерывание вызвал канал 3
	{
		if ( TIM3->CCER & TIM_CCER_CC3P) //если канал 3 настроен на захват спадающего фронта 
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_FallCounter[CH_3]== MEASURE_BUFFER - 1)
				modCounter_FallCounter[CH_3]=0;
			else
				modCounter_FallCounter[CH_3]++;

			modCounter_Falls[CH_3][modCounter_FallCounter[CH_3]] = TIM_GetCapture3(TIM3);//записываем значение захвата
			
			modCounter_WritePending |= CH_3_DOWN;//указываем что произошел новый спад
			TIM3->CCER &= ~(TIM_CCER_CC3P);// перенастройка канала на захват по нарастающему фронту
		}
		else
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_RiseCounter[CH_3]== MEASURE_BUFFER -1)
				modCounter_RiseCounter[CH_3]=0;
			else
				modCounter_RiseCounter[CH_3]++;
			
			modCounter_Rises[CH_3][modCounter_RiseCounter[CH_3]] = TIM_GetCapture3(TIM3);//записываем значение захвата

			modCounter_WritePending |= CH_3_UP;//указываем что произошел подьем
			TIM3->CCER |= TIM_CCER_CC3P;//перенастройка канала на захват по спадающему фронту
		}
		TIM_ClearITPendingBit(TIM3, TIM_Channel_3);//сброс бита прерывания
		
	}
	if ( TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4))//проверяем что прерывание вызвал канал 3
	{
		if ( TIM3->CCER & TIM_CCER_CC4P) //если канал 3 настроен на захват спадающего фронта 
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_FallCounter[CH_4]== MEASURE_BUFFER - 1)
				modCounter_FallCounter[CH_4]=0;
			else
				modCounter_FallCounter[CH_4]++;

			modCounter_Falls[CH_4][modCounter_FallCounter[CH_4]] = TIM_GetCapture4(TIM3);//записываем значение захвата
			
			modCounter_WritePending |= CH_4_DOWN;//указываем что произошел новый спад
			TIM3->CCER &= ~(TIM_CCER_CC4P);// перенастройка канала на захват по нарастающему фронту
		}
		else
		{
			/*проверка что бы проходили циклически по записям массива фронтов*/
			if (modCounter_RiseCounter[CH_4]== MEASURE_BUFFER -1)
				modCounter_RiseCounter[CH_4]=0;
			else
				modCounter_RiseCounter[CH_4]++;
			
			modCounter_Rises[CH_4][modCounter_RiseCounter[CH_4]] = TIM_GetCapture4(TIM3);//записываем значение захвата

			modCounter_WritePending |= CH_4_UP;//указываем что произошел подьемs
			TIM3->CCER |= TIM_CCER_CC4P;//перенастройка канала на захват по спадающему фронту
		}
		TIM_ClearITPendingBit(TIM3, TIM_Channel_4);//сброс бита прерывания
		
	}
	
	
}
示例#22
0
文件: pwm_in.c 项目: Zasy/STM32_Demo
//定时器3中断服务程序	 
void TIM3_IRQHandler(void)
{   
 	if((TIM3CH1_CAPTURE_STA&0X80)==0)//CH1 还未成功捕获	
	{  		
		if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
		{	
			 TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

			if(TIM3CH1_CAPTURE_STA&0X40)		//捕获到一个下降沿为真 		   发生上升沿中断后再次发生中断为下降沿
			{	  			
				TIM3CH1_CAPTURE_DOWNVAL=TIM_GetCapture1(TIM3);   //获取当前的捕获值

				if(TIM3CH1_CAPTURE_DOWNVAL<TIM3CH1_CAPTURE_UPVAL)
				{
					 tim3_T=65535;
			    }
			    else tim3_T=0;	

			   tempup1=TIM3CH1_CAPTURE_DOWNVAL-TIM3CH1_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间

			   pwmout1=tempup1;

			   TIM3CH1_CAPTURE_STA=0;
					
			   TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获			  
			}
			else  								//发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数
			{
				TIM3CH1_CAPTURE_UPVAL=TIM_GetCapture1(TIM3);			//获取上升沿的数据
				TIM3CH1_CAPTURE_STA|=0X40;		//标记以捕获到了上升沿
			   	TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);		//CC1P=1 设置为下降沿捕获
			
			}		    
		}			     	    					   
 	}
	
  
	if((TIM3CH2_CAPTURE_STA&0X80)==0)//CH1 还未成功捕获	
	{  
		if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
		{	
			TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
			if(TIM3CH2_CAPTURE_STA&0X40)		//捕获到一个下降沿为真 		   发生上升沿中断后再次发生中断为下降沿
			{	  			

			   TIM3CH2_CAPTURE_DOWNVAL=TIM_GetCapture2(TIM3);

				if(TIM3CH2_CAPTURE_DOWNVAL<TIM3CH2_CAPTURE_UPVAL)
				{
					tim3_T=65535;
				}
				else tim3_T=0;	

			   tempup2=TIM3CH2_CAPTURE_DOWNVAL-TIM3CH2_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间			

			   pwmout2=tempup2;
	  
			   TIM3CH2_CAPTURE_STA=0;	

			   TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
			   
			}
			else  								//发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数
			{
				TIM3CH2_CAPTURE_UPVAL=TIM_GetCapture2(TIM3);			//获取上升沿的数据
				TIM3CH2_CAPTURE_STA|=0X40;		//标记以捕获到了上升沿
			   	TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling);		//CC1P=1 设置为下降沿捕获	
			}		    
		}			     	    					   
 	}		 
	

	if((TIM3CH3_CAPTURE_STA&0X80)==0)//CH3 还未成功捕获	
	{	
	  
		if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//捕获3发生捕获事件
		{	
			TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);

			if(TIM3CH3_CAPTURE_STA&0X40)		//捕获到一个下降沿为真 		   发生上升沿中断后再次发生中断为下降沿
			{	  			
				TIM3CH3_CAPTURE_DOWNVAL=TIM_GetCapture3(TIM3);

				if(TIM3CH3_CAPTURE_DOWNVAL<TIM3CH3_CAPTURE_UPVAL)
				{
					tim3_T=65535;
				}
				else tim3_T=0;	

				tempup3=TIM3CH3_CAPTURE_DOWNVAL-TIM3CH3_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间

			   pwmout3=tempup3;

			   TIM3CH3_CAPTURE_STA=0;
				
			   TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC3P=0 设置为上升沿捕获
			
			}
			else  								//发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数
			{
				TIM3CH3_CAPTURE_UPVAL=TIM_GetCapture3(TIM3);			//获取上升沿的数据
				TIM3CH3_CAPTURE_STA|=0X40;		//标记以捕获到了上升沿
			   	TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling);		//CC1P=1 设置为下降沿捕获
			}		    
		}			     	    					   
 	}

	if((TIM3CH4_CAPTURE_STA&0X80)==0)//CH4 还未成功捕获	
	{	  
		if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
		{	
			TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);

			if(TIM3CH4_CAPTURE_STA&0X40)		//捕获到一个下降沿为真 		   发生上升沿中断后再次发生中断为下降沿
			{	  			

				TIM3CH4_CAPTURE_DOWNVAL=TIM_GetCapture4(TIM3);
				if(TIM3CH4_CAPTURE_DOWNVAL<TIM3CH4_CAPTURE_UPVAL)
				{
					tim3_T=65535;
				}
				else tim3_T=0;	

				tempup4=TIM3CH4_CAPTURE_DOWNVAL-TIM3CH4_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间

			   	pwmout4=tempup4;

			    TIM3CH4_CAPTURE_STA=0;	
   				
			   	TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获				
			}
			else  								//发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数
			{
				TIM3CH4_CAPTURE_UPVAL=TIM_GetCapture4(TIM3);			//获取上升沿的数据
				TIM3CH4_CAPTURE_STA|=0X40;		//标记以捕获到了上升沿
			   	TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Falling);		//CC4P=1 设置为下降沿捕获
			
			}		    
		}			     	    					   
 	}							  			 		 
}
示例#23
0
static inline void pwmIRQHandler(TIM_TypeDef *tim)
{
	pwm_in_counter++;

    uint8_t i;
    uint16_t val = 0;
    uint16_t time_on = 0;
    uint16_t time_off = 0;

    for (i = 0; i < PWMIN_CHANNEL_NUM; i++) {
        struct TIM_Channel channel = Channels[i];
        struct PWM_State *input = &Inputs[i];

        if (channel.tim == tim && (TIM_GetITStatus(tim, channel.tim_cc) == SET)) {
            TIM_ClearITPendingBit(channel.tim, channel.tim_cc);

            switch (channel.tim_channel) {
                case TIM_Channel_1:
                    val = TIM_GetCapture1(channel.tim);
                    break;
                case TIM_Channel_2:
                    val = TIM_GetCapture2(channel.tim);
                    break;
                case TIM_Channel_3:
                    val = TIM_GetCapture3(channel.tim);
                    break;
                case TIM_Channel_4:
                    val = TIM_GetCapture4(channel.tim);
                    break;
            }


            if (input->state == 0) {
        	input->rise = val;
        	if(input->rise > input->fall)
        	    time_off = input->rise - input->fall;
        	else
        	    time_off = ((0xFFFF - input->fall) + input->rise);

                // switch states
        	if ((time_off >= MINOFFWIDTH) && (time_off <= MAXOFFWIDTH)){
        	    input->state = 1;
                    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
                    TIM_ICInitStructure.TIM_Channel = channel.tim_channel;
                    TIM_ICInit(channel.tim, &TIM_ICInitStructure);
        	}

            } else {
        	input->fall = val;
        	if (input->fall > input->rise)
                    time_on = (input->fall - input->rise);
                else
                    time_on = ((0xFFFF - input->rise) + input->fall);

        	if ((time_on >= MINONWIDTH) && (time_on <= MAXONWIDTH))
        	    {
		    // compute capture
		    input->capture = time_on;

		    // switch state
		    input->state = 0;
		    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
		    TIM_ICInitStructure.TIM_Channel = channel.tim_channel;
		    TIM_ICInit(channel.tim, &TIM_ICInitStructure);
		    }
            }
        }
    }
}
示例#24
0
/**
 * @brief Handler for TIM4 global interrupt
 * @details TIM4 handler
 */
void TIM4_IRQHandler(void) {
  if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) {
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);

    _ge_ic_chan_captures_last[IC_CHAN1] = _ge_ic_chan_captures[IC_CHAN1];
    _ge_ic_chan_captures[IC_CHAN1] = TIM_GetCapture1(TIM4);

    // set updated available flag
    _ge_ic_chan_freq_avail[IC_CHAN1] = true;

    //calculate actual counts based on timer overflow count
    if (_ge_ic_chan_captures_last[IC_CHAN1] > _ge_ic_chan_captures[IC_CHAN1]) {
      _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN1] - 1)
                                     - _ge_ic_chan_captures_last[IC_CHAN1];
    } else {
      _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN1])
                                     - _ge_ic_chan_captures_last[IC_CHAN1];
    }

    _ge_ic_chan_ovf[IC_CHAN1] = 0;

    //reenable chan 1 IC
    TIM_ICInitTypeDef ic_init_struct;
    TIM_ICStructInit(&ic_init_struct);

    ic_init_struct.TIM_Channel = TIM_Channel_1;
    ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising;
    ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI;
    ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    ic_init_struct.TIM_ICFilter = GE_IC_IFILTER;

    TIM_ICInit(TIM4, &ic_init_struct);
    // _ge_ic_chan_captures[IC_CHAN1] = 3000;
  }

  if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) {
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);

    _ge_ic_chan_captures_last[IC_CHAN2] = _ge_ic_chan_captures[IC_CHAN2];
    _ge_ic_chan_captures[IC_CHAN2] = TIM_GetCapture2(TIM4);

    // set updated available flag
    _ge_ic_chan_freq_avail[IC_CHAN2] = true;

    //calculate actual counts based on timer overflow count
    if (_ge_ic_chan_captures_last[IC_CHAN2] > _ge_ic_chan_captures[IC_CHAN2]) {
      _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN2] - 1)
                                     - _ge_ic_chan_captures_last[IC_CHAN2];
    } else {
      _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN2])
                                     - _ge_ic_chan_captures_last[IC_CHAN2];
    }

    _ge_ic_chan_ovf[IC_CHAN2] = 0;

    //reenable chan 2 IC
    TIM_ICInitTypeDef ic_init_struct;
    TIM_ICStructInit(&ic_init_struct);

    ic_init_struct.TIM_Channel = TIM_Channel_2;
    ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising;
    ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI;
    ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    ic_init_struct.TIM_ICFilter = GE_IC_IFILTER;

    TIM_ICInit(TIM4, &ic_init_struct);
  }

  if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) {
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);

    _ge_ic_chan_captures_last[IC_CHAN3] = _ge_ic_chan_captures[IC_CHAN3];
    _ge_ic_chan_captures[IC_CHAN3] = TIM_GetCapture3(TIM4);

    // set updated available flag
    _ge_ic_chan_freq_avail[IC_CHAN3] = true;

    //calculate actual counts based on timer overflow count
    if (_ge_ic_chan_captures_last[IC_CHAN3] > _ge_ic_chan_captures[IC_CHAN3]) {
      _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN3] - 1)
                                     - _ge_ic_chan_captures_last[IC_CHAN3];
    } else {
      _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN3])
                                     - _ge_ic_chan_captures_last[IC_CHAN3];
    }

    _ge_ic_chan_ovf[IC_CHAN3] = 0;

    //reenable chan 3 IC
    TIM_ICInitTypeDef ic_init_struct;
    TIM_ICStructInit(&ic_init_struct);

    ic_init_struct.TIM_Channel = TIM_Channel_3;
    ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising;
    ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI;
    ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    ic_init_struct.TIM_ICFilter = GE_IC_IFILTER;

    TIM_ICInit(TIM4, &ic_init_struct);
  }

  if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) {
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);

    _ge_ic_chan_captures_last[IC_CHAN4] = _ge_ic_chan_captures[IC_CHAN4];
    _ge_ic_chan_captures[IC_CHAN4] = TIM_GetCapture4(TIM4);

    // set updated available flag
    _ge_ic_chan_freq_avail[IC_CHAN4] = true;

    //calculate actual counts based on timer overflow count
    if (_ge_ic_chan_captures_last[IC_CHAN4] > _ge_ic_chan_captures[IC_CHAN4]) {
      _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN4] - 1)
                                     - _ge_ic_chan_captures_last[IC_CHAN4];
    } else {
      _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4]
                                     + 65536 * (_ge_ic_chan_ovf[IC_CHAN4])
                                     - _ge_ic_chan_captures_last[IC_CHAN4];
    }

    _ge_ic_chan_ovf[IC_CHAN4] = 0;

    //reenable chan 4 IC
    TIM_ICInitTypeDef ic_init_struct;
    TIM_ICStructInit(&ic_init_struct);

    ic_init_struct.TIM_Channel = TIM_Channel_4;
    ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising;
    ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI;
    ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    ic_init_struct.TIM_ICFilter = GE_IC_IFILTER;

    TIM_ICInit(TIM4, &ic_init_struct);
  }

  if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) {
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);

    for (int i = 0; i < 4; i++) {
      _ge_ic_chan_ovf[i]++;
    }
  }
}
示例#25
0
void TIM2_IRQHandler(void)
{
	uint32_t current[4];
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICStructInit(&TIM_ICInitStructure);

	if (TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET) {
		/* Clear TIM1 Capture compare interrupt pending bit */
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

		if ( inc[INC6].status == RISING ) {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;

			/* Get the Input Capture value */
			inc[INC6].prev_value = TIM_GetCapture1(TIM2);
			inc[INC6].status = FALLING;
			

		} else {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

			//Get the Input Capture value
			current[0] =  TIM_GetCapture1(TIM2);

			if (current[0] > inc[INC6].prev_value)
				inc[INC6].curr_value =  current[0] - inc[INC6].prev_value ;
			else if (current[0] < inc[INC6].prev_value)
				inc[INC6].curr_value = 0xFFFF - inc[INC6].prev_value  + current[0] ;

			inc[INC6].status = RISING;
		}

		TIM_ICInit(TIM2, &TIM_ICInitStructure);

	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET) {
		/* Clear TIM1 Capture compare interrupt pending bit */
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

		if (inc[INC5].status == RISING) {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;

			/* Get the Input Capture value */
			inc[INC5].prev_value = TIM_GetCapture2(TIM2);
			inc[INC5].status = FALLING;

		} else {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

			/* Get the Input Capture value */
			current[1] =  TIM_GetCapture2(TIM2);

			if (current[1] > inc[INC5].prev_value)
				inc[INC5].curr_value =  current[1] - inc[INC5].prev_value ;
			else if (current[1] < inc[INC5].prev_value)
				inc[INC5].curr_value = 0xFFFF - inc[INC5].prev_value + current[1] ;

			inc[INC5].status = RISING;


		}

		TIM_ICInit(TIM2, &TIM_ICInitStructure);
	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET) {
		/* Clear TIM1 Capture compare interrupt pending bit */
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

		if (inc[INC2].status == RISING) {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;

			/* Get the Input Capture value */
			inc[INC2].prev_value = TIM_GetCapture3(TIM2);
			inc[INC2].status = FALLING;
			


		} else {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

			/* Get the Input Capture value */
			current[2] =  TIM_GetCapture3(TIM2);

			if (current[2] > inc[INC2].prev_value)
				inc[INC2].curr_value=  current[2] - inc[INC2].prev_value;
			else if (current[2] < inc[INC2].prev_value)
				inc[INC2].curr_value =   0xFFFF - inc[INC2].prev_value + current[2] ;
			
			inc[INC2].status = RISING;


		}

		TIM_ICInit(TIM2, &TIM_ICInitStructure);
	}

	if (TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET) {
		/* Clear TIM1 Capture compare interrupt pending bit */
		TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

		if (inc[INC1].status == RISING) {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;

			/* Get the Input Capture value */
			inc[INC1].prev_value = TIM_GetCapture4(TIM2);
			inc[INC1].status  = FALLING;
			

		} else {
			TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
			TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

			/* Get the Input Capture value */
			current[3] =  TIM_GetCapture4(TIM2);

			if (current[3] > inc[INC1].prev_value)
				inc[INC1].curr_value =  current[3] - inc[INC1].prev_value;
			else if (current[3] < inc[INC1].prev_value)
				inc[INC1].curr_value = 0xFFFF - inc[INC1].prev_value+ current[3] ;

			inc[INC1].status = RISING;


		}

		TIM_ICInit(TIM2, &TIM_ICInitStructure);
	}
}
示例#26
0
void _TIM4_IRQHandler(void)		
{
	static u16 temp_cnt1,temp_cnt1_2,temp_cnt2,temp_cnt2_2,temp_cnt3,temp_cnt3_2,temp_cnt4,temp_cnt4_2;
	
	Feed_Rc_Dog(1);//RC
	
	if(TIM4->SR & TIM_IT_CC1) 
	{
		TIM4->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
		TIM4->SR = ~TIM_FLAG_CC1OF;
		if(GPIOD->IDR & GPIO_Pin_12)
		{
			temp_cnt1 = TIM_GetCapture1(TIM4);
		}
		else
		{
			temp_cnt1_2 = TIM_GetCapture1(TIM4);
			if(temp_cnt1_2>=temp_cnt1)
				Rc_Pwm_In[4] = temp_cnt1_2-temp_cnt1;
			else
				Rc_Pwm_In[4] = 0xffff-temp_cnt1+temp_cnt1_2+1;
		}
	}
	if(TIM4->SR & TIM_IT_CC2) 
	{
		TIM4->SR = ~TIM_IT_CC2;
		TIM4->SR = ~TIM_FLAG_CC2OF;
		if(GPIOD->IDR & GPIO_Pin_13)
		{
			temp_cnt2 = TIM_GetCapture2(TIM4);
		}
		else
		{
			temp_cnt2_2 = TIM_GetCapture2(TIM4);
			if(temp_cnt2_2>=temp_cnt2)
				Rc_Pwm_In[5] = temp_cnt2_2-temp_cnt2;
			else
				Rc_Pwm_In[5] = 0xffff-temp_cnt2+temp_cnt2_2+1;
		}
	}
	if(TIM4->SR & TIM_IT_CC3) 
	{
		TIM4->SR = ~TIM_IT_CC3;
		TIM4->SR = ~TIM_FLAG_CC3OF;
		if(GPIOD->IDR & GPIO_Pin_14)
		{
			temp_cnt3 = TIM_GetCapture3(TIM4);
		}
		else
		{
			temp_cnt3_2 = TIM_GetCapture3(TIM4);
			if(temp_cnt3_2>=temp_cnt3)
				Rc_Pwm_In[6] = temp_cnt3_2-temp_cnt3;
			else
				Rc_Pwm_In[6] = 0xffff-temp_cnt3+temp_cnt3_2+1;
		}
	}
	if(TIM4->SR & TIM_IT_CC4) 
	{
		TIM4->SR = ~TIM_IT_CC4;
		TIM4->SR = ~TIM_FLAG_CC4OF;
		if(GPIOD->IDR & GPIO_Pin_15)
		{
			temp_cnt4 = TIM_GetCapture4(TIM4);
		}
		else
		{
			temp_cnt4_2 = TIM_GetCapture4(TIM4);
			if(temp_cnt4_2>=temp_cnt4)
				Rc_Pwm_In[7] = temp_cnt4_2-temp_cnt4;
			else
				Rc_Pwm_In[7] = 0xffff-temp_cnt4+temp_cnt4_2+1;
		}
	}
}