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

📄 75x_tim.c

📁 嵌入式实验源码。包括:电源管理、复位、时钟管理
💻 C
📖 第 1 页 / 共 4 页
字号:
  u16 TIM_IT_Clear = 0;

  /* Calculate the pending bits to be cleared */
  TIM_IT_Clear = TIM_IT & TIM_IT_Clear_Mask;

  /* Clear the pending bits */
  TIMx->ISR &= ~TIM_IT_Clear;
}

/*******************************************************************************
* Function Name  : OCM_ModuleConfig
* Description    : Output Compare Module configuration
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
*                  contains the configuration information for the specified TIM
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
static void OCM_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
{
  u16 TIM_OCControl = 0x0000;

  if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCTiming)
  {
    TIM_OCControl = TIM_OCControl_OCTiming;
  }
  else
  {
    if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCActive || 
       TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Active)
    {
      TIM_OCControl = TIM_OCControl_OCActive;
    }
    else
    {
      if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCInactive)
      {
        TIM_OCControl = TIM_OCControl_OCInactive;
      }
      else
      {
      	 if(TIM_InitStruct->TIM_Mode == TIM_Mode_OCToggle ||
            TIM_InitStruct->TIM_Mode == TIM_Mode_OPM_Toggle)
        {
          TIM_OCControl = TIM_OCControl_OCToggle;
        }
        else
        {
          TIM_OCControl = TIM_OCControl_PWM;

        }
      }
    }
  }

  if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)
  {
    /* Configure Channel 1 on Output Compare mode */
    TIMx->OMR1 &= TIM_OC1C_Mask;
    TIMx->OMR1 |= TIM_OCControl|TIM_OC1_Enable;
    TIMx->OMR1 |= TIM_PLD1_Set;
    TIMx->OCR1 = TIM_InitStruct->TIM_Pulse1;

    /* Set the OC1 wave polarity */
    if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)
    {
      TIMx->OMR1 |= TIM_OC1P_Set;
    }
    else
    {
      TIMx->OMR1 &= TIM_OC1P_Reset;
    }
  }
  else
  {
    if(TIM_InitStruct->TIM_Channel == TIM_Channel_2)
    {
      /* Configure Channel 2 on Output Compare mode */
      TIMx->OMR1 &= TIM_OC2C_Mask;
      TIMx->OMR1 |= TIM_OCControl<<8|TIM_OC2_Enable;
      TIMx->OMR1 |= TIM_PLD2_Set;
      TIMx->OCR2 = TIM_InitStruct->TIM_Pulse2;

      /* Set the OCB wave polarity */
      if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)
      {
        TIMx->OMR1 |= TIM_OC2P_Set;
      }
      else
      {
        TIMx->OMR1 &= TIM_OC2P_Reset;
      }
    }
     /* Configure Channel 1 and Channel 2 on Output Compare mode */
    else
    {
      TIMx->OMR1 &= TIM_OC1C_Mask & TIM_OC2C_Mask; 
      TIMx->OMR1 |= TIM_OCControl|(TIM_OCControl<<8)|TIM_OC1_Enable|TIM_OC2_Enable|
                   TIM_PLD1_Set|TIM_PLD2_Set;

      TIMx->OCR1 = TIM_InitStruct->TIM_Pulse1;
      TIMx->OCR2 = TIM_InitStruct->TIM_Pulse2;

      /* Set the OC1 wave polarity */
      if(TIM_InitStruct->TIM_Polarity1 == TIM_Polarity1_Low)
      {
        TIMx->OMR1 |= TIM_OC1P_Set;
      }
      else
      {
        TIMx->OMR1 &= TIM_OC1P_Reset;
      }

      /* Set the OC2 wave polarity */
      if(TIM_InitStruct->TIM_Polarity2 == TIM_Polarity2_Low)
      {
        TIMx->OMR1 |= TIM_OC2P_Set;
      }
      else
      {
        TIMx->OMR1 &= TIM_OC2P_Reset;
      }
    }
  }
}

