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

📄 91x_tim.c

📁 a set or ARM9 examples by STM
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_tim.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the TIM software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* 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 "91x_tim.h"

/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* TIM Bits Masks */

#define TIM_PWM_MASK          0x0010
#define TIM_OPM_MASK          0x0020
#define TIM_OC1_ENABLE_MASK   0x0040
#define TIM_OC1_DISABLE_MASK  0xFFBF
#define TIM_OC2_ENABLE_MASK   0x0080
#define TIM_OC2_DISABLE_MASK  0xFF7F

#define TIM_OLVL1_SET_MASK    0x0100
#define TIM_OLVL1_RESET_MASK  0xFEFF

#define TIM_OLVL2_SET_MASK    0x0200
#define TIM_OLVL2_RESET_MASK  0xFDFF

#define TIM_ENABLE_MASK       0x8000
#define TIM_DISABLE_MASK      0x7FFF

#define TIM_DMA_CLEAR_MASK    0xCFFF

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : TIM_DeInit
* Description    : Initializes TIM peripheral control and registers to their
*                : default reset values.
* Input          : TIMx: where x can be from 0 to 3 to select the TIM
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM_DeInit(TIM_TypeDef *TIMx)
{ 
  if((TIMx == TIM0)||(TIMx == TIM1))
  {
    SCU_APBPeriphReset(__TIM01, DISABLE);    /* TIM0 & TIM1 Reset's off */
  }
  else
  {
    SCU_APBPeriphReset(__TIM23, DISABLE);    /* TIM2 & TIM3 Reset's off */
  }
  
  /* Set all the TIMx registers to thier default values */ 
  TIMx->OC1R = 0x8000;
  TIMx->OC2R = 0x8000;
  TIMx->CR1  = 0x0;
  TIMx->CR2  = 0x1;
  TIMx->CNTR = 0x1234;
  TIMx->SR   = 0x0;
}

/*******************************************************************************
* Function Name  : TIM_StructInit
* Description    : Fills in a TIM_InitTypeDef structure with the reset value of
*                  each parameter.
* Input          : TIM_InitStruct : pointer to a TIM_InitTypeDef structure
                   which will be initialized.
* Output         : None
* Return         : None.
*******************************************************************************/
void TIM_StructInit(TIM_InitTypeDef *TIM_InitStruct)
{
  TIM_InitStruct->TIM_Mode           = 0x0000;
  TIM_InitStruct->TIM_OC1_Modes      = 0x0000;
  TIM_InitStruct->TIM_OC2_Modes      = 0x0000;
  TIM_InitStruct->TIM_Clock_Source   = 0x0000;
  TIM_InitStruct->TIM_Clock_Edge     = 0x0000;
  TIM_InitStruct->TIM_OPM_INPUT_Edge = 0x0000;
  TIM_InitStruct->TIM_ICAP1_Edge     = 0x0000;
  TIM_InitStruct->TIM_ICAP2_Edge     = 0x0000;
  TIM_InitStruct->TIM_Prescaler      = 0x0000;
  TIM_InitStruct->TIM_Pulse_Level_1  = 0x0000;
  TIM_InitStruct->TIM_Pulse_Level_2  = 0x0000;
  TIM_InitStruct->TIM_Period_Level   = 0x0000;
  TIM_InitStruct->TIM_Pulse_Length_1 = 0x0000;
  TIM_InitStruct->TIM_Pulse_Length_2 = 0x0000;
  TIM_InitStruct->TIM_Full_Period    = 0x0000;
}

/*******************************************************************************
* Function Name  : TIM_Init
* Description    : Initializes TIM  peripheral according to the specified
*                  parameters in the TIM_InitTypeDef structure.
* Input1         : TIMx: where x can be from 0 to 3 to select the TIM
*                  peripheral.
* Input2         : TIM_InitStruct: pointer to a TIM_InitTypeDef structure that
*                  contains the configuration information for the specified
*                  TIM peripheral.
* Output         : None
* Return         : None
*******************************************************************************/

