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

📄 stm32f10x_tim1.c

📁 STM32下做的usb转RS232程序
💻 C
📖 第 1 页 / 共 5 页
字号:
  /* Disable slave mode to clock the prescaler directly with the internal clock */
  TIM1->SMCR &=  SMCR_SMS_Mask;
}
/*******************************************************************************
* Function Name  : TIM1_ETRClockMode1Config
* Description    : Configures the TIM1 External clock Mode1
* Input          : - TIM1_ExtTRGPrescaler: The external Trigger Prescaler.
*                    It can be one of the following values:
*                       - TIM1_ExtTRGPSC_OFF
*                       - TIM1_ExtTRGPSC_DIV2
*                       - TIM1_ExtTRGPSC_DIV4
*                       - TIM1_ExtTRGPSC_DIV8.
*                  - TIM1_ExtTRGPolarity: The external Trigger Polarity.
*                    It can be one of the following values:
*                       - TIM1_ExtTRGPolarity_Inverted
*                       - TIM1_ExtTRGPolarity_NonInverted
*                  - ExtTRGFilter: External Trigger Filter.
*                    This parameter must be a value between 0x00 and 0x0F
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ETRClockMode1Config(u16 TIM1_ExtTRGPrescaler, u16 TIM1_ExtTRGPolarity,
                             u16 ExtTRGFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_EXT_PRESCALER(TIM1_ExtTRGPrescaler));
  assert_param(IS_TIM1_EXT_POLARITY(TIM1_ExtTRGPolarity));

  /* Configure the ETR Clock source */
  TIM1_ETRConfig(TIM1_ExtTRGPrescaler, TIM1_ExtTRGPolarity, ExtTRGFilter);

  /* Select the External clock mode1 */
  TIM1->SMCR &= SMCR_SMS_Mask;
  TIM1->SMCR |= TIM1_SlaveMode_External1;
  
  /* Select the Trigger selection : ETRF */
  TIM1->SMCR &= SMCR_TS_Mask;
  TIM1->SMCR |= TIM1_TS_ETRF;
}

/*******************************************************************************
* Function Name  : TIM1_ETRClockMode2Config
* Description    : Configures the TIM1 External clock Mode2
* Input          : - TIM1_ExtTRGPrescaler: The external Trigger Prescaler.
*                    It can be one of the following values:
*                       - TIM1_ExtTRGPSC_OFF
*                       - TIM1_ExtTRGPSC_DIV2
*                       - TIM1_ExtTRGPSC_DIV4
*                       - TIM1_ExtTRGPSC_DIV8
*                  - TIM1_ExtTRGPolarity: The external Trigger Polarity.
*                    It can be one of the following values:
*                       - TIM1_ExtTRGPolarity_Inverted
*                       - TIM1_ExtTRGPolarity_NonInverted
*                  - ExtTRGFilter: External Trigger Filter.
*                    This parameter must be a value between 0x00 and 0x0F
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ETRClockMode2Config(u16 TIM1_ExtTRGPrescaler, u16 TIM1_ExtTRGPolarity,
                             u16 ExtTRGFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_EXT_PRESCALER(TIM1_ExtTRGPrescaler));
  assert_param(IS_TIM1_EXT_POLARITY(TIM1_ExtTRGPolarity));

  /* Configure the ETR Clock source */
  TIM1_ETRConfig(TIM1_ExtTRGPrescaler, TIM1_ExtTRGPolarity, ExtTRGFilter);

  /* Enable the External clock mode2 */
  *(vu32 *) SMCR_ECE_BB = SMCR_ECE_Set;
}

/*******************************************************************************
* Function Name  : TIM1_ETRConfig
* Description    : Configures the TIM1 External Trigger (ETR).
* Input          : - TIM1_ExtTRGPrescaler: The external Trigger Prescaler.
*                    This parameter can be one of the following values:
*                       - TIM1_ExtTRGPSC_OFF
*                       - TIM1_ExtTRGPSC_DIV2
*                       - TIM1_ExtTRGPSC_DIV4
*                       - TIM1_ExtTRGPSC_DIV8
*                  - TIM1_ExtTRGPolarity: The external Trigger Polarity.
*                    This parameter can be one of the following values:
*                       - TIM1_ExtTRGPolarity_Inverted
*                       - TIM1_ExtTRGPolarity_NonInverted
*                  - ExtTRGFilter: External Trigger Filter.
*                    This parameter must be a value between 0x00 and 0x0F.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ETRConfig(u16 TIM1_ExtTRGPrescaler, u16 TIM1_ExtTRGPolarity,
                    u16 ExtTRGFilter)
{
  u32 tmpsmcr = 0;

  tmpsmcr = TIM1->SMCR;

  /* Set the Prescaler, the Filter value and the Polarity */
  tmpsmcr &= SMCR_ETR_Mask;
  tmpsmcr |= TIM1_ExtTRGPrescaler | TIM1_ExtTRGPolarity | (u16)((u16)ExtTRGFilter << 8);

  TIM1->SMCR = (u16)tmpsmcr;
}

