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

📄 75x_tim.c

📁 嵌入式实验源码。包括:电源管理、复位、时钟管理
💻 C
📖 第 1 页 / 共 4 页
字号:
* Return         : None
*******************************************************************************/
void TIM_PreloadConfig(TIM_TypeDef *TIMx, u16 TIM_Channel, FunctionalState Newstate)
{
  if(Newstate == ENABLE)
  {
    switch (TIM_Channel)
    {
      case TIM_Channel_1:
      TIMx->OMR1 |= TIM_PLD1_Set;
      break;
   
      case TIM_Channel_2:
      TIMx->OMR1 |= TIM_PLD2_Set;
      break;

      case TIM_Channel_ALL:
      TIMx->OMR1 |= TIM_PLD1_Set | TIM_PLD2_Set;
      break;

      default:
      break;
   }
  }
  else
  {
    switch (TIM_Channel)
    {
      case TIM_Channel_1:
      TIMx->OMR1 &= TIM_PLD1_Reset;
      break;
   
      case TIM_Channel_2:
      TIMx->OMR1 &= TIM_PLD2_Reset;
      break;

      case TIM_Channel_ALL:
      TIMx->OMR1 &= TIM_PLD1_Reset & TIM_PLD2_Reset;
      break;

      default:
      break;
    }
  }  
}

/*******************************************************************************
* Function Name  : TIM_DMAConfig
* Description    : Configures the TIM0抯 DMA interface.
* Input          : - TIM_DMASources: specifies the DMA Request sources.
*                    This parameter can be any combination of the following values:
*                         - TIM_DMASource_OC1: Output Compare 1 DMA source
*                         - TIM_DMASource_OC2: Output Compare 2 DMA source
*                         - TIM_DMASource_IC1: Input Capture 1 DMA source
*                         - TIM_DMASource_IC2: Input Capture 2 DMA source
*                         - TIM_DMASource_Update: Timer Update DMA source
*                  - TIM_OCRMState: the state of output compare request mode.
*                    This parameter can be one of the following values:
*                         - TIM_OCRMState_Enable 
*                         - TIM_OCRMState_Disable 
*                  - TIM_DMABase:DMA Base address.
*                    This parameter can be one of the following values:
*                    TIM_DMABase_CR, TIM_DMABase_SCR, TIM_DMABase_IMCR,
*                    TIM_DMABase_OMR1, TIM_DMABase_RSR,
*                    TIM_DMABase_RER, TIM_DMABase_ISR, TIM_DMABase_CNT, 
*                    TIM_DMABase_PSC, TIM_DMABase_ARR, TIM_DMABase_OCR1, 
*                    TIM_DMABase_OCR2, TIM_DMABase_ICR1, TIM_DMABase_ICR2
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_DMAConfig(u16 TIM_DMASources, u16 TIM_OCRMState, u16 TIM_DMABase)
{
  /* Select the DMA requests */
  TIM0->RSR &= TIM_DMASources;

  /* Set the OCRM state */
  if(TIM_OCRMState == TIM_OCRMState_Enable)
  {
    TIM0->RSR |= TIM_OCRM_Set;
  }
  else
  {
    TIM0->RSR &= TIM_OCRM_Reset;
  }

  /* Set the DMA Base address */
  TIM0->CR &= TIM_DBASE_Mask;
  TIM0->CR |= TIM_DMABase;
}

