stm8l15x_tim4.c

来自「STM8L的tim4定时器使用」· C语言 代码 · 共 498 行 · 第 1/2 页

C
498
字号
{
  uint8_t tmpcntr = 0;
  tmpcntr = TIM4->CNTR;
  /* Get the Counter Register value */
  return ((uint8_t)tmpcntr);
}

/**
  * @brief  Gets the TIM4 Prescaler value.
  * @param  None
  * @retval TIM4 Prescaler can be one of the following values:
  *            - TIM4_Prescaler_1: Time base Prescaler = 1 (No effect)
  *            - TIM4_Prescaler_2: Time base Prescaler = 2
  *            - TIM4_Prescaler_4: Time base Prescaler = 4
  *            - TIM4_Prescaler_8: Time base Prescaler = 8
  *            - TIM4_Prescaler_16: Time base Prescaler = 16
  *            - TIM4_Prescaler_32: Time base Prescaler = 32
  *            - TIM4_Prescaler_64: Time base Prescaler = 64
  *            - TIM4_Prescaler_128: Time base Prescaler = 128
  *            - TIM4_Prescaler_256: Time base Prescaler = 256
  *            - TIM4_Prescaler_512: Time base Prescaler = 512
  *            - TIM4_Prescaler_1024: Time base Prescaler = 1024
  *            - TIM4_Prescaler_2048: Time base Prescaler = 2048
  *            - TIM4_Prescaler_4096: Time base Prescaler = 4096
  *            - TIM4_Prescaler_8192: Time base Prescaler = 8192
  *            - TIM4_Prescaler_16384: Time base Prescaler = 16384
  *            - TIM4_Prescaler_32768: Time base Prescaler = 32768  
  */
TIM4_Prescaler_TypeDef TIM4_GetPrescaler(void)
{
  /* Get the Prescaler Register value */
  return ((TIM4_Prescaler_TypeDef)TIM4->PSCR);
}

/**
  * @brief  Enables or Disables the TIM4 Update event.
  * @param  NewState: The new state of the TIM4 peripheral Preload register.
  *          This parameter can be ENABLE or DISABLE
  * @retval None
  */
void TIM4_UpdateDisableConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  /* Set or Reset the UDIS Bit */
  if (NewState != DISABLE)
  {
    TIM4->CR1 |= TIM4_CR1_UDIS ;
  }
  else
  {
    TIM4->CR1 &= (uint8_t)(~TIM4_CR1_UDIS) ;
  }
}

/**
  * @brief  Selects the TIM4 Update Request Interrupt source.
  * @param  TIM4_UpdateSource: Specifies the Update source.
  *          This parameter can be one of the following values:
  *            @arg TIM4_UpdateSource_Global: Global Update request source
  *            @arg TIM4_UpdateSource_Regular: Regular Update request source
  * @retval None
  */
void TIM4_UpdateRequestConfig(TIM4_UpdateSource_TypeDef TIM4_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM4_UPDATE_SOURCE(TIM4_UpdateSource));

  /* Set or Reset the URS Bit */
  if (TIM4_UpdateSource == TIM4_UpdateSource_Regular)
  {
    TIM4->CR1 |= TIM4_CR1_URS ;
  }
  else
  {
    TIM4->CR1 &= (uint8_t)(~TIM4_CR1_URS) ;
  }
}

/**
  * @brief  Enables or disables TIM4 peripheral Preload register on ARR.
  * @param  NewState: The new state of the TIM4 peripheral Preload register.
  *          This parameter can be ENABLE or DISABLE
  * @retval None
  */
void TIM4_ARRPreloadConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  /* Set or Reset the ARPE Bit */
  if (NewState != DISABLE)
  {
    TIM4->CR1 |= TIM4_CR1_ARPE ;
  }
  else
  {
    TIM4->CR1 &= (uint8_t)(~TIM4_CR1_ARPE) ;
  }
}

/**
  * @brief  Selects the TIM4抯 One Pulse Mode.
  * @param  TIM4_OPMode: Specifies the OPM Mode to be used.
  *          This parameter can be one of the following values:
  *            @arg TIM4_OPMode_Single: Single one Pulse mode (OPM Active)
  *            @arg TIM4_OPMode_Repetitive: Repetitive Pulse mode (OPM inactive)
  * @retval None
  */
