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

📄 stm8l15x_tim1.c

📁 STM8L的tim4定时器使用
💻 C
📖 第 1 页 / 共 5 页
字号:
  tmpcntrh = TIM1->CNTRH;
  tmpcntrl = TIM1->CNTRL;

  tmpcntr  = (uint16_t)(tmpcntrl);
  tmpcntr |= (uint16_t)((uint16_t)tmpcntrh << 8);

  /* Get the Counter Register value */
  return (uint16_t)tmpcntr;
}

/**
  * @brief  Gets the TIM1 Prescaler value.
  * @param  None
  * @retval Prescaler Register value.
  */
uint16_t TIM1_GetPrescaler(void)
{
  uint16_t tmpreg = 0;
  tmpreg = (uint16_t)((uint16_t)TIM1->PSCRH << 8);
  /* Get the Prescaler Register value */
  return (uint16_t)(tmpreg | TIM1->PSCRL);
}

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

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

/**
  * @brief  Selects the TIM1 Update Request Interrupt source.
  * @param  TIM1_UpdateSource specifies the Update source.
  *          This parameter can be one of the following values
  *            @arg TIM1_UpdateSource_Regular
  *            @arg TIM1_UpdateSource_Global
  * @retval None
  */
void TIM1_UpdateRequestConfig(TIM1_UpdateSource_TypeDef TIM1_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM1_UPDATE_SOURCE(TIM1_UpdateSource));

  /* Set or Reset the URS Bit */
  if (TIM1_UpdateSource != TIM1_UpdateSource_Global)
  {
    TIM1->CR1 |= TIM1_CR1_URS;
  }
  else
  {
    TIM1->CR1 &= (uint8_t)(~TIM1_CR1_URS);
  }
}

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

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

/**
  * @brief  Selects the TIM1抯 One Pulse Mode.
  * @param  TIM1_OPMode specifies the OPM Mode to be used.
  *          This parameter can be one of the following values
  *            @arg TIM1_OPMode_Single
  *            @arg TIM1_OPMode_Repetitive
  * @retval None
  */
void TIM1_SelectOnePulseMode(TIM1_OPMode_TypeDef TIM1_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM1_OPM_MODE(TIM1_OPMode));

  /* Set or Reset the OPM Bit */
  if (TIM1_OPMode != TIM1_OPMode_Repetitive)
  {
    TIM1->CR1 |= TIM1_CR1_OPM;
  }
  else
  {
    TIM1->CR1 &= (uint8_t)(~TIM1_CR1_OPM);
  }
}

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

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

/**
  * @}
  */

/** @defgroup TIM1_Group2 Output Compare management functions
 *  @brief    Output Compare management functions 
 *
@verbatim   
 ===============================================================================
                        Output Compare management functions
 ===============================================================================  
   
       ===================================================================      
              TIM1 Driver: how to use it in Output Compare Mode
       =================================================================== 
       To use the Timer in Output Compare mode, the following steps are mandatory:
       
       1. Enable TIM1 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE) function.
       
       2. Configure the TIM1 pins in output mode by configuring the corresponding GPIO pins
          
       3. Configure the Time base unit as described in the first part of this driver, if needed,
          otherwise the Timer will run with the default configuration:
          - Autoreload value = 0xFFFF
          - Prescaler value = 0x0000
          - Counter mode = Up counting
      
       4. Call TIM1_OCxInit() to configure the channel x with the desired parameters
          including:
          - TIM1 Output Compare mode: TIM1_OCMode
          - TIM1 Output State: TIM1_OutputState
          - TIM1 Complementary Output State: TIM1_OutputNState
          - TIM1 Pulse value: TIM1_Pulse
          - TIM1 Output Compare Polarity : TIM1_OCPolarity
          - TIM1 Complementary Output Compare Polarity : TIM1_OCNPolarity
          - TIM1 Output Idle State: TIM1_OCIdleState
          - TIM1 Complementary Output Idle State: TIM1_OCNIdleState
       
       5. Call the TIM1_Cmd(ENABLE) function to enable the TIM1 counter.
       
       Note1: All other functions can be used separately to modify, if needed,
          a specific feature of the Timer. 
       
       Note2: If the corresponding interrupt or DMA request are needed, the user should:
              1. Enable global interrupts (or the DMA) to use the TIM1 interrupts (or DMA requests). 
              2. Enable the corresponding interrupt (or DMA request) using the function 
              TIM1_ITConfig(TIM1_IT_CCx) (or TIM1_DMACmd(TIM1_DMASource_CCx))   

@endverbatim
  * @{
  */
  
