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

📄 stm32f10x_tim1.c

📁 STM32F RFID通讯源代码(支持双向发送接收)
💻 C
📖 第 1 页 / 共 5 页
字号:

  /* Select the Opposite Input */
  if (TIM1_ICInitStruct->TIM1_ICSelection == TIM1_ICSelection_DirectTI)
  {
    ICSelection = TIM1_ICSelection_IndirectTI;
  }
  else
  {
    ICSelection = TIM1_ICSelection_DirectTI;
  }

  if (TIM1_ICInitStruct->TIM1_Channel == TIM1_Channel_1)
  {
    /* TI1 Configuration */
    TI1_Config(TIM1_ICInitStruct->TIM1_ICPolarity, TIM1_ICInitStruct->TIM1_ICSelection,
               TIM1_ICInitStruct->TIM1_ICFilter);

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

    /* TI2 Configuration */
    TI2_Config(ICPolarity, ICSelection, TIM1_ICInitStruct->TIM1_ICFilter);

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

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

    /* TI1 Configuration */
    TI1_Config(ICPolarity, ICSelection, TIM1_ICInitStruct->TIM1_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICInitStruct->TIM1_ICPrescaler);
  }
}
/*******************************************************************************
* Function Name  : TIM1_OCStructInit
* Description    : Fills each TIM1_OCInitStruct member with its default value.
* Input          : - TIM1_OCInitStruct : pointer to a TIM1_OCInitTypeDef structure
*                    which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_OCStructInit(TIM1_OCInitTypeDef* TIM1_OCInitStruct)
{
  /* Set the default configuration */
  TIM1_OCInitStruct->TIM1_OCMode = TIM1_OCMode_Timing;
  TIM1_OCInitStruct->TIM1_OutputState = TIM1_OutputState_Disable;
  TIM1_OCInitStruct->TIM1_OutputNState = TIM1_OutputNState_Disable;
  TIM1_OCInitStruct->TIM1_Pulse = TIM1_Pulse_Reset_Mask;
  TIM1_OCInitStruct->TIM1_OCPolarity = TIM1_OCPolarity_High;
  TIM1_OCInitStruct->TIM1_OCNPolarity = TIM1_OCPolarity_High;
  TIM1_OCInitStruct->TIM1_OCIdleState = TIM1_OCIdleState_Reset;
  TIM1_OCInitStruct->TIM1_OCNIdleState = TIM1_OCNIdleState_Reset;
}

/*******************************************************************************
* Function Name  : TIM1_ICStructInit
* Description    : Fills each TIM1_InitStruct member with its default value.
* Input          : - TIM1_ICInitStruct : pointer to a TIM1_ICInitTypeDef structure
*                    which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ICStructInit(TIM1_ICInitTypeDef* TIM1_ICInitStruct)
{
  /* Set the default configuration */
  TIM1_ICInitStruct->TIM1_Channel = TIM1_Channel_1;
  TIM1_ICInitStruct->TIM1_ICSelection = TIM1_ICSelection_DirectTI;
  TIM1_ICInitStruct->TIM1_ICPolarity = TIM1_ICPolarity_Rising;
  TIM1_ICInitStruct->TIM1_ICPrescaler = TIM1_ICPSC_DIV1;
  TIM1_ICInitStruct->TIM1_ICFilter = TIM1_ICFilter_Mask;
}

/*******************************************************************************
* Function Name  : TIM1_TimeBaseStructInit
* Description    : Fills each TIM1_TimeBaseInitStruct member with its default value.
* Input          : - TIM1_TimeBaseInitStruct : pointer to a TIM1_TimeBaseInitTypeDef
*                    structure which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_TimeBaseStructInit(TIM1_TimeBaseInitTypeDef* TIM1_TimeBaseInitStruct)
{
  /* Set the default configuration */
  TIM1_TimeBaseInitStruct->TIM1_Period = TIM1_Period_Reset_Mask;
  TIM1_TimeBaseInitStruct->TIM1_Prescaler = TIM1_Prescaler_Reset_Mask;
  TIM1_TimeBaseInitStruct->TIM1_ClockDivision = TIM1_CKD_DIV1;
  TIM1_TimeBaseInitStruct->TIM1_CounterMode = TIM1_CounterMode_Up;
  TIM1_TimeBaseInitStruct->TIM1_RepetitionCounter = TIM1_RepetitionCounter_Reset_Mask;
}

/*******************************************************************************
* Function Name  : TIM1_BDTRStructInit
* Description    : Fills each TIM1_BDTRInitStruct member with its default value.
* Input          : - TIM1_BDTRInitStruct : pointer to a TIM1_BDTRInitTypeDef
*                    structure which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_BDTRStructInit(TIM1_BDTRInitTypeDef* TIM1_BDTRInitStruct)
{
  /* Set the default configuration */
  TIM1_BDTRInitStruct->TIM1_OSSRState = TIM1_OSSRState_Disable;
  TIM1_BDTRInitStruct->TIM1_OSSIState = TIM1_OSSIState_Disable;
  TIM1_BDTRInitStruct->TIM1_LOCKLevel = TIM1_LOCKLevel_OFF;
  TIM1_BDTRInitStruct->TIM1_DeadTime = TIM1_DeadTime_Reset_Mask;
  TIM1_BDTRInitStruct->TIM1_Break = TIM1_Break_Disable;
  TIM1_BDTRInitStruct->TIM1_BreakPolarity = TIM1_BreakPolarity_Low;
  TIM1_BDTRInitStruct->TIM1_AutomaticOutput = TIM1_AutomaticOutput_Disable;
}

