STM32F10x - TIM - 图文 下载本文

assert_param(IS_TIM_PERIPH_DMA(TIMx, TIM_DMASource)); assert_param(IS_FUNCTIONAL_STATE(NewState));

if (NewState != DISABLE) {

/* Enable the DMA sources */ TIMx->DIER |= TIM_DMASource; } else {

/* Disable the DMA sources */

TIMx->DIER &= (u16)~TIM_DMASource; } }

19.2.12 函数TIM_InternalClockConfig

Table 487. 函数TIM_InternalClockConfig 函数名 TIM_InternalClockConfig void TIM_DMACmd(TIM_TypeDef* TIMx, u16 TIM_DMASource, 函数原形 FunctionalState Newstate) 功能描述 设置TIMx内部时钟 输入参数 TIMx:x可以是1,2,3,4,5,8来选择TIM外设 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例: /* Selects the internal clock for TIM2 */ TIM_InternalClockConfig(TIM2); 函数原型如下:

void TIM_InternalClockConfig(TIM_TypeDef* TIMx) {

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

/* Disable slave mode to clock the prescaler directly with the internal clock */ TIMx->SMCR &= SMCR_SMS_Mask;

// #define SMCR_SMS_Mask ((u16)0xFFF8)//0b000:关闭从模式:预分频器直接由内部时钟驱动 }

19.2.13 函数TIM_ITRxExternalClockConfig

Table 488. 函数TIM_ITRxExternalClockConfig 函数名 TIM_ITRxExternalClockConfig void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, u16 函数原形 TIM_InputTriggerSource) 功能描述 设置TIMx内部触发为外部时钟模式 输入参数1 TIMx:x可以是2,3,4,来选择TIM外设 输入参数2 TIM_InputTriggerSource:输入触发源 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_InputTriggerSource:选择TIM输入触发。见Table 489. 参阅该参数的取值。 Table 489. TIM_InputTriggerSource值 TIM_InputTriggerSource 描述/SMCR.bit6-4 #define值 TIM_TS_ITR0 TIM内部触发0 0x0000 TIM_TS_ITR1 TIM内部触发1 0x0010 内部触发输入 TIM_TS_ITR2 TIM内部触发2 0x0020 TIM_TS_ITR3 TIM内部触发3 0x0030 TIM_TS_TI1F_ED TI1边缘检测 0x0040 TI1边缘检测 TIM_TS_TI1FP1 TI1滤波分频 0x0050 TI1滤波分频 TIM_TS_TI2FP2 TIM_TS_ETRF 例: TI2滤波分频 外部触发输入 0x0060 0x0070 TI2滤波分频 外部触发输入 /* TIM2 internal trigger 3 used as clock source */ TIM_ITRxExternalClockConfig(TIM2, TIM_TS_ITR3); 函数原型如下:

void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource) {

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));

/* Select the Internal Trigger */

TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);

/*

void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource) {

u16 tmpsmcr = 0;

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));

/* Get the TIMx SMCR register value */ tmpsmcr = TIMx->SMCR;

/* Reset the TS Bits */

tmpsmcr &= SMCR_TS_Mask;// #define SMCR_TS_Mask ((u16)0xFF8F)

/* Set the Input Trigger source */ tmpsmcr |= TIM_InputTriggerSource;

/* Write to TIMx SMCR */ TIMx->SMCR = tmpsmcr; } */

/* Select the External clock mode1 */

TIMx->SMCR |= TIM_SlaveMode_External1;// #define TIM_SlaveMode_External1 ((u16)0x0007) 参数TIM_SlaveMode的定义 #define值 SMCR.SMS[2:0]/bit2-0 TIM_SlaveMode_Reset ((u16)0x0004) 复位模式 TIM_SlaveMode_Gated ((u16)0x0005) 门控模式 TIM_SlaveMode_Trigger ((u16)0x0006) 触发模式 TIM_SlaveMode_External1 ((u16)0x0007) 外部时钟模式1 }

19.2.14 函数TIM_TIxExternalClockConfig

