在使用STM32F103VC 的ETR的功能的时候,发现使用TIM3的ETR对外部的脉冲可以实现正确的计数,而同样的设置用在TIM1上就出问题。
使用TIM1的ETR时,出现的问题如下:
只要TIM1 ETR输入脚(PA12)为低电平,TIM1的计数器就一直在计数(高电平时没有计数)。
而同样的设置在TIM3上,只有脉冲上下跳变时计数器才会计数。
目的:我是要TIM1也和TIM3一样,只有信号的上升沿或下降沿时才会计数。
以下是TIM1和TIM3的 ETR功能的初始化代码:
void TIM1_ETR_GetDropCounts_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//Config PA12 为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1, ENABLE);
}
void TIM3_ETR_GetDropCounts_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//Config PD2 为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
u16 n_Counter1;
u16 n_Counter2;
//主程序
int main(void)
{
while(1)
{
n_Counter1 = TIM_GetCounter(TIM1);
n_Counter2 = TIM_GetCounter(TIM3);
}
} |