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

📄 stm32f10x_tim.c

📁 中文固件库.rar
💻 C
📖 第 1 页 / 共 5 页
字号:
  /* Get the TIMx SMCR register value */
  tmpsmcr = TIMx->SMCR;
  /* Reset the SMS Bits */
  tmpsmcr &= SMCR_SMS_Mask;
  /* Select the External clock mode1 */
  tmpsmcr |= TIM_SlaveMode_External1;
  /* Select the Trigger selection : ETRF */
  tmpsmcr &= SMCR_TS_Mask;
  tmpsmcr |= TIM_TS_ETRF;
  /* Write to TIMx SMCR */
  TIMx->SMCR = tmpsmcr;
}

/**
  * @brief  Configures the External clock Mode2
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  *   It can be one of the following values:
  * @arg TIM_ExtTRGPSC_OFF
  * @arg TIM_ExtTRGPSC_DIV2
  * @arg TIM_ExtTRGPSC_DIV4
  * @arg TIM_ExtTRGPSC_DIV8
  * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  *   It can be one of the following values:
  * @arg TIM_ExtTRGPolarity_Inverted
  * @arg TIM_ExtTRGPolarity_NonInverted
  * @param ExtTRGFilter: External Trigger Filter.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval : None
  */
void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, 
                             uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  /* Configure the ETR Clock source */
  TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  /* Enable the External clock mode2 */
  TIMx->SMCR |= SMCR_ECE_Set;
}

/**
  * @brief  Configures the TIMx External Trigger (ETR).
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  *   This parameter can be one of the following values:
  * @arg TIM_ExtTRGPSC_OFF
  * @arg TIM_ExtTRGPSC_DIV2
  * @arg TIM_ExtTRGPSC_DIV4
  * @arg TIM_ExtTRGPSC_DIV8
  * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  *   This parameter can be one of the following values:
  * @arg TIM_ExtTRGPolarity_Inverted
  * @arg TIM_ExtTRGPolarity_NonInverted
  * @param ExtTRGFilter: External Trigger Filter.
  *   This parameter must be a value between 0x00 and 0x0F.
  * @retval : None
  */
void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
                   uint16_t ExtTRGFilter)
{
  uint16_t tmpsmcr = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  tmpsmcr = TIMx->SMCR;
  /* Reset the ETR Bits */
  tmpsmcr &= SMCR_ETR_Mask;
  /* Set the Prescaler, the Filter value and the Polarity */
  tmpsmcr |= TIM_ExtTRGPrescaler | TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << 8);
  /* Write to TIMx SMCR */
  TIMx->SMCR = tmpsmcr;
}

/**
  * @brief  Configures the TIMx Prescaler.
  * @param TIMx: where x can be  1 to 8 to select the TIM peripheral.
  * @param Prescaler: specifies the Prescaler Register value
  * @param TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
  *   This parameter can be one of the following values:
  * @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at
  *   the update event.
  * @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded
  *   immediatly.
  * @retval : None
  */
void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
{
  /* Check the parameters */
  assert_param(IS_TIM_ALL_PERIPH(TIMx));
  assert_param(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));
  /* Set the Prescaler value */
  TIMx->PSC = Prescaler;
  /* Set or reset the UG Bit */
  TIMx->EGR = TIM_PSCReloadMode;
}

/**
  * @brief  Specifies the TIMx Counter Mode to be used.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_CounterMode: specifies the Counter Mode to be used
  *   This parameter can be one of the following values:
  * @arg TIM_CounterMode_Up: TIM Up Counting Mode
  * @arg TIM_CounterMode_Down: TIM Down Counting Mode
  * @arg TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
  * @arg TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
  * @arg TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
  * @retval : None
  */
void TIM_CounterModeConfig(TIM_TypeDef* TIMx, uint16_t TIM_CounterMode)
{
  uint16_t tmpcr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_COUNTER_MODE(TIM_CounterMode));
  tmpcr1 = TIMx->CR1;
  /* Reset the CMS and DIR Bits */
  tmpcr1 &= CR1_CounterMode_Mask;
  /* Set the Counter Mode */
  tmpcr1 |= TIM_CounterMode;
  /* Write to TIMx CR1 register */
  TIMx->CR1 = tmpcr1;
}

/**
  * @brief  Selects the Input Trigger source
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_InputTriggerSource: The Input Trigger source.
  *   This parameter can be one of the following values:
  * @arg TIM_TS_ITR0: Internal Trigger 0
  * @arg TIM_TS_ITR1: Internal Trigger 1
  * @arg TIM_TS_ITR2: Internal Trigger 2
  * @arg TIM_TS_ITR3: Internal Trigger 3
  * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  * @arg TIM_TS_ETRF: External Trigger input
  * @retval : None
  */
void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
{
  uint16_t tmpsmcr = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));
  /* Get the TIMx SMCR register value */
  tmpsmcr = TIMx->SMCR;
  /* Reset the TS Bits */
  tmpsmcr &= SMCR_TS_Mask;
  /* Set the Input Trigger source */
  tmpsmcr |= TIM_InputTriggerSource;
  /* Write to TIMx SMCR */
  TIMx->SMCR = tmpsmcr;
}

/**
  * @brief  Configures the TIMx Encoder Interface.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_EncoderMode: specifies the TIMx Encoder Mode.
  *   This parameter can be one of the following values:
  * @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge
  *   depending on TI2FP2 level.
  * @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge
  *   depending on TI1FP1 level.
  * @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and
  *   TI2FP2 edges depending on the level of the other input.
  * @param TIM_IC1Polarity: specifies the IC1 Polarity
  *   This parmeter can be one of the following values:
  * @arg TIM_ICPolarity_Falling: IC Falling edge.
  * @arg TIM_ICPolarity_Rising: IC Rising edge.
  * @param TIM_IC2Polarity: specifies the IC2 Polarity
  *   This parmeter can be one of the following values:
  * @arg TIM_ICPolarity_Falling: IC Falling edge.
  * @arg TIM_ICPolarity_Rising: IC Rising edge.
  * @retval : None
  */