/**
  * @brief  Initializes the TIM1 Channel1 according to the specified parameters.
  * @param  TIM1_OCMode specifies the Output Compare mode
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCMode_Timing: OC Mode Timing
  *            @arg TIM1_OCMode_Active: OC Mode Active
  *            @arg TIM1_OCMode_Inactive: OC Mode Inactive
  *            @arg TIM1_OCMode_Toggle: OC Mode Toggle
  *            @arg TIM1_OCMode_PWM1: OC Mode PWM1
  *            @arg TIM1_OCMode_PWM2: OC Mode PWM2          
  * @param  TIM1_OutputState specifies the Output State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OutputState_Disable: Output state disable
  *            @arg TIM1_OutputState_Enable: Output state enable
  * @param  TIM1_OutputNState specifies the Complementary Output State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OutputNState_Disable: Output N state disable
  *            @arg TIM1_OutputNState_Enable: Output N state enable
  * @param  TIM1_Pulse specifies the Pulse width value.
  * @param  TIM1_OCPolarity specifies the Output Compare Polarity
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCPolarity_High: Output Compare active low
  *            @arg TIM1_OCPolarity_Low: Output Compare active high
  * @param  TIM1_OCNPolarity specifies the Complementary Output Compare Polarity 
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCNPolarity_High: Complementary Output Compare active low
  *            @arg TIM1_OCNPolarity_Low: Complementary Output Compare active high
  * @param  TIM1_OCIdleState specifies the Output Compare Idle State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCIdleState_Set: Output Compare Idle state set
  *            @arg TIM1_OCIdleState_Reset: Output Compare Idle state reset
  * @param  TIM1_OCNIdleState specifies the Complementary Output Compare Idle State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCNIdleState_Set: Complementary Output Compare Idle state set
  *            @arg TIM1_OCNIdleState_Reset: Complementary Output Compare Idle state reset
  * @retval None
  */
void TIM1_OC1Init(TIM1_OCMode_TypeDef TIM1_OCMode,
                  TIM1_OutputState_TypeDef TIM1_OutputState,
                  TIM1_OutputNState_TypeDef TIM1_OutputNState,
                  uint16_t TIM1_Pulse,
                  TIM1_OCPolarity_TypeDef TIM1_OCPolarity,
                  TIM1_OCNPolarity_TypeDef TIM1_OCNPolarity,
                  TIM1_OCIdleState_TypeDef TIM1_OCIdleState,
                  TIM1_OCNIdleState_TypeDef TIM1_OCNIdleState)
{
  /* Check the parameters */
  assert_param(IS_TIM1_OC_MODE(TIM1_OCMode));
  assert_param(IS_TIM1_OUTPUT_STATE(TIM1_OutputState));
  assert_param(IS_TIM1_OUTPUTN_STATE(TIM1_OutputNState));
  assert_param(IS_TIM1_OC_POLARITY(TIM1_OCPolarity));
  assert_param(IS_TIM1_OCN_POLARITY(TIM1_OCNPolarity));
  assert_param(IS_TIM1_OCIDLE_STATE(TIM1_OCIdleState));
  assert_param(IS_TIM1_OCNIDLE_STATE(TIM1_OCNIdleState));

  /* Disable the Channel 1: Reset the CCE Bit, Set the Output State ,
  the Output N State, the Output Polarity & the Output N Polarity*/
  TIM1->CCER1 &= (uint8_t)(~(TIM1_CCER1_CC1E | TIM1_CCER1_CC1NE
                             | TIM1_CCER1_CC1P | TIM1_CCER1_CC1NP));
  /* Set the Output State & Set the Output N State & Set the Output Polarity
  & Set the Output N Polarity */
  TIM1->CCER1 |= (uint8_t)((uint8_t)((uint8_t)(TIM1_OutputState & TIM1_CCER1_CC1E)
                                     | (uint8_t)(TIM1_OutputNState & TIM1_CCER1_CC1NE))
                           | (uint8_t)( (uint8_t)(TIM1_OCPolarity  & TIM1_CCER1_CC1P)
                                        | (uint8_t)(TIM1_OCNPolarity & TIM1_CCER1_CC1NP)));

  /* Reset the Output Compare Bits & Set the Output Compare Mode */
  TIM1->CCMR1 = (uint8_t)((uint8_t)(TIM1->CCMR1 & (uint8_t)(~TIM1_CCMR_OCM))
                          | (uint8_t)TIM1_OCMode);

  /* Reset the Output Idle state & the Output N Idle state bits */
  TIM1->OISR &= (uint8_t)(~(TIM1_OISR_OIS1 | TIM1_OISR_OIS1N));
  /* Set the Output Idle state & the Output N Idle state configuration */
  TIM1->OISR |= (uint8_t)((uint8_t)(TIM1_OCIdleState & TIM1_OISR_OIS1)
                          | (uint8_t)(TIM1_OCNIdleState & TIM1_OISR_OIS1N));

  /* Set the Pulse value */
  TIM1->CCR1H = (uint8_t)(TIM1_Pulse >> 8);
  TIM1->CCR1L = (uint8_t)(TIM1_Pulse);
}