/*******************************************************************************
* Function Name  : TIM1_Cmd
* Description    : Enables or disables the TIM1 peripheral.
* Input          : - Newstate: new state of the TIM1 peripheral.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_Cmd(FunctionalState NewState)
{
  /* set or Reset the CEN Bit */
  *(vu32 *) CR1_CEN_BB = NewState;
}

/*******************************************************************************
* Function Name  : TIM1_CtrlPWMOutputs
* Description    : Enables or disables TIM1 peripheral Main Outputs.
* Input          : - Newstate: new state of the TIM1 peripheral Main Outputs.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_CtrlPWMOutputs(FunctionalState Newstate)
{
  /* Set or Reset the MOE Bit */
  *(vu32 *) BDTR_MOE_BB = Newstate;
}

/*******************************************************************************
* Function Name  : TIM1_ITConfig
* Description    : Enables or disables the TIM1 interrupts.
* Input          : - 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
*                  - Newstate: new state of the TIM1 interrupts.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_ITConfig(u16 TIM1_IT, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the Interrupt sources */
    TIM1->DIER |= TIM1_IT;
  }
  else
  {
    /* Disable the Interrupt sources */
    TIM1->DIER &= ~TIM1_IT;
  }
}

/*******************************************************************************
* Function Name  : TIM1_DMAConfig
* Description    : Configures the TIM1抯 DMA interface.
* Input          : - TIM1_DMABase: DMA Base address.
*                    This parameter can be one of the following values:
*                       - TIM1_DMABase_CR1, TIM1_DMABase_CR2, TIM1_DMABase_SMCR,
*                         TIM1_DMABase_DIER, TIM1_DMABase_SR, TIM1_DMABase_EGR,
*                         TIM1_DMABase_CCMR1, TIM1_DMABase_CCMR2, TIM1_DMABase_CCER,
*                         TIM1_DMABase_CNT, TIM1_DMABase_PSC, TIM1_DMABase_ARR,
*                         TIM1_DMABase_RCR, TIM1_DMABase_CCR1, TIM1_DMABase_CCR2,
*                         TIM1_DMABase_CCR3, TIM1_DMABase_CCR4, TIM1_DMABase_BDTR,
*                         TIM1_DMABase_DCR.
*                   - TIM1_DMABurstLength: DMA Burst length.
*                     This parameter can be one value between:
*                     TIM1_DMABurstLength_1Byte and TIM1_DMABurstLength_18Bytes.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_DMAConfig(u16 TIM1_DMABase, u16 TIM1_DMABurstLength)
{
  u32 tmpdcr = 0;

  tmpdcr = TIM1->DCR;

  /* Reset the DBA and the DBL Bits */
  tmpdcr &= DCR_DMA_Mask;

  /* Set the DMA Base and the DMA Burst Length */
  tmpdcr |= TIM1_DMABase | TIM1_DMABurstLength;

  TIM1->DCR = tmpdcr;
}

/*******************************************************************************
* Function Name  : TIM1_DMACmd
* Description    : Enables or disables the TIM1抯 DMA interface.
* Input          : - TIM1_DMASources: specifies the DMA Request sources.
*                    This parameter can be any combination of the following values:
*                       - TIM1_DMA_Update: TIM1 update Interrupt source
*                       - TIM1_DMA_CC1: TIM1 Capture Compare 1 DMA source
*                       - TIM1_DMA_CC2: TIM1 Capture Compare 2 DMA source
*                       - TIM1_DMA_CC3: TIM1 Capture Compare 3 DMA source
*                       - TIM1_DMA_CC4: TIM1 Capture Compare 4 DMA source
*                       - TIM1_DMA_CCUpdate: TIM1 Capture Compare Update DMA
*                         source
*                       - TIM1_DMA_Trigger: TIM1 Trigger DMA source
*                  - Newstate: new state of the DMA Request sources.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_DMACmd(u16 TIM1_DMASource, FunctionalState Newstate)
{
  u32 tmpdier = 0;

  tmpdier = TIM1->DIER;

  if (Newstate == ENABLE)
  {
    /* Enable the DMA sources */
    tmpdier |= TIM1_DMASource;
  }
  else
  {
    /* Disable the DMA sources */
    tmpdier &= ~TIM1_DMASource;
  }
  TIM1->DIER = tmpdier;
}

/*******************************************************************************
* Function Name  : TIM1_InternalClockConfig
* Description    : Configures the TIM1 interrnal Clock
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void TIM1_InternalClockConfig(void)
{
  /* Disable slave mode to clock the prescaler directly with the internal clock */
  TIM1->SMCR &=  SMCR_SMS_Mask;
}
/*******************************************************************************
* Function Name  : TIM1_ETRClockMode1Config
* Description    : Configures the 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)
{
  /* Configure the ETR Clock source */
  ETR_Config(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 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,

⌨️ 快捷键说明

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