Table 490. 函数TIM_TIxExternalClockConfig 函数名 TIM_TIxExternalClockConfig void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, u16 函数原形 TIM_TIxExternalCLKSource, u8 TIM_ICPolarity, u8 ICFilter) 功能描述 设置TIMx触发为外部时钟 输入参数1 TIMx:x可以是1,2,3,4,5,8来选择TIM外设 输入参数2 TIM_ TIxExternalCLKSource:触发源 输入参数3 TIM_ ICPolarity:指定的 TI极性 输入参数4 ICFilter:指定的输入比较滤波器。数取值在 0x0-0xF之间。 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_TIxExternalCLKSource:选择TIMx外部时钟源。见Table 491. 参阅该参数的取值。 Table 491. TIM_TIxExternalCLKSource值 TIM_TIxExternalCLKSource 描述 #define值 TIM_TS_TI1FP1 TIM IC1连接到TI1 0x0050 TIM_TS_TI2FP2 TIM IC2连接到TI2 0x0060 TIM_TS_TI1F_ED TIM IC1连接到TI1:使用边沿探测 0x0040 例: /* Selects the TI1 as clock for TIM2: the external clock is connected to TI1 input pin, the rising edge is the active edge and no filter sampling is done (ICFilter = 0) */

TIM_TIxExternalClockConfig(TIM2, TIM_TS_TI1FP1, TIM_ICPolarity_Rising, 0); 函数原型如下:

void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, u16 TIM_TIxExternalCLKSource, u16 TIM_ICPolarity, u16 ICFilter) {

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_TIXCLK_SOURCE(TIM_TIxExternalCLKSource)); assert_param(IS_TIM_IC_POLARITY(TIM_ICPolarity)); assert_param(IS_TIM_IC_FILTER(ICFilter));

/* Configure the Timer Input Clock Source */

if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)

#define 的 TIM_TIxExternalCLKSource 值定义 备注/SMCR.TS[2:0]/bit6-4 TIM_TIxExternalCLK1Source_TI1 ((u16)0x0050) TIM IC1连接到TI1 TIM_TIxExternalCLK1Source_TI2 ((u16)0x0060) TIM IC2连接到TI2 TIM_TIxExternalCLK1Source_TI1ED ((u16)0x0040) TIM IC1连接到TI1:使用边沿探测 {

TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter); } else {

TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter); }

/* Select the Trigger source */

TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);

/* Select the External clock mode1 */ TIMx->SMCR |= TIM_SlaveMode_External1; 参数TIM_SlaveMode的定义 #define值 SMCR.SMS[2:0]/bit2-0 TIM_SlaveMode_Reset ((u16)0x0004) 复位模式 TIM_SlaveMode_Gated ((u16)0x0005) 门控模式 TIM_SlaveMode_Trigger ((u16)0x0006) 触发模式 TIM_SlaveMode_External1 ((u16)0x0007) 外部时钟模式1 }

/*TI2_Config函数定义如下:

static void TI2_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection, u16 TIM_ICFilter) {

u16 tmpccmr1 = 0, tmpccer = 0, tmp = 0;

/* Disable the Channel 2: Reset the CC2E Bit */ TIMx->CCER &= CCER_CC2E_Reset;

tmpccmr1 = TIMx->CCMR1; tmpccer = TIMx->CCER;

tmp = (u16)(TIM_ICPolarity << 4);

/* Select the Input and set the filter */

tmpccmr1 &= CCMR_CC24S_Mask & CCMR_IC24F_Mask; tmpccmr1 |= (u16)(TIM_ICFilter << 12); tmpccmr1 |= (u16)(TIM_ICSelection << 8);

/* Select the Polarity and set the CC2E Bit */ tmpccer &= CCER_CC2P_Reset;

tmpccer |= tmp | CCER_CC2E_Set;

/* Write to TIMx CCMR1 and CCER registers */ TIMx->CCMR1 = tmpccmr1 ; TIMx->CCER = tmpccer; } */

