⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 75x_tim.c

📁 嵌入式实验源码。包括:电源管理、复位、时钟管理
💻 C
📖 第 1 页 / 共 4 页
字号:
*******************************************************************************/
void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode)
{
  /* Counter mode configuration */
  TIMx->CR &= TIM_CounterMode_Mask;
  TIMx->CR |= TIM_CounterMode;
}

/*******************************************************************************
* Function Name  : TIM_ForcedOCConfig
* Description    : Forces the TIM output waveform to active or inactive level.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_Channel: specifies the TIM channel to be used.
*                    This parameter can be one of the following values:
*                       - TIM_Channel_1: Timer Channel 1 is used
*                       - TIM_Channel_2: Timer Channel 2 is used
*                       - TIM_Channel_ALL: Timer Channel 1 and 2 are used
*                 - TIM_ForcedAction: specifies the forced Action to be set to
*                  the output waveform.
*                    This parameter can be one of the following values:
*                       - TIM_ForcedAction_Active: Force active level on OCxREF
*                       - TIM_ForcedAction_InActive: Force inactive level on 
*                         OCxREF.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_ForcedOCConfig(TIM_TypeDef* TIMx, u16 TIM_Channel,u16 TIM_ForcedAction)
{
  /* Channel 1 Forced Output Compare mode configuration */
  if(TIM_Channel == TIM_Channel_1)
  {
    TIMx->OMR1 &= TIM_OC1C_Mask;
    TIMx->OMR1 |= TIM_ForcedAction;
  }
  /* Channel 2 Forced Output Compare mode configuration */
  else
  {
    if(TIM_Channel == TIM_Channel_2)
    {
      TIMx->OMR1 &= TIM_OC2C_Mask;
      TIMx->OMR1 |= (TIM_ForcedAction<<8);
    }
    /* Channel 1 and Channel 2 Forced Output Compare mode configuration */
    else
    {
      TIMx->OMR1 &= TIM_OC1C_Mask & TIM_OC2C_Mask;
      TIMx->OMR1 |= TIM_ForcedAction |(TIM_ForcedAction<<8);
    }
  }
}

/*******************************************************************************
* Function Name  : TIM_ResetCounter
* Description    : Re-intializes the TIM counter and generates an update of the
*                  registers.
* Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_ResetCounter(TIM_TypeDef* TIMx)
{
  /* Re-intialize the TIM counter */
  TIMx->CR |= TIM_COUNTER_Reset;
}

/*******************************************************************************
* Function Name  : TIM_SynchroConfig
* Description    : Synchronizes Timers and PWM in a specified mode.
* Input          : - Master: specifies the peripheral master.
*                    This parameter can be one of the following values:
*                    PWM_Master, TIM0_Master, TIM1_Master or TIM2_Master.
*                  - Slave: specifies the peripheral slave.
*                    This parameter can be one of the following values:
*                    PWM_Slave, TIM0_Slave, TIM1_Slave or TIM2_Slave.
*                  - TIM_SynchroAction: specifies the synchronization Action to 
*                    be used.
*                    This parameter can be one of the following values:
*                         - TIM_SynchroAction_Enable: The CNT_EN bit is used as TRGO
*                         - TIM_SynchroAction_Update: The Update event is used as TRGO
*                         - TIM_SynchroAction_Reset: The CNT_RST bit is used as TRGO
*                         - SynchroAction_OC: The OC1 signal is used as TRGO
*                  - TIM_SynchroMode: specifies the synchronization Mode to be used.
*                    This parameter can be one of the following values:
*                         - TIM_SynchroMode_Gated: Both start and stop of the 
*                           counter is controlled.
*                         - TIM_SynchroMode_Trigger: Only the start of the 
*                           counter is controlled.
*                         - TIM_SynchroMode_External: The rising edge of selected trigger 
*                           clocks the counter.
*                         - TIM_SynchroMode_Reset: The rising edge of the selected trigger 
*                           signal resets the counter and generates an update of the registers.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_SynchroConfig(Master_TypeDef Master, Slave_TypeDef Slave,
                       u16 TIM_SynchroAction, u16 TIM_SynchroMode)
{
  switch (Slave)
  {
    case PWM_Slave:
    {
      PWM->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
                  TIM_InternalTriggerSelection_Mask;
      PWM->SCR |= TIM_SynchroMode | TIM_SME_Set;

      if(Master == TIM1_Master)
      {
        /* Set the internal trigger */
      	PWM->SCR |= TIM_ITS_TIM1;

        /* Set the synchronization action */
        TIM1->CR &= TIM_MasterModeSelection_Mask;
        TIM1->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM0_Master)
      {
        /* Set the internal trigger */
        PWM->SCR |= TIM_ITS_TIM0;

        /* Set the synchronization action */
        TIM0->CR &= TIM_MasterModeSelection_Mask;
        TIM0->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM2_Master)
      {
        /* Set the internal trigger */
        PWM->SCR |= TIM_ITS_TIM2;

        /* Set the synchronization action */
        TIM2->CR &= TIM_MasterModeSelection_Mask;
        TIM2->CR |= TIM_SynchroAction;
      }
    }
    break;

    case TIM0_Slave:
    {
      TIM0->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
                   TIM_InternalTriggerSelection_Mask;
      TIM0->SCR |= TIM_SynchroMode | TIM_SME_Set;

      if(Master == PWM_Master)
      {
        /* Set the internal trigger */
        TIM0->SCR |= TIM_ITS_PWM;

        /* Set the synchronization action */
        PWM->CR &= TIM_MasterModeSelection_Mask;
        PWM->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM1_Master)
      {
        /* Set the internal trigger */
        TIM0->SCR |= TIM_ITS_TIM1;

        /* Set the synchronization action */
        TIM1->CR &= TIM_MasterModeSelection_Mask;
        TIM1->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM2_Master)
      {
        /* Set the internal trigger */
        TIM0->SCR |= TIM_ITS_TIM2;

        /* Set the synchronization action */
        TIM2->CR &= TIM_MasterModeSelection_Mask;
        TIM2->CR |= TIM_SynchroAction;
      }
    }
    break;

    case TIM1_Slave:
    {

      TIM1->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
                   TIM_InternalTriggerSelection_Mask;
      TIM1->SCR |= TIM_SynchroMode | TIM_SME_Set;
     
      if(Master == PWM_Master)
      {
      	 /* Set the internal trigger */
      	 TIM1->SCR |= TIM_ITS_PWM;

        /* Set the synchronization action */
        PWM->CR &= TIM_MasterModeSelection_Mask;
        PWM->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM0_Master)
      {
        /* Set the internal trigger */
        TIM1->SCR |= TIM_ITS_TIM0;

        /* Set the synchronization action */
        TIM0->CR &= TIM_MasterModeSelection_Mask;
        TIM0->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM2_Master)
      {
        /* Set the internal trigger */
        TIM1->SCR |= TIM_ITS_TIM2;

        /* Set the synchronization action */
        TIM2->CR &= TIM_MasterModeSelection_Mask;
        TIM2->CR |= TIM_SynchroAction;
      }
    }
    break;

    case TIM2_Slave:
    {
     
      TIM2->SCR &= TIM_SME_Reset & TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask &
                   TIM_InternalTriggerSelection_Mask;
      TIM2->SCR |= TIM_SynchroMode | TIM_SME_Set;

      if(Master == PWM_Master)
      {
        /* Internal trigger selection */
        TIM2->SCR |= TIM_ITS_PWM;

        /* Set the synchronization action */
        PWM->CR &= TIM_MasterModeSelection_Mask;
        PWM->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM1_Master)
      {
        /* Internal trigger selection */
        TIM2->SCR |= TIM_ITS_TIM1;

        /* Set the synchronization action */
        TIM1->CR &= TIM_MasterModeSelection_Mask;
        TIM1->CR |= TIM_SynchroAction;
      }

      else if(Master == TIM0_Master)
      {
        /* Internal trigger selection */
        TIM2->SCR |= TIM_ITS_TIM0;

        /* Set the synchronization action */
        TIM0->CR &= TIM_MasterModeSelection_Mask;
        TIM0->CR |= TIM_SynchroAction;
      }
    }
    break;

    default:
    break;
  }
}

