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); } |
回复:stm32f207编码器接口模式 计数器不计数
2看看你的中断函数配置好了吗
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
回复:stm32f207编码器接口模式 计数器不计数【悬赏问答】
1大部分引脚可以直接接的,少数不可以,参见数据手册
2看看你的中断函数配置好了吗
我直接接在stm32引脚上,用示波器能观察到编码器的脉冲波形,幅值是3.2-3.4V左右。TIM3的中断函数没有问题,我直接用TIM3做的计数器可以正确计数。可是现在还是不能计输入的脉冲,郁闷!! 谢谢你的回答!
回复:stm32f207编码器接口模式 计数器不计数【悬赏问答】
关于STM模块的使用,要注意各个模块的共性:开启时钟、配置参数、配置中断、中断代码的处理。
谢谢 !问题还没有解决
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
回复:stm32f207编码器接口模式 计数器不计数【悬赏问答】
您好,请问您的IO方向配置了吗?
你好,IO方向配置啦
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_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4);
之前没有配置最后这两句,所以不能计数,现在可以计数了,但是计数不正确,我正在努力找原因。本以为只对GPIO模式配置就可以呢,原来不行。STM32F1XX的编码器例程中都没有最后这两句,所以憋了我好久才发现问题所在,还是对固件库各函数的功能不熟悉呀!!等我调试好代码,把它发到论坛供大家参考。再次感谢管理员的回答!有你们这些爱帮助别人的前辈在,学起来也更有信心啦!!!
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
RE:stm32f207编码器接口模式 计数器不计数【悬赏问答】
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
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 TIM5 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//PA0 A相 PA1 B相
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5);
//Timer configuration in Encoder mode
TIM_DeInit(TIM5);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5 , ENABLE); //30MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0; // 未分频
TIM_TimeBaseStructure.TIM_Period = 0xfffffff;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12,
TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity= TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICFilter = 0;//ICx_FILTER;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity= TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICFilter = 0;//ICx_FILTER;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
//Clear all pending interrupts
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);// 不中断
//Reset counter设定初始值
TIM_SetCounter(TIM5, 0x7fffffff);
TIM_Cmd(TIM5, ENABLE);
}
回复:stm32f207编码器接口模式 计数器不计数【悬赏问答】
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOA时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA6 PA7浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_Config(4);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 360*4; //设定计数器重装值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器
TIM_ICInit(TIM4, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM3
// TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM3的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//运行更新中断
//Reset counter
TIM4->CNT = 0;//
TIM_Cmd(TIM4, ENABLE); //启动TIM4定时器
}