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

📄 stm8s_tim1.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 5 页
字号:
  * None
  * @par Example:
  * Initialize the TIM1 Channel4 according to specified parameters.
  * @code
  * TIM1_OCMode_TypeDef My_OCMode = TIM1_OCMODE_PWM1;
  * TIM1_OutputState_TypeDef My_OutputState = TIM1_OUTPUTSTATE_ENABLE;
  *  u16 My_Pulse = 0x3FFF;
  *  TIM1_OCPolarity_TypeDef My_OCPolarity = TIM1_OCPOLARITY_HIGH;
  *  TIM1_OCIdleState_TypeDef My_OCIdleState = TIM1_OCIDLESTATE_RESET;
  *  TIM1_OC1Init( My_OCMode, My_OutputState,   My_Pulse, My_OCPolarity,  My_OCIdleState);
  * @endcode
  */
void TIM1_OC4Init(TIM1_OCMode_TypeDef TIM1_OCMode,
                  TIM1_OutputState_TypeDef TIM1_OutputState,
                  u16 TIM1_Pulse,
                  TIM1_OCPolarity_TypeDef TIM1_OCPolarity,
                  TIM1_OCIdleState_TypeDef TIM1_OCIdleState)
{

  /* Check the parameters */
  assert_param(IS_TIM1_OC_MODE_OK(TIM1_OCMode));
  assert_param(IS_TIM1_OUTPUT_STATE_OK(TIM1_OutputState));
  assert_param(IS_TIM1_OC_POLARITY_OK(TIM1_OCPolarity));
  assert_param(IS_TIM1_OCIDLE_STATE_OK(TIM1_OCIdleState));



  /* Disable the Channel 4: Reset the CCE Bit */
  TIM1->CCER2 &= (u8)(~(TIM1_CCER2_CC4E | TIM1_CCER2_CC4P));
  /* Set the Output State  &  the Output Polarity */
  TIM1->CCER2 |= (u8)((TIM1_OutputState & TIM1_CCER2_CC4E ) |  (TIM1_OCPolarity  & TIM1_CCER2_CC4P ));

  /* Reset the Output Compare Bit  and Set the Ouput Compare Mode */
  TIM1->CCMR4 = (u8)((TIM1->CCMR4 & (u8)(~TIM1_CCMR_OCM)) | (TIM1_OCMode));

  /* Set the Output Idle state */
  if (TIM1_OCIdleState != TIM1_OCIDLESTATE_RESET)
  {
    TIM1->OISR |= (u8)(~TIM1_CCER2_CC4P);
  }
  else
  {
    TIM1->OISR &= (u8)(~TIM1_OISR_OIS4);
  }

  /* Set the Pulse value */
  TIM1->CCR4H = (u8)(TIM1_Pulse >> 8);
  TIM1->CCR4L = (u8)(TIM1_Pulse);

}

/**
  * @brief Configures the Break feature, dead time, Lock level, the OSSI,
  * and the AOE(automatic output enable).
  * @param[in]  TIM1_OSSIState specifies the OSSIS State from @ref TIM1_OSSIState_TypeDef.
  * @param[in] TIM1_LockLevel specifies the lock level from @ref TIM1_LockLevel_TypeDef.
  * @param[in]  TIM1_DeadTime specifies the dead time value.
  * @param[in]  TIM1_Break specifies the Break state @ref TIM1_BreakState_TypeDef.
  * @param[in]  TIM1_BreakPolarity specifies the Break polarity from @ref TIM1_BreakPolarity_TypeDef.
  * @param[in]  TIM1_AutomaticOutput specifies the Automatic Output configuration from @ref TIM1_AutomaticOutput_TypeDef.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Configure TIM1 according to specified parameters.
  * @code
  *  TIM1_OSSIState_TypeDef My_OSSIState = TIM1_OSSISTATE_DISABLE;
  *  TIM1_LockLevel_TypeDef My_LockLevel = TIM1_LOCKLEVEL_OFF;
  *  u8 My_DeadTime = 0x00;
  *  TIM1_BreakState_TypeDef My_Break  = TIM1_BREAK_ENABLE;
  *  TIM1_BreakPolarity_TypeDef My_BreakPolarity = TIM1_BREAKPOLARITY_LOW;
  *  TIM1_AutomaticOutput_TypeDef My_AutomaticOutput = TIM1_AUTOMATICOUTPUT_DISABLE;
  * TIM1_BDTRConfig(TIM1_OSSIState,  TIM1_LockLevel_TypeDef TIM1_LockLevel, My_DeadTime,  My_Break,  My_BreakPolarity,  My_AutomaticOutput);
  * @endcode
  */