/*******************************************************************************
* Function Name  : ICAP_ModuleConfig
* Description    : Input Capture Module configuration
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
*                  contains the configuration information for the specified TIM
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
static void ICAP_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
{
  if(TIM_InitStruct->TIM_Mode == TIM_Mode_PWMI)
  { /* PWM input mode configuration */
    TIMx->SCR |= TIM_TS_IC1_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;

    /* Channel 1 and channel 2 input selection */
    if(TIM_InitStruct->TIM_PWMI_ICSelection == TIM_PWMI_ICSelection_TI1)
    {
      TIMx->IMCR &= TIM_IC1S_Reset;
      TIMx->IMCR |= TIM_IC2S_Set;
    }
    else
    {
      TIMx->IMCR |= TIM_IC1S_Set;
      TIMx->IMCR &= TIM_IC2S_Reset;
    }

    /* Channel polarity */
    if(TIM_InitStruct->TIM_PWMI_ICPolarity == TIM_PWMI_ICPolarity_Rising)
    {
      TIMx->IMCR &= TIM_IC1P_Reset;
      TIMx->IMCR |= TIM_IC2P_Set;
    }
    else
    {
      TIMx->IMCR |= TIM_IC1P_Set;
      TIMx->IMCR &= TIM_IC2P_Reset;
    }

    /* Input capture  Enable */
    TIMx->IMCR |= TIM_IC1_Enable |TIM_IC2_Enable;
  }
  else
  {
    if(TIM_InitStruct->TIM_Channel == TIM_Channel_1)
    {
      /* Input Capture 1 mode configuration */
      TIMx->SCR &= TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask;
      TIMx->SCR |= TIM_TS_IC1_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;
      
      /* Channel 1 input selection */
      if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI1)
      {
        TIMx->IMCR &= TIM_IC1S_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC1S_Set;
      }
      /* Channel 1 polarity */
      if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Rising)
      {
        TIMx->IMCR &= TIM_IC1P_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC1P_Set;
      }

      /* Input capture  Enable */
      TIMx->IMCR |= TIM_IC1_Enable;
    }
    else
    {
      /* Input Capture 2 mode configuration */
      TIMx->SCR &= (TIM_TriggerSelection_Mask & TIM_SlaveModeSelection_Mask);
      TIMx->SCR |= TIM_TS_IC2_Set|TIM_SMS_RESETCLK_Set|TIM_SME_Set;

      /* Channel 2 input selection */
      if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI2)
      {
        TIMx->IMCR &= TIM_IC2S_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2S_Set;
      }

      /* Channel 2 polarity */
      if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Rising)
      {
        TIMx->IMCR &= TIM_IC2P_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2P_Set;
      }

      /* Input capture  Enable */
      TIMx->IMCR |= TIM_IC2_Enable;
    }
  }
}

/*******************************************************************************
* Function Name  : Encoder_ModeConfig
* Description    : Encoder Mode configuration
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
*                  contains the configuration information for the specified TIM
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
static void Encoder_ModeConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct)
{
  if(TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder1)
  {
    /* Set Encoder 1 mode */
    TIMx->SCR &= TIM_Encoder_Mask;
    TIMx->SCR |= TIM_Encoder1_Set;

    /* Channel 1 input selection */
    if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI1)
    {
      TIMx->IMCR &= TIM_IC1S_Reset;
    }
    else
    {
      TIMx->IMCR |= TIM_IC1S_Set;
    }

     /* Channel 1 polarity */
    if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Rising)
    {
      TIMx->IMCR &= TIM_IC1P_Reset;
    }
    else
    {
      TIMx->IMCR |= TIM_IC1P_Set;
    }
      TIMx->IMCR |= TIM_IC1_Enable;
  }
  else
  {
    if(TIM_InitStruct->TIM_Mode == TIM_Mode_Encoder2)
    {
      /* Set Encoder 2 mode */
      TIMx->SCR &= TIM_Encoder_Mask;
      TIMx->SCR |= TIM_Encoder2_Set;

      /* Channel 2 input selection */
      if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI2)
      {
        TIMx->IMCR &= TIM_IC2S_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2S_Set;
      }

      /* Channel 2 polarity */
      if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Rising)
      {
        TIMx->IMCR &= TIM_IC2P_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2P_Set;
      }
        TIMx->IMCR |= TIM_IC2_Enable;
    }
    else
    {
      /* Set Encoder 3 mode */
      TIMx->SCR &= TIM_Encoder_Mask;
      TIMx->SCR |= TIM_Encoder3_Set;

      /* Channel 1 input selection */
      if(TIM_InitStruct->TIM_IC1Selection == TIM_IC1Selection_TI1)
      {
        TIMx->IMCR &= TIM_IC1S_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC1S_Set;
      }

      /* Channel B input selection */
      if(TIM_InitStruct->TIM_IC2Selection == TIM_IC2Selection_TI2)
      {
        TIMx->IMCR &= TIM_IC2S_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2S_Set;
      }

      /* Channel 1 polarity */
      if(TIM_InitStruct->TIM_IC1Polarity == TIM_IC1Polarity_Rising)
      {
        TIMx->IMCR &= TIM_IC1P_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC1P_Set;
      }
      /* Channel 2 polarity */
      if(TIM_InitStruct->TIM_IC2Polarity == TIM_IC2Polarity_Rising)
      {
        TIMx->IMCR &= TIM_IC2P_Reset;
      }
      else
      {
        TIMx->IMCR |= TIM_IC2P_Set;
      }
      /* Input capture  Enable */
      TIMx->IMCR |= TIM_IC1_Enable |TIM_IC2_Enable;
    }
  }
}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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