/*******************************************************************************
* Function Name  : TIM_DMACmd
* Description    : Enables or disables the TIM0抯 DMA interface.
* Input          : - TIM_DMASources: specifies the DMA Request sources.
*                    This parameter can be any combination of the following values:
*                         - TIM_DMASource_OC1: Output Compare 1 DMA source
*                         - TIM_DMASource_OC2: Output Compare 2 DMA source
*                         - TIM_DMASource_IC1: Input Capture 1 DMA source
*                         - TIM_DMASource_IC2: Input Capture 2 DMA source
*                         - TIM_DMASource_Update: Timer Update DMA source
*                  - Newstate: new state of the DMA Request sources.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_DMACmd(u16 TIM_DMASources, FunctionalState Newstate)
{
  if(Newstate == ENABLE)
  {
    TIM0->RER |= TIM_DMASources;
  }
  else
  {
    TIM0->RER &= ~TIM_DMASources;
  }
}

/*******************************************************************************
* Function Name  : TIM_ClockSourceConfig
* Description    : Configures the TIM clock source.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_ClockSource: specifies the TIM clock source to be 
*                    selected.
*                    This parameter can be one of the following values:
*                         - TIM_ClockSource_Internal: CK_TIM internal clock
*                         - TIM_ClockSource_TI11: External input pin TI1 
*                           connected to IC1 channel.
*                         - TIM_ClockSource_TI12: External input pin TI1
*                           connected to IC2 channel.
*                         - TIM_ClockSource_TI22: External input pin TI2
*                           connected to IC2 channel.
*                         - TIM_ClockSource_TI21: External input pin TI2
*                           connected to IC1 channel.
*                  - TIM_ExtCLKEdge: specifies the External input signal edge.
*                    This parameter can be one of the following values:
*                         - TIM_ExtCLKEdge_Falling : Falling edge selected.
*                         - TIM_ExtCLKEdge_Rising : Rising edge selected.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_ClockSourceConfig(TIM_TypeDef *TIMx, u16 TIM_ClockSource,
                           u16 TIM_ExtCLKEdge)
{
  if(TIM_ClockSource == TIM_ClockSource_Internal)
  {
    /* CK_TIM is used as clock source */
    TIMx->SCR &= TIM_SME_Reset & TIM_SlaveModeSelection_Mask & TIM_TriggerSelection_Mask;
  }
  else
  /* Input Captures are used as TIM external clock */
  {
    TIMx->SCR &= TIM_SME_Reset & TIM_SlaveModeSelection_Mask & TIM_TriggerSelection_Mask;
    TIMx->SCR |= TIM_SMS_EXTCLK_Set | TIM_SME_Set;

    if((TIM_ClockSource == TIM_ClockSource_TI11) ||
      (TIM_ClockSource == TIM_ClockSource_TI21))
    /* Input Capture 1 is selected */
    {
     /* Input capture  Enable */
      TIMx->IMCR |= TIM_IC1_Enable;
      TIMx->SCR |= TIM_TS_IC1_Set;

      if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)
      /* Set the corresponding polarity */
      {
        TIMx->IMCR |= TIM_IC1P_Set;
      }
      else
      {   
        TIMx->IMCR &= TIM_IC1P_Reset;
      }
      if(TIM_ClockSource == TIM_ClockSource_TI11)
      {
        /* External signal TI1 connected to IC1 channel */
        TIMx->IMCR &= TIM_IC1S_Reset;
      }
      else
      {
        /* External signal TI2 connected to IC1 channel */
        TIMx->IMCR |= TIM_IC1S_Set;
      }
    }
    else
    /* Input Capture 2 is selected */
    {
      /* Input capture  Enable */
      TIMx->IMCR |= TIM_IC2_Enable;
      TIMx->SCR |= TIM_TS_IC2_Set;

      if(TIM_ExtCLKEdge == TIM_ExtCLKEdge_Falling)
      /* Set the corresponding polarity */
      {
        TIMx->IMCR |= TIM_IC2P_Set;
      }
      else
      {
         TIMx->IMCR &= TIM_IC2P_Reset;
      }
      if(TIM_ClockSource == TIM_ClockSource_TI22)
      {
        /* External signal TI2 connected to IC2 channel */
        TIMx->IMCR &= TIM_IC2S_Reset;
      }
      else
      {
        /* External signal TI1 connected to IC2 channel */
        TIMx->IMCR |= TIM_IC2S_Set;
      }
    }
  }
}

