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

📄 71x_tim.c

📁 STR7系列32位ARM控制器的固件库
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name          : 71x_tim.c
* Author             : MCD Application Team
* Version            : V4.0
* Date               : 10/09/2007
* Description        : This file provides all the TIM firmware functions.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "71x_tim.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : TIM_Init
* Description    : This routine is used to Initialize the TIM peripheral
*                  registers to their default values.
* Input          : - TIMx: the Timer to be Initialized.
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_Init(TIM_TypeDef *TIMx)
{
  TIMx->CR1 = 0x0000;
  TIMx->CR2 = 0x0000;
  TIMx->SR  = 0x0000;
}

/*******************************************************************************
* Function Name  : TIM_ClockSourceConfig
* Description    : This routine is used to configure the TIM clock source
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xclock: specifies the TIM source clock. It can be:
*                     TIM_INTERNAL : the TIM is clocked by the APB2 frequency
*                                    divided by the prescaler value.
*                     TIM_EXTERNAL : the TIM is clocked by an external Clock.
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_ClockSourceConfig (TIM_TypeDef *TIMx, TIM_Clocks Xclock)
{
  if (Xclock == TIM_EXTERNAL)
  {
    TIMx->CR1 |= TIM_ECKEN_Mask;
  }
  else
  {
    TIMx->CR1 &= ~TIM_ECKEN_Mask;
  }
}

/*******************************************************************************
* Function Name  : TIM_ClockSourceValue
* Description    : This routine is used to get the TIM clock source
* Input          : - TIMx: specifies the TIM to check its source clock.
* Output         : None.
* Return         : The TIM source clock. It can be:
*                   TIM_INTERNAL: The TIM is clocked by the APB2 frequency
*                                 divided by the prescaler value.
*                   TIM_EXTERNAL: The TIM is clocked by an external Clock.
*******************************************************************************/
TIM_Clocks TIM_ClockSourceValue (TIM_TypeDef *TIMx)
{
  if ((TIMx->CR1 & TIM_ECKEN_Mask) == 0)
  {
    return TIM_INTERNAL;	
  }
  else
  {
    return TIM_EXTERNAL;
  }	
}

/*******************************************************************************
* Function Name  : TIM_PrescalerConfig
* Description    : This routine is used to configure the TIM prescaler value
*                  to divide the internal clock.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xprescaler: specifies the TIM prescaler value (8bit).
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_PrescalerConfig (TIM_TypeDef *TIMx, u8 Xprescaler)
{
  TIMx->CR2 = (TIMx->CR2 & 0xFF00) | Xprescaler;
}

/*******************************************************************************
* Function Name  : TIM_PrescalerValue
* Description    : This routine is used to get the TIM prescaler value 
*                  when the internal clock is used.
* Input          : - TIMx: specifies the timer to get its prescaler value.
* Output         : None.
* Return         : The Current TIM prescaler Value (8bit).
*******************************************************************************/
u8 TIM_PrescalerValue (TIM_TypeDef *TIMx)
{
  return TIMx->CR2 & 0x00FF;
}

/*******************************************************************************
* Function Name  : TIM_ClockLevelConfig
* Description    : This routine is used to configure the TIM clock level
*                  when an external clock source is used.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xedge: specifies the active edge of the external clock.
*                    It can be:
*                     TIM_RISING : The rising  edge.
*                     TIM_FALLING: The falling edge.
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_ClockLevelConfig (TIM_TypeDef *TIMx, TIM_Clock_Edges Xedge)
{
  if (Xedge == TIM_RISING)
  {
    TIMx->CR1 |= TIM_EXEDG_Mask;
  }
  else
  {
    TIMx->CR1 &= ~TIM_EXEDG_Mask;
  }
}

/*******************************************************************************
* Function Name  : TIM_ClockLevelValue
* Description    : This routine is used to get and return the clock active level
*                  when using an external clock source
* Input          : - TIMx: specifies the TIM to be configured.
* Output         : None.
* Return         : The external clock level of the specified timer.
*                  It can be:            
*                    TIM_RISING  : The rising  edge.
*                    TIM_FALLING : The falling edge.
*******************************************************************************/
TIM_Clock_Edges TIM_ClockLevelValue (TIM_TypeDef *TIMx)
{
  if ((TIMx->CR1 & TIM_EXEDG_Mask) == 0)
  {
    return TIM_FALLING;	
  }	
  else
  {
    return TIM_RISING;	
  }
}