/*******************************************************************************
* Function Name  : TIM1_ITRxExternalClockConfig
* Description    : Configures the TIM1 Internal Trigger as External Clock
* Input          : - TIM1_ITRSource: Internal Trigger source.
*                    This parameter can be one of the following values:
*                       - TIM1_TS_ITR0: Internal Trigger 0
*                       - TIM1_TS_ITR1: Internal Trigger 1
*                       - TIM1_TS_ITR2: Internal Trigger 2
*                       - TIM1_TS_ITR3: Internal Trigger 3
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ITRxExternalClockConfig(u16 TIM1_InputTriggerSource)
{
  /* Check the parameters */
  assert_param(IS_TIM1_INTERNAL_TRIGGER_SELECTION(TIM1_InputTriggerSource));
  
  /* Select the Internal Trigger */
  TIM1_SelectInputTrigger(TIM1_InputTriggerSource);

  /* Select the External clock mode1 */
  TIM1->SMCR |= TIM1_SlaveMode_External1;
}

/*******************************************************************************
* Function Name  : TIM1_TIxExternalClockConfig
* Description    : Configures the TIM1 Trigger as External Clock
* Input          : - TIM1_TIxExternalCLKSource: Trigger source.
*                    This parameter can be one of the following values:
*                       - TIM1_TS_TI1F_ED: TI1 Edge Detector
*                       - TIM1_TS_TI1FP1: Filtered TIM1 Input 1
*                       - TIM1_TS_TI2FP2: Filtered TIM1 Input 2
*                  - TIM1_ICPolarity: specifies the TIx Polarity.
*                    This parameter can be:
*                       - TIM1_ICPolarity_Rising
*                       - TIM1_ICPolarity_Falling
*                   - ICFilter : specifies the filter value.
*                     This parameter must be a value between 0x0 and 0xF.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_TIxExternalClockConfig(u16 TIM1_TIxExternalCLKSource,
                                u16 TIM1_ICPolarity, u8 ICFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_TIX_TRIGGER_SELECTION(TIM1_TIxExternalCLKSource));
  assert_param(IS_TIM1_IC_POLARITY(TIM1_ICPolarity));
  assert_param(IS_TIM1_IC_FILTER(ICFilter));

  /* Configure the TIM1 Input Clock Source */
  if (TIM1_TIxExternalCLKSource == TIM1_TIxExternalCLK1Source_TI2)
  {
    TI2_Config(TIM1_ICPolarity, TIM1_ICSelection_DirectTI, ICFilter);
  }
  else
  {
    TI1_Config(TIM1_ICPolarity, TIM1_ICSelection_DirectTI, ICFilter);
  }

  /* Select the Trigger source */
  TIM1_SelectInputTrigger(TIM1_TIxExternalCLKSource);

  /* Select the External clock mode1 */
  TIM1->SMCR |= TIM1_SlaveMode_External1;
}
/*******************************************************************************
* Function Name  : TIM1_SelectInputTrigger
* Description    : Selects the TIM1 Input Trigger source
* Input          : - TIM1_InputTriggerSource: The Trigger source.
*                    This parameter can be one of the following values:
*                       - TIM1_TS_ITR0: Internal Trigger 0
*                       - TIM1_TS_ITR1: Internal Trigger 1
*                       - TIM1_TS_ITR2: Internal Trigger 2
*                       - TIM1_TS_ITR3: Internal Trigger 3
*                       - TIM1_TS_TI1F_ED: TI1 Edge Detector
*                       - TIM1_TS_TI1FP1: Filtered Timer Input 1
*                       - TIM1_TS_TI2FP2: Filtered Timer Input 2
*                       - TIM1_TS_ETRF: External Trigger input
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_SelectInputTrigger(u16 TIM1_InputTriggerSource)
{
  u32 tmpsmcr = 0;

  /* Check the parameters */
  assert_param(IS_TIM1_TRIGGER_SELECTION(TIM1_InputTriggerSource));

  tmpsmcr = TIM1->SMCR;

  /* Select the Tgigger Source */
  tmpsmcr &= SMCR_TS_Mask;
  tmpsmcr |= TIM1_InputTriggerSource;

  TIM1->SMCR = (u16)tmpsmcr;
}

