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

📄 71x_tim.c

📁 STR7系列32位ARM控制器的固件库
💻 C
📖 第 1 页 / 共 2 页
字号:
      /* Output Compare Used for external wave generation */
       if (Xchannel == TIM_CHANNEL_A)
       {
         Tmp1 = TIM_OCAE_Mask;
       }
       else
       {
       	 Tmp1 = TIM_OCBE_Mask;
       } 
            
      if (Xlevel == TIM_HIGH)
      {
      	if (Xchannel == TIM_CHANNEL_A)
      	{
      	  Tmp1 |= TIM_OLVLA_Mask;
        }
        else
        {
          Tmp1 |= TIM_OLVLB_Mask;	
        }
      }
      else
      {
      	if (Xchannel == TIM_CHANNEL_A)
      	{
      	  Tmp1 &= ~TIM_OLVLA_Mask;	
      	}
      	else
      	{
      	  Tmp1 &= ~TIM_OLVLB_Mask;	
      	}
      }
      break;
      
    default:
      break;
  }
  
  if (Xchannel == TIM_CHANNEL_A)
  {
    TIMx->OCAR = (XpulseLength);
  }
  else
  {
    TIMx->OCBR = (XpulseLength);
  }
  TIMx->CNTR = 0x0000;
  TIMx->CR1 |= Tmp1;
}

/*******************************************************************************
* Function Name  : TIM_OPModeConfig
* Description    : This routine is used to configure the one pulse mode.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - XpulseLength: specifies the pulse length.
*                  - XLevel1: specifies the output level on the OCMPA pin during
*                    the pulse it can be:
*                     TIM_HIGH, TIM_LOW
*                  - XLevel2: specifies the output level on the OCMPB pin after
*                    the pulse it can be:
*                     TIM_HIGH, TIM_LOW
*                  - Xedge:  specifies the edge to be detected by the input 
*                    capture A pin it can be:
*                     TIM_RISING, TIM_FALLING
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_OPModeConfig (TIM_TypeDef *TIMx, u16 XpulseLength,
                       TIM_Logic_Levels XLevel1, TIM_Logic_Levels XLevel2,
                       TIM_Clock_Edges Xedge)
{
  u16 Tmp = 0;

  /* Set the Level During the pulse */
  if (XLevel1 == TIM_HIGH)
  {
    Tmp |= TIM_OLVLB_Mask;
  }
  
  /* Set the Level after After the pulse */
  if (XLevel2 == TIM_HIGH)
  {
    Tmp |= TIM_OLVLA_Mask;
  }
  
  /* Set the Activation Edge on the INCAP 1 */  /* to be verified*/
  if (Xedge == TIM_RISING)
  {
    Tmp |= TIM_IEDGA_Mask;
  }
  
  /* Set the Output Compare Function */
  Tmp |= TIM_OCAE_Mask;
  
  /* Set the One pulse mode */
  Tmp |= TIM_OPM_Mask;
  
  /* Update the CR1 register Value */
  TIMx->CR1 = Tmp;
  
  /* Set the Pulse length */
  TIMx->OCAR = XpulseLength;
}