void TIM4_SelectOnePulseMode(TIM4_OPMode_TypeDef TIM4_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM4_OPM_MODE(TIM4_OPMode));

  /* Set or Reset the OPM Bit */
  if (TIM4_OPMode == TIM4_OPMode_Single)
  {
    TIM4->CR1 |= TIM4_CR1_OPM ;
  }
  else
  {
    TIM4->CR1 &= (uint8_t)(~TIM4_CR1_OPM) ;
  }
}

/**
  * @brief  Enables or disables the TIM4 peripheral.
  * @param  NewState: The new state of the TIM4 peripheral.
  *          This parameter can be ENABLE or DISABLE
  * @retval None
  */
void TIM4_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

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

/**
  * @}
  */

/** @defgroup TIM4_Group2 Interrupts DMA and flags management functions
 *  @brief    Interrupts, DMA and flags management functions 
 *
@verbatim   
 ===============================================================================
                 Interrupts, DMA and flags management functions
 ===============================================================================  

@endverbatim
  * @{
  */

/**
  * @brief  Enables or disables the specified TIM4 interrupts.
  * @param  TIM4_IT: Specifies the TIM4 interrupts sources to be enabled or disabled.
  *          This parameter can be one of the following values:
  *            @arg TIM4_IT_Update: Update
  *            @arg TIM4_IT_Trigger: Trigger
  * @param  NewState: The new state of the TIM4 peripheral.
  *          This parameter can be ENABLE or DISABLE
  * @retval None
  */
void TIM4_ITConfig(TIM4_IT_TypeDef TIM4_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_TIM4_IT(TIM4_IT));
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    /* Enable the Interrupt sources */
    TIM4->IER |= (uint8_t)TIM4_IT;
  }
  else
  {
    /* Disable the Interrupt sources */
    TIM4->IER &= (uint8_t)(~(uint8_t)TIM4_IT);
  }
}

/**
  * @brief  Configures the TIM4 event to be generated by software.
  * @param  TIM4_EventSource: Specifies the event source.
  *          This parameter can be one of the following values:
  *            @arg TIM4_EventSource_Update: Update
  *            @arg TIM4_EventSource_Trigger: Trigger
  * @retval None
  */
void TIM4_GenerateEvent(TIM4_EventSource_TypeDef TIM4_EventSource)
{
  /* Check the parameters */
  assert_param(IS_TIM4_EVENT_SOURCE((uint8_t)TIM4_EventSource));

  /* Set the event sources */
  TIM4->EGR |= (uint8_t)TIM4_EventSource;
}

/**
  * @brief  Checks whether the specified TIM4 flag is set or not.
  * @param  TIM4_FLAG: Specifies the flag to check.
  *          This parameter can be one of the following values:
  *            @arg TIM4_FLAG_Update: Update
  *            @arg TIM4_FLAG_Trigger: Trigger
  * @retval FlagStatus: The new state of TIM4_FLAG.
  *          This parameter can be SET or RESET.
  */
FlagStatus TIM4_GetFlagStatus(TIM4_FLAG_TypeDef TIM4_FLAG)
{
  FlagStatus bitstatus = RESET;

  /* Check the parameters */
  assert_param(IS_TIM4_GET_FLAG(TIM4_FLAG));

  if ((TIM4->SR1 & (uint8_t)TIM4_FLAG)  != 0)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return ((FlagStatus)bitstatus);
}

/**
  * @brief  Clears the TIM抯 pending flags.
  * @param  TIM4_FLAG: Specifies the flag to clear.
  *          This parameter can be one of the following values:
  *            @arg TIM4_FLAG_Update: Update
  *            @arg TIM4_FLAG_Trigger: Trigger
  * @retval None
  */
void TIM4_ClearFlag(TIM4_FLAG_TypeDef TIM4_FLAG)
{
  /* Check the parameters */
  assert_param(IS_TIM4_CLEAR_FLAG((uint8_t)TIM4_FLAG));
  /* Clear the flags (rc_w0) clear this bit by writing 0. Writing 

⌨️ 快捷键说明

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