stm32f207编码器接口模式 计数器不计数
[复制链接]
大家好,初学stm32,正在做stm32f207编码器接口实验,有几个问题想请教高手
一、5v供电的编码器输出脉冲幅度4.2V左右,stm32f207最高供电电压3.6V,请问编码器能否直接接到单片机的引脚上,手册上说是可以直接接的,下面是st32f2XX参考手册上的描述
An external incremental encoder can be connected directly to the MCU without external
interface logic. However, comparators are normally be used to convert the encoder’s
differential outputs to digital signals. This greatly increases noise immunity. The third
encoder output which indicate the mechanical zero position, may be connected to an
external interrupt input and trigger a counter reset.
二、下面是我参考stm32f1XX编码器接口例程,改写的一段代码,配置TIM4为编码器模式。用TIM3定时读取TIM4的数值,可是接入编码器的时候计数器不计数。请大家帮忙分析下原因
void Encoder_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PB6 A相 PB7 B相
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); //使能GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//Timer configuration in Encoder mode
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE); //使能TIM4时钟 1Mhz
TIM_TimeBaseStructure.TIM_Prescaler = 30-1; // 30分频1MHz
TIM_TimeBaseStructure.TIM_Period = 65500; //自动重装载的值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //编码器模式下计数方向由硬件决定,这里配置成向上的计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//Encoder configuration
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,
TIM_ICPolarity_BothEdge , TIM_ICPolarity_BothEdge); //在TI1和TI2的上升沿和下降沿都计数
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 2;//ICx_FILTER;2次滤波
TIM_ICInit(TIM4, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清标志位
//Reset counter设定初始值
TIM_SetCounter(TIM4, 0x7fff);
//使能计数器
TIM_Cmd(TIM4, ENABLE);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM //如果C/C++ Compiler\Preprocessor\Defined symbols中的定义了VECT_TAB_RAM(见程序库更改内容的表格)
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //则在RAM调试
#else //如果没有定义VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);//则在Flash里调试
#endif
// Enable the TIM2 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* ---------------------------------------------------------------
TIM3 Configuration: Output Compare Timing Mode:
TIM3 counter clock at 30 MHz 3000分频为10kh
--------------------------------------------------------------- */
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 5000;
TIM_TimeBaseStructure.TIM_Prescaler = 3000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM3, 12000-1, TIM_PSCReloadMode_Immediate);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}