/*******************************************************************************
* Function Name  : TIM_SetPrescaler
* Description    : Sets the TIM prescaler value.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - Prescaler: TIM prescaler new value.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_SetPrescaler(TIM_TypeDef* TIMx, u16 Prescaler)
{
  TIMx->PSC = Prescaler;
}

/*******************************************************************************
* Function Name  : TIM_SetPeriod
* Description    : Sets the TIM period value.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - Period: TIM period new value.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_SetPeriod(TIM_TypeDef* TIMx, u16 Period)
{
  TIMx->ARR = Period;
}

/*******************************************************************************
* Function Name  : TIM_SetPulse
* Description    : Sets the TIM pulse value.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - TIM_Channel: specifies the TIM channel to be used.
*                    This parameter can be one of the following values:
*                         - TIM_Channel_1: TIM Channel 1 is used
*                         - TIM_Channel_2: TIM Channel 2 is used
*                         - TIM_Channel_ALL: TIM Channel 1and 2 are used
*                  - Pulse: TIM pulse new value.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_SetPulse(TIM_TypeDef* TIMx, u16 TIM_Channel, u16 Pulse)
{
  /* Set Channel 1 pulse value */
  if(TIM_Channel == TIM_Channel_1)
  {
    TIMx->OCR1 = Pulse;
  }
  /* Set Channel 2 pulse value */
  else if(TIM_Channel == TIM_Channel_2)
  {
   TIMx->OCR2 = Pulse;
  }
  /* Set Channel 1 and Channel 2 pulse values */
  else if(TIM_Channel == TIM_Channel_ALL)
  {
    TIMx->OCR1 = Pulse;
    TIMx->OCR2 = Pulse;
  }
}

/*******************************************************************************
* Function Name  : TIM_GetICAP1
* Description    : Gets the Input Capture 1 value. 
* Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
* Output         : None
* Return         : Input Capture 1 Register value.
*******************************************************************************/
u16 TIM_GetICAP1(TIM_TypeDef *TIMx)
{
  return TIMx->ICR1;
}

/*******************************************************************************
* Function Name  : TIM_GetICAP2
* Description    : Gets the Input Capture 2 value.
* Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
* Output         : None
* Return         : Input Capture 2 Register value
*******************************************************************************/
u16 TIM_GetICAP2(TIM_TypeDef *TIMx)
{
  return TIMx->ICR2;
}

/*******************************************************************************
* Function Name  : TIM_GetPWMIPulse
* Description    : Gets the PWM Input pulse value.
* Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
* Output         : None
* Return         : Input Capture 2 Register value
*******************************************************************************/
u16 TIM_GetPWMIPulse(TIM_TypeDef *TIMx)
{
  return TIMx->ICR2;
}

/*******************************************************************************
* Function Name  : TIM_GetPWMIPeriod
* Description    : Gets the PWM Input period value.
* Input          : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
* Output         : None
* Return         : Input Capture 1 Register value
*******************************************************************************/
u16 TIM_GetPWMIPeriod(TIM_TypeDef *TIMx)
{
  return TIMx->ICR1;
}

/*******************************************************************************
* Function Name  : TIM_DebugCmd
* Description    : Enables or disables the specified TIM peripheral Debug control.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral
*                  - Newstate: new state of the TIMx Debug control.
                     This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_DebugCmd(TIM_TypeDef *TIMx, FunctionalState Newstate)
{
  if(Newstate == ENABLE)
  {
    TIMx->CR |= TIM_DBGC_Set;
  }
  else
  {
    TIMx->CR &= TIM_DBGC_Reset;
  }
}

/*******************************************************************************
* Function Name  : TIM_CounterModeConfig
* Description    : Specifies the Counter Mode to be used.
* Input          : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
*                  - TIM_CounterMode: specifies the Counter Mode to be used
*                    This parameter can be one of the following values:
*                       - TIM_CounterMode_Up: TIM Up Counting Mode
*                       - TIM_CounterMode_Down: TIM Down Counting Mode
*                       - TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
*                       - TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
*                       - TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
* Output         : None
* Return         : None

⌨️ 快捷键说明

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