void TIM1_BDTRConfig(TIM1_OSSIState_TypeDef TIM1_OSSIState,
                     TIM1_LockLevel_TypeDef TIM1_LockLevel,
                     u8 TIM1_DeadTime,
                     TIM1_BreakState_TypeDef TIM1_Break,
                     TIM1_BreakPolarity_TypeDef TIM1_BreakPolarity,
                     TIM1_AutomaticOutput_TypeDef TIM1_AutomaticOutput)
{


  /* Check the parameters */
  assert_param(IS_TIM1_OSSI_STATE_OK(TIM1_OSSIState));
  assert_param(IS_TIM1_LOCK_LEVEL_OK(TIM1_LockLevel));
  assert_param(IS_TIM1_BREAK_STATE_OK(TIM1_Break));
  assert_param(IS_TIM1_BREAK_POLARITY_OK(TIM1_BreakPolarity));
  assert_param(IS_TIM1_AUTOMATIC_OUTPUT_STATE_OK(TIM1_AutomaticOutput));


  TIM1->DTR = (u8)(TIM1_DeadTime);
  /* Set the Lock level, the Break enable Bit and the Ploarity, the OSSI State,
            the dead time value  and the Automatic Output Enable Bit */

  TIM1->BKR  =  (u8)((u8)TIM1_OSSIState       | \
                     (u8)TIM1_LockLevel       | \
                     (u8)TIM1_Break           | \
                     (u8)TIM1_BreakPolarity   | \
                     (u8)TIM1_AutomaticOutput);

}

/**
  * @brief Initializes the TIM1 peripheral according to the specified parameters.
  * @param[in]  TIM1_Channel specifies the input capture channel from TIM1_Channel_TypeDef.
  * @param[in]  TIM1_ICPolarity specifies the Input capture polarity from  TIM1_ICPolarity_TypeDef .
  * @param[in] TIM1_ICSelection specifies the Input capture source selection  from  TIM1_ICSelection_TypeDef.
  * @param[in] TIM1_ICPrescaler specifies the Input capture Prescaler from  TIM1_ICPSC_TypeDef.
  * @param[in] TIM1_ICFilter specifies the Input capture filter value.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TI1_Config
  * TI2_Config
  * TI3_Config
  * TI4_Config
  * TIM1_SetIC1Prescaler
  * TIM1_SetIC2Prescaler
  * TIM1_SetIC3Prescaler
  * TIM1_SetIC4Prescaler
  * @par Example:
  * Configure the TIM1 Input Capture according to specified parameters.
  * @code
  * TIM1_Channel_TypeDef My_Channel = TIM1_CHANNEL_1;
  * TIM1_ICPolarity_TypeDef My_ICPolarity = TIM1_ICPOLARITY_RISING;
  * TIM1_ICSelection_TypeDef My_ICSelection = TIM1_ICSELECTION_DIRECTTI;
  * TIM1_ICPSC_TypeDef My_ICPrescaler = TIM1_ICPSC_DIV1;
  *  u8 My_ICFilter = 0x00;
  * TIM1_ICInit( My_Channel, My_ICPolarity, My_ICSelection, My_ICPrescaler, My_ICFilter);
  * @endcode
  */