/*******************************************************************************
* Function Name  : TIM_ICAPModeConfig
* Description    : This routine is used to configure the input capture feature
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xchannel: specifies the input Capture Channel, it can be:
*                     TIM_CHANNEL_A, TIM_CHANNEL_B
*                  - Xedge: specifies the Active Edge, it can be:
*                     TIM_RISING, TIM_FALLING
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_ICAPModeConfig (TIM_TypeDef  *TIMx, TIM_Channels Xchannel,
                         TIM_Clock_Edges  Xedge)
{
  switch (Xchannel)
  {
    case TIM_CHANNEL_A :
      if (Xedge == TIM_RISING)
      {
        TIMx->CR1 |= TIM_IEDGA_Mask;
      }
      else
      {
        TIMx->CR1 &= ~TIM_IEDGA_Mask;
      }
      break;
    case TIM_CHANNEL_B :
      if (Xedge == TIM_RISING)
      {
        TIMx->CR1 |= TIM_IEDGB_Mask;
      }
      else
      {
        TIMx->CR1 &= ~TIM_IEDGB_Mask;
      }
      break;
  }
}

/*******************************************************************************
* Function Name  : TIM_ICAPValue
* Description    : This routine is used to get and return the Input Capture 
*                  value.
* Input          : - TIMx: specifies the TIM to check its Input Capture value.
*                  - Xchannel: specifies the Input Capture channel, it can be:
*                     TIM_Channel_A, TIM_Channel_B 
* Output         : None.
* Return         : The input capture value of the specified timer and channel.
*******************************************************************************/
u16 TIM_ICAPValue (TIM_TypeDef *TIMx, TIM_Channels Xchannel)
{
  if (Xchannel == TIM_CHANNEL_A)
  {
    return TIMx->ICAR;	
  } 	
  else
  {
    return TIMx->ICBR;	
  }
}

/*******************************************************************************
* Function Name  : TIM_OCMPModeConfig
* Description    : This routine is used to configure the output compare mode.
* Input          : - TIMx: specifies the TIM to be configured.
*                  - Xchannel: specifies the output compare channel, it can be:
*                     TIM_CHANNEL_A, TIM_CHANNEL_B
*                  - XpulseLength: specifies the pulse length.
*                  - Xmode: specifies the output compare mode, it can be:
*                      TIM_TIMING, TIM_WAVE 
*                  - Xlevel: specifies the level of the external signal after
*                    the match occurs, it can be:
*                      TIM_HIGH, TIM_LOW 
* Output         : None.
* Return         : None.
*******************************************************************************/
void TIM_OCMPModeConfig (TIM_TypeDef  *TIMx, TIM_Channels Xchannel,
                         u16 XpulseLength, TIM_OC_Modes Xmode,
                         TIM_Logic_Levels Xlevel)
{
  u16 Tmp1 = 0x0000;
  u16 Tmp2 = TIMx->CR2;

  TIMx->CR2 = 0x0000;
  /* Start The TIM Counter */
  TIMx->CR1  = TIM_EN_Mask;
  /* Update the CR2 Register */
  TIMx->CR2  = Tmp2;
  switch (Xmode)
  {
    case TIM_TIMING:
      /* Output Compare Used only for Internal Timing Operation */
       if (Xchannel == TIM_CHANNEL_A)
       {
         Tmp1 &= ~TIM_OCAE_Mask;
       }
       else
       {
       	 Tmp1 &= ~TIM_OCBE_Mask;
       }
      break;
      
    case TIM_WAVE:

⌨️ 快捷键说明

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