/* TI1_Config函数定义如下:

static void TI1_Config(TIM_TypeDef* TIMx, u16 TIM_ICPolarity, u16 TIM_ICSelection, u16 TIM_ICFilter) {

u16 tmpccmr1 = 0, tmpccer = 0;

/* Disable the Channel 1: Reset the CC1E Bit */ TIMx->CCER &= CCER_CC1E_Reset;

tmpccmr1 = TIMx->CCMR1; tmpccer = TIMx->CCER;

/* Select the Input and set the filter */

tmpccmr1 &= CCMR_CC13S_Mask & CCMR_IC13F_Mask;

tmpccmr1 |= TIM_ICSelection | (u16)(TIM_ICFilter << 4);

/* Select the Polarity and set the CC1E Bit */ tmpccer &= CCER_CC1P_Reset;

tmpccer |= TIM_ICPolarity | CCER_CC1E_Set;

/* Write to TIMx CCMR1 and CCER registers */ TIMx->CCMR1 = tmpccmr1; TIMx->CCER = tmpccer;

} */

19.2.15 函数TIM_ETRClockMode1Config

Table 492. 函数TIM_ETRClockMode1Config 函数名 TIM_ETRClockMode1Config void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, 函数原形 u16 TIM_ExtTRGPolarity, u16 ExtTRGFilter) 功能描述 配置TIMx外部时钟模式1 输入参数1 TIMx:x可以是2,3 ,4,来选择TIM外设 输入参数2 TIM_ExtTRGPrescaler:外部触发预分频 输入参数3 TIM_ExtTRGPolarity:外部时钟极性 输入参数4 ExtTRGFilter:外部触发滤波器。取值在0x0-0xF之间。 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_ExtTRGPrescaler :设置TIMx外部触发预分频。见Table 493. 参阅该参数的取值。 Table 493. TIM_ExtTRGPrescaler值 TIM_ExtTRGPrescaler 描述/SMCR. ETPS[1:0] #define值 备注 TIM_ExtTRGPSC_OFF TIM ETRP预分频 OFF 0x0000 TIM_ExtTRGPSC_DIV2 TIM ETRP频率/2 0x1000 bit13-12 TIM_ExtTRGPSC_DIV4 TIM ETRP频率/4 0x2000 TIM_ExtTRGPSC_DIV8 TIM ETRP频率/8 0x3000 TIM_ExtTRGPolarity:设置TIMx外部触发极性。见Table 494. 参阅该参数的取值。 Table 494. TIM_ExtTRGPolarity值 TIM_ExtTRGPolarity 描述/SMCR.ETP/bit15 #define值 TIM_ExtTRGPolarity_Inverted TIM外部触发极性翻转:低电平或下降沿有效 0x8000 TIM_ExtTRGPolarity_NonInverted TIM外部触发极性非翻转:高电平或上升沿有效 0x0000 例:

/* Selects the external clock Mode 1 for TIM2: the external clock is connected to ETR input pin, the rising edge is the active edge, no filter sampling is done (ExtTRGFilter = 0) and the prescaler is fixed to TIM_ExtTRGPSC_DIV2 */

TIM_ExternalCLK1Config(TIM2, TIM_ExtTRGPSC_DIV2, TIM_ExtTRGPolarity_NonInverted, 0x0); 函数原型如下:

void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity, u16 ExtTRGFilter) {

u16 tmpsmcr = 0;

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler)); assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity)); assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));

/* Configure the ETR Clock source */

TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);

/* Get the TIMx SMCR register value */ tmpsmcr = TIMx->SMCR;

/* Reset the SMS Bits */

tmpsmcr &= SMCR_SMS_Mask;//=0xFFF8-清除

/* Select the External clock mode1 */

tmpsmcr |= TIM_SlaveMode_External1;//0x0007—设置模式1

/* Select the Trigger selection : ETRF */ tmpsmcr &= SMCR_TS_Mask;//bit6-4

tmpsmcr |= TIM_TS_ETRF;// #define TIM_TS_ETRF ((u16)0x0070) //滤波分频

/* Write to TIMx SMCR */ TIMx->SMCR = tmpsmcr; }

19.2.16 函数TIM_ETRClockMode2Config

Table 495. 函数TIM_ETRClockMode2Config