STM32F10x - TIM - 图文 下载本文

函数名 函数原形 功能描述 输入参数1 输入参数2 输入参数3 输入参数4 输出参数 返回值 先决条件 被调用函数 TIM_ETRClockMode2Config void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, u16 TIM_ExtTRGPolarity, u16 ExtTRGFilter) 配置TIMx外部时钟模式2 TIMx:x可以是1,2,3,4,5,8,来选择TIM外设 TIM_ExtTRGPrescaler:外部触发预分频 TIM_ExtTRGPolarity:外部时钟极性 ExtTRGFilter:外部触发滤波器。取值在0x0-0xF之间。 无 无 无 无 例: /* Selects the external clock Mode 2 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_ExternalCLK2Config(TIM2, TIM_ExtTRGPSC_DIV2, TIM_ExtTRGPolarity_NonInverted, 0x0); 函数原型如下:

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

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));//关;/2;/4;/8

assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));//0x8000(翻转);0x0000(不) assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));

/* Configure the ETR Clock source */

TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);//外部时钟设置(共三个)

/* Enable the External clock mode2 */

TIMx->SMCR |= SMCR_ECE_Set;//外部时钟使能:SMCR.bit14 [ECE] }

/*

void TIM_ETRConfig(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));

tmpsmcr = TIMx->SMCR;

/* Reset the ETR Bits */

tmpsmcr &= SMCR_ETR_Mask;// #define SMCR_ETR_Mask ((u16)0x00FF)

/* Set the Prescaler, the Filter value and the Polarity */

tmpsmcr |= TIM_ExtTRGPrescaler | TIM_ExtTRGPolarity | (u16)(ExtTRGFilter << 8);// bit13-12 | bit15 | bit11-8<=0xF

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

19.2.17 函数TIM_ETRConfig

Table 496. 函数TIM_ETRConfig 函数名 TIM_ETRConfig void TIM_ETRConfig(TIM_TypeDef* TIMx, u16 TIM_ExtTRGPrescaler, 函数原形 u16 TIM_ExtTRGPolarity, u8 ExtTRGFilter) 功能描述 配置TIMx外部触发 输入参数 1 TIMx:x可以是1,2,3,4,5,8来选择TIM外设 输入参数 2 TIM_ExtTRGPrescaler:外部触发预分频 输入参数 3 TIM_ExtTRGPolarity:外部时钟极性 输入参数 4 ExtTRGFilter:外部触发滤波器。取值在0x0-0xF之间。 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例:

/* Configure the External Trigger (ETR) for TIM2: the rising edge is the active edge, no filter sampling is done (ExtTRGFilter = 0) and the prescaler is fixed to TIM_ExtTRGPSC_DIV2 */

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

void TIM_ETRConfig(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));//关闭,1/2;1/4;1/8。 assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));//bit 15 assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter)); //<=0xF

tmpsmcr = TIMx->SMCR;

/* Reset the ETR Bits */

tmpsmcr &= SMCR_ETR_Mask;// #define SMCR_ETR_Mask ((u16)0x00FF)//清除所有ETR设置。

/* Set the Prescaler, the Filter value and the Polarity */

tmpsmcr |= TIM_ExtTRGPrescaler | TIM_ExtTRGPolarity | (u16)(ExtTRGFilter << 8); // bit13-12 | bit15 | bit11-8<0xF

/* Write to TIMx SMCR */

TIMx->SMCR = tmpsmcr;//设置但不使能。 }

19.2.18 函数TIM_SelectInputTrigger

Table 497. 函数TIM_SelectInputTrigger 函数名 TIM_SelectInputTrigger 函数原形 void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, u16 TIM_InputTriggerSource) 功能描述 选择TIMx输入触发源 输入参数1 TIMx:x 可以是1,2,3,4,5,8来选择TIM外设 输入参数2 TIM_InputTriggerSource:输入触发源 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_InputTriggerSource :选择TIMx输入触发源。见Table 498. 参阅该参数的取值。 Table 498. TIM_InputTriggerSource值 TIM_InputTriggerSource 描述/SMCR. TS[2:0]/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 TIM TL1边沿探测器 0x0040 TIM_TS_TI1FP1 TIM经滤波定时器输入1 0x0050 TIM_TS_TI2FP2 TIM经滤波定时器输入2 0x0060 TIM_TS_ETRF TIM外部触发输入 0x0070 例: /* Selects the Internal Trigger 3 as input trigger fot TIM2 */ void TIM_SelectInputTrigger(TIM2, TIM_TS_ITR3); 函数原型如下:

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;//(u16)0xFF8F

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

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

19.2.19 函数TIM_PrescalerConfig