/*******************************************************************************
* Function Name  : TIM1_UpdateDisableConfig
* Description    : Enables or Disables the TIM1 Update event.
* Input          : - Newstate: new state of the TIM1 peripheral Preload register
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_UpdateDisableConfig(FunctionalState Newstate)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(Newstate));

  /* Set or Reset the UDIS Bit */
  *(vu32 *) CR1_UDIS_BB = (u16)Newstate;
}

/*******************************************************************************
* Function Name  : TIM1_UpdateRequestConfig
* Description    : Selects the TIM1 Update Request Interrupt source.
* Input          : - TIM1_UpdateSource: specifies the Update source.
*                    This parameter can be one of the following values:
*                       - TIM1_UpdateSource_Regular
*                       - TIM1_UpdateSource_Global
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_UpdateRequestConfig(u8 TIM1_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM1_UPDATE_SOURCE(TIM1_UpdateSource));

  /* Set or Reset the URS Bit */
  *(vu32 *) CR1_URS_BB = TIM1_UpdateSource;
}

/*******************************************************************************
* Function Name  : TIM1_SelectHallSensor
* Description    : Enables or disables the TIM1抯 Hall sensor interface.
* Input          : - Newstate: new state of the TIM1 Hall sensor interface
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_SelectHallSensor(FunctionalState Newstate)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(Newstate));

  /* Set or Reset the TI1S Bit */
  *(vu32 *) CR2_TI1S_BB = (u16)Newstate;
}

/*******************************************************************************
* Function Name  : TIM1_SelectOPM
* Description    : Enables or disables the TIM1抯 One Pulse Mode.
* Input          : - TIM1_OPMode: specifies the OPM Mode to be used.
*                    This parameter can be one of the following values:
*                    - TIM1_OPMode_Single
*                    - TIM1_OPMode_Repetitive
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_SelectOnePulseMode(u16 TIM1_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM1_OPM_MODE(TIM1_OPMode));

  /* Set or Reset the OPM Bit */
  *(vu32 *) CR1_OPM_BB = TIM1_OPMode;
}

/*******************************************************************************
* Function Name  : TIM1_SelectOutputTrigger
* Description    : Selects the TIM1 Trigger Output Mode.
* Input          : - TIM1_TRGOSource: specifies the Trigger Output source.
*                    This paramter can be one of the following values:
*                       - TIM1_TRGOSource_Reset
*                       - TIM1_TRGOSource_Enable
*                       - TIM1_TRGOSource_Update
*                       - TIM1_TRGOSource_OC1
*                       - TIM1_TRGOSource_OC1Ref
*                       - TIM1_TRGOSource_OC2Ref
*                       - TIM1_TRGOSource_OC3Ref
*                       - TIM1_TRGOSource_OC4Ref
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_SelectOutputTrigger(u16 TIM1_TRGOSource)
{
  u32 tmpcr2 = 0;

  /* Check the parameters */
  assert_param(IS_TIM1_TRGO_SOURCE(TIM1_TRGOSource));

  tmpcr2 = TIM1->CR2;

  /* Reset the MMS Bits */
  tmpcr2 &= CR2_MMS_Mask;

  /* Select the TRGO source */
  tmpcr2 |=  TIM1_TRGOSource;

  TIM1->CR2 = (u16)tmpcr2;
}

/*******************************************************************************
* Function Name  : TIM1_SelectSlaveMode
* Description    : Selects the TIM1 Slave Mode.
* Input          : - TIM1_SlaveMode: specifies the TIM1 Slave Mode.
*                    This paramter can be one of the following values:
*                       - TIM1_SlaveMode_Reset
*                       - TIM1_SlaveMode_Gated
*                       - TIM1_SlaveMode_Trigger
*                       - TIM1_SlaveMode_External1

⌨️ 快捷键说明

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