/*******************************************************************************
* Function Name  : TIM_PWMOModeConfig
* Description    : This routine is used to configure the PWM output mode.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - XDutyCycle: specifies the PWM signal duty cycle.
*                  - XLevel1: specifies the PWM signal level during the duty
*                    cycle, it can be:
*                     TIM_HIGH, TIM_LOW
*                  - XFullperiod: specifies the PWM signal full period.
*                  - XLevel2: specifies the PWM signal level out of the duty
*                    cycle, it can be:
*                     TIM_HIGH, TIM_LOW
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_PWMOModeConfig (TIM_TypeDef *TIMx, u16 XDutyCycle,
                         TIM_Logic_Levels XLevel1, u16 XFullperiod,
                         TIM_Logic_Levels XLevel2)
{
  vu16 Tmp = TIMx->CR1;
  
  /* Set the level during the pulse */
  if (XLevel1 == TIM_HIGH)
  {
    Tmp |= TIM_OLVLB_Mask;	
  }
  else
  {
    Tmp &= ~TIM_OLVLB_Mask;	
  }
  
  /* Set the level after the pulse */
  if (XLevel2 == TIM_HIGH)
  {
    Tmp |= TIM_OLVLA_Mask;
  }
  else
  {
    Tmp &= ~TIM_OLVLA_Mask;	
  }

  /* Set the OCAE */
  Tmp |= TIM_OCAE_Mask;
  
  /* Set the PWM Bit */
  Tmp |= TIM_PWM_Mask;
  
  /* Update the CR1 */
  TIMx->CR1 = Tmp;
  
  /* Set the Duty Cycle value */
  if (XDutyCycle < 5)
  {
    XDutyCycle = 4;
  }
  
  TIMx->OCAR = XDutyCycle - 4;
  
  /* Set the Full Period */
  TIMx->OCBR = XFullperiod - 4;
}

/*******************************************************************************
* Function Name  : TIM_PWMIModeConfig
* Description    : This routine is used to configure the PWM input mode.
* Input          : - TIMx: specifies the TIM to be configured.
*                : - Xedge: specifies the first edge of the external PWM signal.
*                    It can be:
*                     TIM_RISING, TIM_FALLING
* Output         : None.
* Output         : None.
*******************************************************************************/
void TIM_PWMIModeConfig (TIM_TypeDef  *TIMx,  TIM_Clock_Edges  Xedge)
{
  vu16 Tmp = TIMx->CR1;
  
  if (Xedge == TIM_RISING)
  {
    Tmp |= TIM_IEDGA_Mask;	
  }
  else
  {
    Tmp &= ~TIM_IEDGA_Mask;
  }
  
  /* Set the first edge Level */
  /* Set the Second edge Level (Opposit of the first level)*/
  if (Xedge == TIM_FALLING)
  {
    Tmp |= TIM_IEDGB_Mask;
  }
  else
  {
    Tmp &= ~TIM_IEDGB_Mask;	
  }

  /* Set the PWM I Bit */
  Tmp |= TIM_PWMI_Mask;

  /* Update the CR1 */
  TIMx->CR1 = Tmp;
}

/*******************************************************************************
* Function Name  : TIM_PWMIValue
* Description    : This routine is used to get the PWMI values.
* Input          : - TIMx: specifies the TIM to get its PWM parameters values.
* Output         : None.
* Return         : The PWM input parameters: pulse and period.
*******************************************************************************/
PWMI_parameters TIM_PWMIValue (TIM_TypeDef  *TIMx)
{
  PWMI_parameters Tmp;
  
  Tmp.Pulse  = TIMx->ICBR;
  Tmp.Period = TIMx->ICAR;
  return Tmp;
}

/*******************************************************************************
* Function Name  : TIM_CounterConfig
* Description    : This routine is used to start/stop and clear the selected 
*                  timer counter.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xoperation: specifies the operation of the counter.
*                    It can be:
*                     TIM_START, TIM_STOP, TIM_CLEAR
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_CounterConfig (TIM_TypeDef  *TIMx, TIM_CounterOperations Xoperation)
{
  switch (Xoperation)
  {
    case TIM_START :
      TIMx->CR1 |= TIM_EN_Mask;
      break;
      
    case TIM_STOP :
      TIMx->CR1 &= ~TIM_EN_Mask;
      break;
      
    case TIM_CLEAR :
      TIMx->CNTR = 0x1234;
      break;
      
    default:
      break;  
  }
}

/*******************************************************************************
* Function Name  : TIM_ITConfig
* Description    : This routine is used to configure the TIM interrupt.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - New_IT: specifies the TIM interrupt to be configured.
*                    You can specify one or more TIM interrupts to be configured
*                    using the logical operator 慜R

⌨️ 快捷键说明

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