void TIM1_ICInit(TIM1_Channel_TypeDef TIM1_Channel,
                 TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                 TIM1_ICSelection_TypeDef TIM1_ICSelection,
                 TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
                 u8 TIM1_ICFilter)
{

  /* Check the parameters */
  assert_param(IS_TIM1_CHANNEL_OK(TIM1_Channel));
  assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
  assert_param(IS_TIM1_IC_SELECTION_OK(TIM1_ICSelection));
  assert_param(IS_TIM1_IC_PRESCALER_OK(TIM1_ICPrescaler));
  assert_param(IS_TIM1_IC_FILTER_OK(TIM1_ICFilter));

  if (TIM1_Channel == TIM1_CHANNEL_1)
  {
    /* TI1 Configuration */
    TI1_Config(TIM1_ICPolarity,
               TIM1_ICSelection,
               TIM1_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
  }
  else if (TIM1_Channel == TIM1_CHANNEL_2)
  {
    /* TI2 Configuration */
    TI2_Config(TIM1_ICPolarity,
               TIM1_ICSelection,
               TIM1_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
  }
  else if (TIM1_Channel == TIM1_CHANNEL_3)
  {
    /* TI3 Configuration */
    TI3_Config(TIM1_ICPolarity,
               TIM1_ICSelection,
               TIM1_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC3Prescaler(TIM1_ICPrescaler);
  }
  else
  {
    /* TI4 Configuration */
    TI4_Config(TIM1_ICPolarity,
               TIM1_ICSelection,
               TIM1_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC4Prescaler(TIM1_ICPrescaler);
  }

}

/**
  * @brief Configures the TIM1 peripheral in PWM Input Mode according to the specified parameters.
  * @param[in]  TIM1_Channel specifies the input capture channel from TIM1_Channel_TypeDef.
  * @param[in]  TIM1_ICPolarity specifies the Input capture polarity from  TIM1_ICPolarity_TypeDef .
  * @param[in] TIM1_ICSelection specifies the Input capture source selection  from  TIM1_ICSelection_TypeDef.
  * @param[in] TIM1_ICPrescaler specifies the Input capture Prescaler from  TIM1_ICPSC_TypeDef.
  * @param[in] TIM1_ICFilter specifies the Input capture filter value.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TI1_Config
  * TI2_Config
  * TIM1_SetIC1Prescaler
  * TIM1_SetIC2Prescaler
  * @par Example:
  * Configure the TIM1 peripheral in PWM Input Mode according to specified parameters.
  * @code
  * TIM1_Channel_TypeDef My_Channel = TIM1_CHANNEL_1;
  * TIM1_ICPolarity_TypeDef My_ICPolarity = TIM1_ICPOLARITY_RISING;
  * TIM1_ICSelection_TypeDef My_ICSelection = TIM1_ICSELECTION_DIRECTTI;
  * TIM1_ICPSC_TypeDef My_ICPrescaler = TIM1_ICPSC_DIV1;
  *  u8 My_ICFilter = 0x00;
  * TIM1_PWMIConfig( My_Channel, My_ICPolarity, My_ICSelection, My_ICPrescaler, My_ICFilter);
  * @endcode
  */
void TIM1_PWMIConfig(TIM1_Channel_TypeDef TIM1_Channel,
                     TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                     TIM1_ICSelection_TypeDef TIM1_ICSelection,
                     TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
                     u8 TIM1_ICFilter)
{
  u8 icpolarity = TIM1_ICPOLARITY_RISING;
  u8 icselection = TIM1_ICSELECTION_DIRECTTI;

  /* Check the parameters */
  assert_param(IS_TIM1_PWMI_CHANNEL_OK(TIM1_Channel));
  assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
  assert_param(IS_TIM1_IC_SELECTION_OK(TIM1_ICSelection));
  assert_param(IS_TIM1_IC_PRESCALER_OK(TIM1_ICPrescaler));

  /* Select the Opposite Input Polarity */
  if (TIM1_ICPolarity != TIM1_ICPOLARITY_FALLING)
  {
    icpolarity = TIM1_ICPOLARITY_FALLING;
  }
  else
  {
    icpolarity = TIM1_ICPOLARITY_RISING;
  }

  /* Select the Opposite Input */
  if (TIM1_ICSelection == TIM1_ICSELECTION_DIRECTTI)
  {
    icselection = TIM1_ICSELECTION_INDIRECTTI;
  }
  else
  {
    icselection = TIM1_ICSELECTION_DIRECTTI;
  }

  if (TIM1_Channel == TIM1_CHANNEL_1)
  {
    /* TI1 Configuration */
    TI1_Config(TIM1_ICPolarity, TIM1_ICSelection,
               TIM1_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICPrescaler);

    /* TI2 Configuration */
    TI2_Config(icpolarity, icselection, TIM1_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
  }
  else
  {
    /* TI2 Configuration */
    TI2_Config(TIM1_ICPolarity, TIM1_ICSelection,
               TIM1_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM1_SetIC2Prescaler(TIM1_ICPrescaler);

    /* TI1 Configuration */
    TI1_Config(icpolarity, icselection, TIM1_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
  }
}


/**
  * @brief Enables or disables the TIM1 peripheral.
  * @param[in] NewState new state of the TIM1 peripheral. This parameter can
  * be ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1 peripheral.
  * @code
  * TIM1_Cmd(ENABLE);
  * @endcode
  */
void TIM1_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* set or Reset the CEN Bit */
  if (NewState != DISABLE)
  {
    TIM1->CR1 |= TIM1_CR1_CEN;
  }
  else
  {
    TIM1->CR1 &= (u8)(~TIM1_CR1_CEN);
  }
}


/**
  * @brief Enables or disables the TIM1 peripheral Main Outputs.
  * @param[in] NewState new state of the TIM1 peripheral. This parameter can
  * be ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1 peripheral Main Outputs.
  * @code
  * TIM1_CtrlPWMOutputs(ENABLE);
  * @endcode
  */
void TIM1_CtrlPWMOutputs(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* Set or Reset the MOE Bit */

  if (NewState != DISABLE)
  {
    TIM1->BKR |= TIM1_BKR_MOE;
  }
  else
  {
    TIM1->BKR &= (u8)(~TIM1_BKR_MOE);
  }
}


/**
  * @brief Enables or disables the specified TIM1 interrupts.
  * @param[in] NewState new state of the TIM1 peripheral.
  * This parameter can be: ENABLE or DISABLE.
  * @param[in] TIM1_IT specifies the TIM1 interrupts sources to be enabled or disabled.
  * This parameter can be any combination of the following values:
  *                       - TIM1_IT_UPDATE: TIM1 update Interrupt source
  *                       - TIM1_IT_CC1: TIM1 Capture Compare 1 Interrupt source
  *                       - TIM1_IT_CC2: TIM1 Capture Compare 2 Interrupt source
  *                       - TIM1_IT_CC3: TIM1 Capture Compare 3 Interrupt source
  *                       - TIM1_IT_CC4: TIM1 Capture Compare 4 Interrupt source
  *                       - TIM1_IT_CCUpdate: TIM1 Capture Compare Update Interrupt source
  *                       - TIM1_IT_TRIGGER: TIM1 Trigger Interrupt source
  *                       - TIM1_IT_BREAK: TIM1 Break Interrupt source
  * @param[in] NewState new state of the TIM1 peripheral.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1_IT_UPDATE interrupt.
  * @code
  * TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);

⌨️ 快捷键说明

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