void TIM_Init(TIM_TypeDef *TIMx, TIM_InitTypeDef *TIM_InitStruct)
{
/***************************** Clock configuration ****************************/

  if (TIM_InitStruct->TIM_Clock_Source == TIM_CLK_APB)
  {
    /* APB clock */
    TIMx->CR1 &= TIM_CLK_APB;
  }
  else
  {
    /* External/SCU clock */
    TIMx->CR1 |= TIM_CLK_EXTERNAL;
    if (TIM_InitStruct->TIM_Clock_Edge == TIM_CLK_EDGE_RISING)
    {
      /* Clock rising edge */
      TIMx->CR1 |= TIM_CLK_EDGE_RISING;
    }
    else
    {
      /* Clock falling edge */
      TIMx->CR1 &= TIM_CLK_EDGE_FALLING;
    }
  }

/************************** Prescaler configuration ***************************/

  TIMx->CR2 =( TIMx->CR2 & 0xFF00 )|TIM_InitStruct->TIM_Prescaler ;
  
/********************************** TIM Modes *********************************/

  switch ( TIM_InitStruct->TIM_Mode)
  {
/******************************* PWM Input mode *******************************/

    case TIM_PWMI:

      /* Set the PWMI Bit */
      TIMx->CR1 |= TIM_PWMI;

      /* Set the first edge Level */
      if ( TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING)
      {
        TIMx->CR1 |= TIM_ICAP1_EDGE_RISING;
      }
      else
      {
        TIMx->CR1 &= TIM_ICAP1_EDGE_FALLING;
      }

      /* Set the Second edge Level ( Opposite of the first level ) */
      if ( TIM_InitStruct->TIM_ICAP1_Edge == TIM_ICAP1_EDGE_RISING)
      {
        TIMx->CR1 &= TIM_ICAP2_EDGE_FALLING;
      }
      else
      {
      	TIMx->CR1 |= TIM_ICAP2_EDGE_RISING;
      }

      break;

/************************** Output compare channel 1 **************************/

    case TIM_OCM_CHANNEL_1:

      if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH)
      {
        TIMx->CR1 |= TIM_OLVL1_SET_MASK;
      }
      else
      {
        TIMx->CR1 &= TIM_OLVL1_RESET_MASK;
      }
      
      TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1;

      if (TIM_InitStruct->TIM_OC1_Modes == TIM_TIMING)
      {
      	TIMx->CR1 &= TIM_OC1_DISABLE_MASK;
      }
      else
      {
        TIMx->CR1 |= TIM_OC1_ENABLE_MASK;
      }

      break;

/************************** Output compare channel 2 **************************/

    case TIM_OCM_CHANNEL_2:

      if (TIM_InitStruct->TIM_Pulse_Level_2 == TIM_HIGH)
      {
      	TIMx->CR1 |= TIM_OLVL2_SET_MASK;
      }
      else
      {
        TIMx->CR1 &= TIM_OLVL2_RESET_MASK;
      }
       
      TIMx->OC2R = TIM_InitStruct->TIM_Pulse_Length_2;

      if (TIM_InitStruct->TIM_OC2_Modes == TIM_TIMING)
      {
        TIMx->CR1 &= TIM_OC2_DISABLE_MASK;
      }
      else
      {
        TIMx->CR1 |= TIM_OC2_ENABLE_MASK;
      }

      break;

/************************ Output compare channel 1 & 2 ************************/

   case TIM_OCM_CHANNEL_12:

    TIMx->OC2R = TIM_InitStruct->TIM_Pulse_Length_2;
    TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1;

    if (TIM_InitStruct->TIM_OC2_Modes == TIM_TIMING)
    {
      TIMx->CR1 &= TIM_OC2_DISABLE_MASK;
    }
    else
    {
      TIMx->CR1 |= TIM_OC2_ENABLE_MASK;
    }

    if (TIM_InitStruct->TIM_OC1_Modes == TIM_TIMING)
    {
      TIMx->CR1 &= TIM_OC1_DISABLE_MASK;
    }
    else
    {
      TIMx->CR1 |= TIM_OC1_ENABLE_MASK;
    }
    
    if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH)
    {
      TIMx->CR1 |= TIM_OLVL1_SET_MASK;
    }
    else
    {
      TIMx->CR1 &= TIM_OLVL1_RESET_MASK;
    }

    if (TIM_InitStruct->TIM_Pulse_Level_2 == TIM_HIGH)
    {
      TIMx->CR1 |= TIM_OLVL2_SET_MASK;
    }
    else
    {
      TIMx->CR1 &= TIM_OLVL2_RESET_MASK;
    }

    break;

/********************************** PWM mode **********************************/

    case TIM_PWM:

      /* Set the Level During the pulse */
      if ( TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH)
      {
        TIMx->CR1 |= TIM_OLVL2_SET_MASK;
      }
      else
      {
        TIMx->CR1 &= TIM_OLVL2_RESET_MASK;
      }

      /* Set the Level after the pulse */
      if (TIM_InitStruct->TIM_Period_Level == TIM_HIGH)
      {
        TIMx->CR1 |= TIM_OLVL1_SET_MASK;
      }
      else
      {
        TIMx->CR1 &= TIM_OLVL1_RESET_MASK;
      }
      
      /* Set the OCAE */
      TIMx->CR1 |= TIM_OC1_ENABLE_MASK;

      /* Set the PWM Bit */
      TIMx->CR1 |= TIM_PWM_MASK;

      /* Set the Duty Cycle value */
      if ( TIM_InitStruct->TIM_Pulse_Length_1 < 5 )
      {
        TIM_InitStruct->TIM_Pulse_Length_1 = 4;
      }
      
      TIMx->OC1R = TIM_InitStruct->TIM_Pulse_Length_1 - 4;

      /* Set the Full Period */
      TIMx->OC2R = TIM_InitStruct->TIM_Full_Period - 4;

      break;

/******************************* One pulse mode *******************************/

    case TIM_OPM:

      /* Set the Level During the pulse */
      if (TIM_InitStruct->TIM_Pulse_Level_1 == TIM_HIGH)
      {
        TIMx->CR1 |= TIM_OLVL2_SET_MASK;
      }

      /* Set the Level after the pulse  */
      if (TIM_InitStruct->TIM_Period_Level == TIM_HIGH)
      {
        TIMx->CR1 |= TIM_OLVL1_SET_MASK;
      }
      
      /* Set the Activation Edge on the ICAP 1 */
      if (TIM_InitStruct->TIM_OPM_INPUT_Edge == TIM_OPM_EDGE_RISING)
      {
        TIMx->CR1 |= TIM_OPM_EDGE_RISING;
      }

      /* Set the Output Compare Function  */
      TIMx->CR1 |= TIM_OC1_ENABLE_MASK;

⌨️ 快捷键说明

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