void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
                                uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
{
  uint16_t tmpsmcr = 0;
  uint16_t tmpccmr1 = 0;
  uint16_t tmpccer = 0;
    
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
  assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
  assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
  /* Get the TIMx SMCR register value */
  tmpsmcr = TIMx->SMCR;
  /* Get the TIMx CCMR1 register value */
  tmpccmr1 = TIMx->CCMR1;
  /* Get the TIMx CCER register value */
  tmpccer = TIMx->CCER;
  /* Set the encoder Mode */
  tmpsmcr &= SMCR_SMS_Mask;
  tmpsmcr |= TIM_EncoderMode;
  /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  tmpccmr1 &= CCMR_CC13S_Mask & CCMR_CC24S_Mask;
  tmpccmr1 |= CCMR_TI13Direct_Set | CCMR_TI24Direct_Set;
  /* Set the TI1 and the TI2 Polarities */
  tmpccer &= CCER_CC1P_Reset & CCER_CC2P_Reset;
  tmpccer |= (TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << 4));
  /* Write to TIMx SMCR */
  TIMx->SMCR = tmpsmcr;
  /* Write to TIMx CCMR1 */
  TIMx->CCMR1 = tmpccmr1;
  /* Write to TIMx CCER */
  TIMx->CCER = tmpccer;
}

/**
  * @brief  Forces the TIMx output 1 waveform to active or inactive level.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ForcedAction: specifies the forced Action to be set to
  *   the output waveform.
  *   This parameter can be one of the following values:
  * @arg TIM_ForcedAction_Active: Force active level on OC1REF
  * @arg TIM_ForcedAction_InActive: Force inactive level on
  *   OC1REF.
  * @retval : None
  */
void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
  uint16_t tmpccmr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  tmpccmr1 = TIMx->CCMR1;
  /* Reset the OC1M Bits */
  tmpccmr1 &= CCMR_OC13M_Mask;
  /* Configure The Forced output Mode */
  tmpccmr1 |= TIM_ForcedAction;
  /* Write to TIMx CCMR1 register */
  TIMx->CCMR1 = tmpccmr1;
}

/**
  * @brief  Forces the TIMx output 2 waveform to active or inactive level.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ForcedAction: specifies the forced Action to be set to
  *   the output waveform.
  *   This parameter can be one of the following values:
  * @arg TIM_ForcedAction_Active: Force active level on OC2REF
  * @arg TIM_ForcedAction_InActive: Force inactive level on
  *   OC2REF.
  * @retval : None
  */
void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
  uint16_t tmpccmr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  tmpccmr1 = TIMx->CCMR1;
  /* Reset the OC2M Bits */
  tmpccmr1 &= CCMR_OC24M_Mask;
  /* Configure The Forced output Mode */
  tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  /* Write to TIMx CCMR1 register */
  TIMx->CCMR1 = tmpccmr1;
}

/**
  * @brief  Forces the TIMx output 3 waveform to active or inactive level.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ForcedAction: specifies the forced Action to be set to
  *   the output waveform.
  *   This parameter can be one of the following values:
  * @arg TIM_ForcedAction_Active: Force active level on OC3REF
  * @arg TIM_ForcedAction_InActive: Force inactive level on
  *   OC3REF.
  * @retval : None
  */
void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
  uint16_t tmpccmr2 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  tmpccmr2 = TIMx->CCMR2;
  /* Reset the OC1M Bits */
  tmpccmr2 &= CCMR_OC13M_Mask;
  /* Configure The Forced output Mode */
  tmpccmr2 |= TIM_ForcedAction;
  /* Write to TIMx CCMR2 register */
  TIMx->CCMR2 = tmpccmr2;
}

/**
  * @brief  Forces the TIMx output 4 waveform to active or inactive level.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param TIM_ForcedAction: specifies the forced Action to be set to
  *   the output waveform.
  *   This parameter can be one of the following values:
  * @arg TIM_ForcedAction_Active: Force active level on OC4REF
  * @arg TIM_ForcedAction_InActive: Force inactive level on
  *   OC4REF.
  * @retval : None
  */
void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
  uint16_t tmpccmr2 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_123458_PERIPH(TIMx));
  assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  tmpccmr2 = TIMx->CCMR2;
  /* Reset the OC2M Bits */
  tmpccmr2 &= CCMR_OC24M_Mask;
  /* Configure The Forced output Mode */
  tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  /* Write to TIMx CCMR2 register */
  TIMx->CCMR2 = tmpccmr2;
}

/**
  * @brief  Enables or disables TIMx peripheral Preload register on ARR.
  * @param TIMx: where x can be  1, 2, 3, 4, 5 or 8 to select the TIM 
  *   peripheral.
  * @param NewState: new state of the TIMx peripheral Preload register
  *   This parameter can be: ENABLE or DISABLE.
  * @retval : None
  */
void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_TIM_ALL_PERIPH(TIMx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    /* Set the ARR Preload Bit */
    TIMx->CR1 |= CR1_ARPE_Set;
  }
  else
  {
    /* Reset the ARR Preload Bit */
    TIMx->CR1 &= CR1_ARPE_Reset;
  }
}

/**
  * @brief  Selects the TIM peripheral Commutation event.
  * @param TIMx: where x can be  1 or 8 to select the TIMx peripheral
  * @param NewState: new state of the Commutation event.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval : None

⌨️ 快捷键说明

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