/**
  * @brief  Initializes the TIM1 Channel2 according to the specified parameters.
  * @param  TIM1_OCMode specifies the Output Compare mode
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCMode_Timing: OC Mode Timing
  *            @arg TIM1_OCMode_Active: OC Mode Active
  *            @arg TIM1_OCMode_Inactive: OC Mode Inactive
  *            @arg TIM1_OCMode_Toggle: OC Mode Toggle
  *            @arg TIM1_OCMode_PWM1: OC Mode PWM1
  *            @arg TIM1_OCMode_PWM2: OC Mode PWM2  
  * @param  TIM1_OutputState specifies the Output State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OutputState_Disable: Output state disable
  *            @arg TIM1_OutputState_Enable: Output state enable
  * @param  TIM1_OutputNState specifies the Complementary Output State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OutputNState_Disable: Output N state disable
  *            @arg TIM1_OutputNState_Enable: Output N state enable
  * @param  TIM1_Pulse specifies the Pulse width value.
  * @param  TIM1_OCPolarity specifies the Output Compare Polarity
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCPolarity_High: Output Compare active low
  *            @arg TIM1_OCPolarity_Low: Output Compare active high
  * @param  TIM1_OCNPolarity specifies the Complementary Output Compare
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCNPolarity_High: Complementary Output Compare active low
  *            @arg TIM1_OCNPolarity_Low: Complementary Output Compare active high
  * @param  TIM1_OCIdleState specifies the Output Compare Idle State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCIdleState_Set: Output Compare Idle state set
  *            @arg TIM1_OCIdleState_Reset: Output Compare Idle state reset
  * @param  TIM1_OCNIdleState specifies the Complementary Output Compare Idle State
  *          This parameter can be one of the following values:
  *            @arg TIM1_OCNIdleState_Set: Complementary Output Compare Idle state set
  *            @arg TIM1_OCNIdleState_Reset: Complementary Output Compare Idle state reset
  * @retval None
  */
void TIM1_OC2Init(TIM1_OCMode_TypeDef TIM1_OCMode,
                  TIM1_OutputState_TypeDef TIM1_OutputState,
                  TIM1_OutputNState_TypeDef TIM1_OutputNState,
                  uint16_t TIM1_Pulse,
                  TIM1_OCPolarity_TypeDef TIM1_OCPolarity,
                  TIM1_OCNPolarity_TypeDef TIM1_OCNPolarity,
                  TIM1_OCIdleState_TypeDef TIM1_OCIdleState,
                  TIM1_OCNIdleState_TypeDef TIM1_OCNIdleState)
{

  /* Check the parameters */
  assert_param(IS_TIM1_OC_MODE(TIM1_OCMode));
  assert_param(IS_TIM1_OUTPUT_STATE(TIM1_OutputState));
  assert_param(IS_TIM1_OUTPUTN_STATE(TIM1_OutputNState));
  assert_param(IS_TIM1_OC_POLARITY(TIM1_OCPolarity));
  assert_param(IS_TIM1_OCN_POLARITY(TIM1_OCNPolarity));
  assert_param(IS_TIM1_OCIDLE_STATE(TIM1_OCIdleState));
  assert_param(IS_TIM1_OCNIDLE_STATE(TIM1_OCNIdleState));

  /* Disable the Channel 1: Reset the CCE Bit, Set the Output State,
     the Output N State, the Output Polarity & the Output N Polarity*/
  TIM1->CCER1 &= (uint8_t)(~(TIM1_CCER1_CC2E | TIM1_CCER1_CC2NE | TIM1_CCER1_CC2P | TIM1_CCER1_CC2NP));

  /* Set the Output State & Set the Output N State & Set the Output Polarity & Set the Output N Polarity */
  TIM1->CCER1 |= (uint8_t)((uint8_t)((uint8_t)(TIM1_OutputState & TIM1_CCER1_CC2E) | (uint8_t)(TIM1_OutputNState & TIM1_CCER1_CC2NE))
                           | (uint8_t) ((uint8_t)(TIM1_OCPolarity & TIM1_CCER1_CC2P) | (uint8_t)(TIM1_OCNPolarity & TIM1_CCER1_CC2NP)));

  /* Reset the Output Compare Bits & Set the Output Compare Mode */
  TIM1->CCMR2 = (uint8_t)((uint8_t)(TIM1->CCMR2 & (uint8_t)(~TIM1_CCMR_OCM)) | (uint8_t)TIM1_OCMode);

  /* Reset the Output Idle state & the Output N Idle state bits */
  TIM1->OISR &= (uint8_t)(~(TIM1_OISR_OIS2 | TIM1_OISR_OIS2N));
  /* Set the Output Idle state & the Output N Idle state configuration */
  TIM1->OISR |= (uint8_t)((uint8_t)(TIM1_OISR_OIS2 & TIM1_OCIdleState) | (uint8_t)(TIM1_OISR_OIS2N & TIM1_OCNIdleState));

  /* Set the Pulse value */
  TIM1->CCR2H = (uint8_t)(TIM1_Pulse >> 8);

⌨️ 快捷键说明

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