Table 499. 函数TIM_PrescalerConfig //为CNT工作做准备 函数名 TIM_PrescalerConfig void TIM_PrescalerConfig(TIM_TypeDef* TIMx, u16 Prescaler, 函数原形 u16 TIM_PSCReloadMode) 功能描述 设置TIMx预分频 输入参数1 TIMx:x 可以是1,2,3,4,5,8来选择TIM外设 输入参数2 CNT的前 预分频值(0x0000-0xFFFF)【CK_PSC?CK_CNT】 输入参数3 TIM_PSCReloadMode:预分频重载模式 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_PSCReloadMode:选择预分频重载模式。见Table 500. 参阅该参数的取值。 Table 500. TIM_PSCReloadMode值 TIM_PSCReloadMode 描述/EGR.bit0/UG #define值 TIM_PSCReloadMode_Update TIM预分频值在更新事件时装入 0x0000 TIM_PSCReloadMode_Immediate TIM预分频值即时装入 0x0001 例: /* Configures the TIM2 new Prescaler value */ u16 TIMPrescaler = 0xFF00;

TIM_PrescalerConfig(TIM2, TIMPrescaler, TIM_PSCReloadMode_Immediate); 函数原型如下:

void TIM_PrescalerConfig(TIM_TypeDef* TIMx, u16 Prescaler, u16 TIM_PSCReloadMode) {

/* Check the parameters */

assert_param(IS_TIM_ALL_PERIPH(TIMx));

assert_param(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));

/* Set the Prescaler value */

TIMx->PSC = Prescaler;//fCK_CNT=fCK_PSC/(PSC[15:0]+1)

/* Set or reset the UG Bit */ TIMx->EGR = TIM_PSCReloadMode;

//此语句不是最佳方式:把TIM_PSCReloadMode_Update的值改为OxFFFE,然后用条件来&或|,避免更改EGR的其他位。 }

19.2.20 函数TIM_CounterModeConfig

Table 501. 函数TIM_CounterModeConfig 函数名 TIM_CounterModeConfig 函数原形 void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode) 功能描述 设置TIMx计数器模式 输入参数1 TIMx:x 可以是1,2,3,4,5,8来选择TIM外设 输入参数2 TIM_CounterMode:待使用的计数器模式 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_CounterMode:待使用的计数器模式。

参数TIM_CounterMode的值 TIM_CounterMode_Up TIM_CounterMode_Down 描述/CR1. CMS[1:0] +DIR/bit6-5+bit4 向上计数 向下计数 #define值 ((u16)0x0000) ((u16)0x0010) CMS[1:0]中央计数方向 对齐模式选择 bit6-5=00 [边缘对齐] bit4=0 bit4=1 bit4=0 对CMS无效 TIM_CounterMode_CenterAligned1 中央对齐模式1 TIM_CounterMode_CenterAligned2 中央对齐模式1 TIM_CounterMode_CenterAligned3 中央对齐模式1 ((u16)0x0020) bit6-5=01 ((u16)0x0040) bit6-5=10 ((u16)0x0060) bit6-5=11 例:

/* Selects the Center Aligned counter Mode 1 for the TIM2 */ TIM_CounterModeConfig(TIM2, TIM_Counter_CenterAligned1); 函数原型如下:

void TIM_CounterModeConfig(TIM_TypeDef* TIMx, u16 TIM_CounterMode) {

u16 tmpcr1 = 0;

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_COUNTER_MODE(TIM_CounterMode));

tmpcr1 = TIMx->CR1;

/* Reset the CMS and DIR Bits */

tmpcr1 &= CR1_CounterMode_Mask;//0xFF8F=bit6-5+bit4

/* Set the Counter Mode */ tmpcr1 |= TIM_CounterMode;

/* Write to TIMx CR1 register */ TIMx->CR1 = tmpcr1; }

19.2.21 函数TIM_ForcedOC1Config

Table 502. 函数 TIM_ForcedOC1Config 函数名 TIM_ForcedOC1Config 函数原形 void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction) 功能描述 置TIMx输出1为活动或非活动电平 输入参数 1 TIMx:x 可以是1,2,3,4,5,8来选择TIM外设 输入参数 2 TIM_ForcedAction:输出信号的设置动作 输出参数 无 返回值 无 先决条件 无 被调用函数 无 TIM_ForcedAction :输出信号的设置动作取值见下表。 Table 503. TIM_ForcedAction值 参数TIM_ForcedAction值 描述/CCMR. OCyM[2:0]/bit #define值 备注 TIM_ForcedAction_Active 置为OCxREF上的活动电平 0x0050 强制高电平 TIM_ForcedAction_InActive 置为OCxREF上的非活动电平 0x0040 强制低电平[无效] 例: /* Forces the TIM2 Output Compare 1 signal to the active level */ TIM_ForcedOC1Config(TIM2, TIM_ForcedAction_Active); 函数原型如下:

void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, u16 TIM_ForcedAction) {

u16 tmpccmr1 = 0;

/* Check the parameters */

assert_param(IS_TIM_123458_PERIPH(TIMx));

assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));

tmpccmr1 = TIMx->CCMR1;

/* Reset the OC1M Bits */

tmpccmr1 &= CCMR_OC13M_Mask;//OxFF8F,bit6-4 = CCMR1.OC1M[2:0]

/* Configure The Forced output Mode */