/*******************************************************************************
* Function Name  : TIM_GetFlagStatus
* Description    : Checks whether the specified TIM flag is set or not.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_FLAG: specifies the flag to check. 
*                    This parameter can be one of the following values:
*                         - TIM_FLAG_IC1: Input Capture 1 Flag
*                         - TIM_FLAG_OC1: Output Compare 1 Flag
*                         - TIM_FLAG_Update: Timer update Flag
*                         - TIM_FLAG_IC2: Input Capture 2 Flag
*                         - TIM_FLAG_OC2: Output Compare 2 Flag
* Output         : None
* Return         : The new state of TIM_FLAG (SET or RESET).
*******************************************************************************/
FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, u16 TIM_FLAG)
{
  if((TIMx->ISR & TIM_FLAG) != RESET )
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : TIM_ClearFlag
* Description    : Clears the TIMx's pending flags.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_FLAG: specifies the flag bit to clear.
*                    This parameter can be any combination of the following values:
*                         - TIM_FLAG_IC1: Timer Input Capture 1 flag
*                         - TIM_FLAG_OC1: Timer Output Compare 1 flag
*                         - TIM_FLAG_Update: Timer update flag
*                         - TIM_FLAG_IC2: Timer Input Capture 2 flag
*                         - TIM_FLAG_OC2: Timer Output Compare 2 flag
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_ClearFlag(TIM_TypeDef* TIMx, u16 TIM_FLAG)
{
  /* Clear the flags */
  TIMx->ISR &= ~TIM_FLAG;
}

/*******************************************************************************
* Function Name  : TIM_GetITStatus
* Description    : Checks whether the specified TIM interrupt has occurred or not.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_IT: specifies the TIM interrupt source to check.
*                    This parameter can be one of the following values:
*                         - TIM_IT_IC1: Input Capture 1 interrupt
*                         - TIM_IT_OC1: Output Compare 1 interrupt
*                         - TIM_IT_Update: Timer update interrupt
*                         - TIM_IT_GlobalUpdate: Timer global update interrupt
*                         - TIM_IT_IC2: Input Capture 2 interrupt
*                         - TIM_IT_OC2: Output Compare 2 interrupt
* Output         : None
* Return         : The new state of TIM_IT(SET or RESET).
*******************************************************************************/
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, u16 TIM_IT)
{
  u16 TIM_IT_Check = 0;

  /* Calculates the pending bits to be checked */
  TIM_IT_Check = TIM_IT & TIM_IT_Clear_Mask;
  
  if((TIMx->ISR & TIM_IT_Check) != RESET )
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : TIM_ClearITPendingBit
* Description    : Clears the TIM's interrupt pending bits.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_IT: specifies the interrupt pending bit to clear.
*                    This parameter can be one of the following values:
*                         - TIM_IT_IC1: Input Capture 1 Interrupt 
*                         - TIM_IT_OC1: Output Compare 1 Interrupt 
*                         - TIM_IT_Update: Timer update Interrupt 
*                         - TIM_IT_GlobalUpdate: Timer global update Interrupt 
*                         - TIM_IT_IC2: Input Capture 2 Interrupt 
*                         - TIM_IT_OC2: Output Compare 2 Interrupt 
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -