📄 75x_tim.c
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : 75x_tim.c
* Author : MCD Application Team
* Date First Issued : 03/10/2006 : V0.1
* Description : This file provides all the TIM software functions.
********************************************************************************
* History:
* 03/10/2006 : V0.1
********************************************************************************
* 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 "75x_tim.h"
#include "75x_mrcc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* TIM interrupt masks */
#define TIM_IT_Clear_Mask 0x7FFF
#define TIM_IT_Enable_Mask 0x7FFF
/* TIM Input Capture Selection Set/Reset */
#define TIM_IC1S_Set 0x0001
#define TIM_IC1S_Reset 0x003E
/* TIM Input Capture Selection Set/Reset */
#define TIM_IC2S_Set 0x0002
#define TIM_IC2S_Reset 0x003D
/* TIM_SCR Masks bit */
#define TIM_Encoder_Mask 0x731C
#define TIM_SlaveModeSelection_Mask 0x7307
#define TIM_TriggerSelection_Mask 0x701F
#define TIM_InternalTriggerSelection_Mask 0x031F
/* TIM Encoder mode Set value */
#define TIM_Encoder1_Set 0x0001
#define TIM_Encoder2_Set 0x0002
#define TIM_Encoder3_Set 0x0003
/* TIM Slave Mode Enable Set/Reset value */
#define TIM_SME_Reset 0x731B
#define TIM_SME_Set 0x0004
/* TIM Internal Trigger Selection value */
#define TIM_ITS_TIM0 0x1000
#define TIM_ITS_TIM1 0x2000
#define TIM_ITS_TIM2 0x3000
#define TIM_ITS_PWM 0x4000
/* TIM Trigger Selection value */
#define TIM_TS_IC1_Set 0x0200
#define TIM_TS_IC2_Set 0x0300
/* TIM Slave Mode selction external clock Set value */
#define TIM_SMS_EXTCLK_Set 0x0008
#define TIM_SMS_RESETCLK_Set 0x0000
/* TIM_CR Masks bit */
#define TIM_DBASE_Mask 0x077F
#define TIM_MasterModeSelection_Mask 0xFC7F
#define TIM_CounterMode_Mask 0xFF8F
/* TIM Update flag selection Set/Reset value */
#define TIM_UFS_Reset 0xFFFE
#define TIM_UFS_Set 0x0001
/* TIM Counter value */
#define TIM_COUNTER_Reset 0x0002
#define TIM_COUNTER_Start 0x0004
#define TIM_COUNTER_Stop 0xFFFB
/* TIM One pulse Mode set value */
#define TIM_OPM_Set 0x0008
#define TIM_OPM_Reset 0xFFF7
/* TIM Debug Mode Set/Reset value */
#define TIM_DBGC_Set 0x0400
#define TIM_DBGC_Reset 0xFB7F
/* TIM Input Capture Enable/Disable value */
#define TIM_IC1_Enable 0x0004
#define TIM_IC2_Enable 0x0010
/* TIM Input Capture Polarity Set/Reset value */
#define TIM_IC1P_Set 0x0008
#define TIM_IC2P_Set 0x0020
#define TIM_IC1P_Reset 0x0037
#define TIM_IC2P_Reset 0x001F
/* TIM Output Compare Polarity Set/Reset value */
#define TIM_OC1P_Set 0x0020
#define TIM_OC2P_Set 0x2000
#define TIM_OC1P_Reset 0x3F1F
#define TIM_OC2P_Reset 0x1F3F
/* TIM Output Compare control mode constant */
#define TIM_OCControl_PWM 0x000C
#define TIM_OCControl_OCToggle 0x0006
#define TIM_OCControl_OCInactive 0x0004
#define TIM_OCControl_OCActive 0x0002
#define TIM_OCControl_OCTiming 0x0000
/* TIM Output Compare mode Enable value */
#define TIM_OC1_Enable 0x0010
#define TIM_OC2_Enable 0x1000
/* TIM Output Compare mode Mask value */
#define TIM_OC1C_Mask 0x3F31
#define TIM_OC2C_Mask 0x313F
/* TIM Preload bit Set/Reset value */
#define TIM_PLD1_Set 0x0001
#define TIM_PLD1_Reset 0xFFFE
#define TIM_PLD2_Set 0x0100
#define TIM_PLD2_Reset 0xFEFF
/* TIM OCRM Set/Reset value */
#define TIM_OCRM_Set 0x0080
#define TIM_OCRM_Reset 0x030D
/* Reset Register Masks */
#define TIM_Pulse2_Reset_Mask 0x0000
#define TIM_Prescaler_Reset_Mask 0x0000
#define TIM_Pulse1_Reset_Mask 0x0000
#define TIM_Period_Reset_Mask 0xFFFF
#define TIM_Counter_Reset 0x0002
/* Private function prototypes -----------------------------------------------*/
static void ICAP_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
static void Encoder_ModeConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
static void OCM_ModuleConfig(TIM_TypeDef* TIMx, TIM_InitTypeDef* TIM_InitStruct);
/* Private functions ---------------------------------------------------------*/
/******************************************************************************
* Function Name : TIM_DeInit
* Description : Deinitializes TIM peripheral registers to their default reset
* values.
* Input : TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
* Output : None
* Return : None
*******************************************************************************/
void TIM_DeInit(TIM_TypeDef *TIMx)
{
if(TIMx == TIM0)
{
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,ENABLE);
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0,DISABLE);
}
else if(TIMx == TIM1)
{
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,ENABLE);
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,DISABLE);
}
else if(TIMx == TIM2)
{
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,ENABLE);
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM2,DISABLE);
}
}
/*******************************************************************************
* Function Name : TIM_Init
* Description : Initializes the TIMx peripheral according to the specified
* parameters in the TIM_InitStruct .
* Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
* - 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)
{
/* Set the prescaler value */
TIMx->PSC = TIM_InitStruct->TIM_Prescaler;
/* Select the clock source */
TIM_ClockSourceConfig(TIMx, TIM_InitStruct->TIM_ClockSource,
TIM_InitStruct->TIM_ExtCLKEdge);
/* Select the counter mode */
TIMx->CR &= TIM_CounterMode_Mask;
TIMx->CR |= TIM_InitStruct->TIM_CounterMode;
/* Set the period value */
TIMx->ARR = TIM_InitStruct->TIM_Period;
switch(TIM_InitStruct->TIM_Mode)
{
case TIM_Mode_OCTiming: case TIM_Mode_OCActive: case TIM_Mode_OCInactive:
case TIM_Mode_OCToggle: case TIM_Mode_PWM:
OCM_ModuleConfig(TIMx, TIM_InitStruct);
break;
case TIM_Mode_PWMI: case TIM_Mode_IC:
ICAP_ModuleConfig(TIMx, TIM_InitStruct);
break;
case TIM_Mode_Encoder1: case TIM_Mode_Encoder2: case TIM_Mode_Encoder3:
Encoder_ModeConfig(TIMx, TIM_InitStruct);
break;
case TIM_Mode_OPM_PWM: case TIM_Mode_OPM_Toggle: case TIM_Mode_OPM_Active:
/* Output module configuration */
OCM_ModuleConfig(TIMx, TIM_InitStruct);
/* Input module configuration */
ICAP_ModuleConfig(TIMx, TIM_InitStruct);
/* Set the slave mode to trigger Mode */
TIMx->SCR |= TIM_SynchroMode_Trigger;
/* Repetitive pulse state selection */
if(TIM_InitStruct->TIM_RepetitivePulse == TIM_RepetitivePulse_Disable)
{
TIMx->CR |= TIM_OPM_Set;
}
else
{
TIMx->CR &= TIM_OPM_Reset;
}
break;
default:
break;
}
}
/*******************************************************************************
* Function Name : TIM_StructInit
* Description : Fills each TIM_InitStruct member with its default value.
* Input : TIM_InitStruct : pointer to a TIM_InitTypeDef structure
* which will be initialized.
* Output : None
* Return : None.
*******************************************************************************/
void TIM_StructInit(TIM_InitTypeDef *TIM_InitStruct)
{
/* Set the default configuration */
TIM_InitStruct->TIM_Mode = TIM_Mode_OCTiming;
TIM_InitStruct->TIM_Prescaler = TIM_Prescaler_Reset_Mask;
TIM_InitStruct->TIM_ClockSource = TIM_ClockSource_Internal;
TIM_InitStruct->TIM_ExtCLKEdge = TIM_ExtCLKEdge_Rising;
TIM_InitStruct->TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct->TIM_Period = TIM_Period_Reset_Mask;
TIM_InitStruct->TIM_Channel = TIM_Channel_ALL;
TIM_InitStruct->TIM_Pulse1 = TIM_Pulse1_Reset_Mask;
TIM_InitStruct->TIM_Pulse2 = TIM_Pulse2_Reset_Mask;
TIM_InitStruct->TIM_RepetitivePulse = TIM_RepetitivePulse_Disable;
TIM_InitStruct->TIM_Polarity1 = TIM_Polarity1_Low;
TIM_InitStruct->TIM_Polarity2 = TIM_Polarity2_Low;
TIM_InitStruct->TIM_IC1Selection = TIM_IC1Selection_TI1;
TIM_InitStruct->TIM_IC2Selection = TIM_IC2Selection_TI1;
TIM_InitStruct->TIM_IC1Polarity = TIM_IC1Polarity_Rising;
TIM_InitStruct->TIM_IC2Polarity = TIM_IC2Polarity_Rising;
TIM_InitStruct->TIM_PWMI_ICSelection = TIM_PWMI_ICSelection_TI1;
TIM_InitStruct->TIM_PWMI_ICPolarity = TIM_PWMI_ICPolarity_Rising;
}
/*******************************************************************************
* Function Name : TIM_Cmd
* Description : Enables or disables the specified TIM peripheral.
* Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
* - Newstate: new state of the TIMx peripheral.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState Newstate)
{
if(Newstate == ENABLE)
{
TIMx->CR |= TIM_COUNTER_Start;
}
else
{
TIMx->CR &= TIM_COUNTER_Stop;
}
}
/*******************************************************************************
* Function Name : TIM_ITConfig
* Description : Enables or disables the TIM interrupts.
* Input : - TIMx: where x can be 0, 1 or 2 to select the TIM peripheral.
* - TIM_IT: specifies the TIM interrupts sources to be enabled
* or disabled.
* This parameter can be any combination of the following values:
* - TIM_IT_IC1: Input Capture 1 Interrupt
* - TIM_IT_OC1: Output Compare 1 Interrupt
* - TIM_IT_Update: Timer update Interrupt
* - TIM_IT_GlobalUpdate: Timer global update Interrupt
* - TIM_IT_IC2: Input Capture 2 Interrupt
* - TIM_IT_OC2: Output Compare 2 Interrupt
* - Newstate: new state of the specified TIMx interrupts.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void TIM_ITConfig(TIM_TypeDef *TIMx, u16 TIM_IT, FunctionalState Newstate)
{
u16 TIM_IT_Enable = 0;
TIM_IT_Enable = TIM_IT & TIM_IT_Enable_Mask;
if(Newstate == ENABLE)
{
/* Update interrupt global source: overflow/undeflow, counter reset operation
or slave mode controller in reset mode */
if((TIM_IT & TIM_IT_GlobalUpdate) == TIM_IT_GlobalUpdate)
{
TIMx->CR &= TIM_UFS_Reset;
}
/* Update interrupt source: counter overflow/underflow */
else if((TIM_IT & TIM_IT_Update) == TIM_IT_Update)
{
TIMx->CR |= TIM_UFS_Set;
}
/* Select and enable the interrupts requests */
TIMx->RSR |= TIM_IT_Enable;
TIMx->RER |= TIM_IT_Enable;
}
/* Disable the interrupts requests */
else
{
TIMx->RSR &= ~TIM_IT_Enable;
TIMx->RER &= ~TIM_IT_Enable;
}
}
/*******************************************************************************
* Function Name : TIM_PreloadConfig
* Description : Enables or disables TIM peripheral Preload register on OCRx.
* 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
* - Newstate: new state of the TIMx peripheral Preload register
* This parameter can be: ENABLE or DISABLE.
* Output : None
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -