[求助] stm32f207编码器接口模式 计数器不计数

阿郑   2012-8-29 10:57 楼主
大家好,初学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);
}

回复评论 (5)

看代码太累,这个是我的,你试试
点赞  2012-8-29 18:08
#include
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "spwm.h"
#define DV_BUFFER_SIZE        256
u16 Encoder_Mem;
u16 Encoder_Dir;
u32 Cap_Timer=0;        //上次捕获时间
u32 Encode_Time0=0;                //码盘捕获单位时间
u32 Encode_Time1=0;
               
u32 Acceleration[DV_BUFFER_SIZE];
u16 Acceleration_point=0;
void TIM4_IRQHandler(void)
{
  u32 Current_time=((u32)Tim2_Base16<<16)+TIM2->CNT;//本次捕获时间
  u32 Current_en,tmp32;
  u16 tmp16;
/*        if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) {
                TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
        }
*/
  if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) {
//        if (Encoder_Mem!=TIM4->CNT) {               
          Encoder_Dir=(u16)TIM4->CNT-Encoder_Mem;                        //码盘增量
        Encoder_Mem=TIM4->CNT;                                                        //刷新
        if (Encoder_Dir & 0x8000)                                                 //码盘增量绝对值
                tmp16=~Encoder_Dir+1;
        else
                tmp16=Encoder_Dir;
        if (tmp16 !=0) {
                Current_en=(Current_time-Cap_Timer)/tmp16;                //单位时间
                Cap_Timer=Current_time;
                  tmp32=Current_en+Encode_Time0-2*Encode_Time1;                //单位时间增量
                  if (tmp32&0x80000000) tmp32=~tmp32+1;                    //时间增量绝对值
                Encode_Time0=Encode_Time1;Encode_Time1=Current_en;        //刷新
                Acceleration_point++;Acceleration_point&=(DV_BUFFER_SIZE-1);
                Acceleration[Acceleration_point]=tmp32;
        }
        TIM4->SR &= ~TIM_IT_CC1;
        //TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
        }

}
void TIM4_Init(void) {
          TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
          TIM_ICInitTypeDef TIM_ICInitStructure;  
          GPIO_InitTypeDef GPIO_InitStructure;

          /* TIM4 clock source enable */
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
          /* Enable GPIOA, clock */
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
          GPIO_StructInit(&GPIO_InitStructure);
          /* Configure PA.06,07 as encoder input */
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOA, &GPIO_InitStructure);

          /* Timer configuration in Encoder mode */
          TIM_DeInit(TIM4);
          TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  
          TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // No prescaling
          TIM_TimeBaseStructure.TIM_Period = T4_OVERFLOW;        //溢出设置  
          TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
          TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

          TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
        //TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
          TIM_ICStructInit(&TIM_ICInitStructure);
          TIM_ICInitStructure.TIM_ICFilter = 15;
          TIM_ICInit(TIM4, &TIM_ICInitStructure);
// Clear all pending interrupts
          TIM_ClearFlag(TIM4, TIM_FLAG_Update|TIM_IT_CC1|TIM_IT_CC2);
          TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
          //TIM_ITConfig(TIM4, TIM_FLAG_Update, ENABLE);
          //Reset counter
          TIM4->CNT=0x8000;
//  ENC_Clear_Speed_Buffer();  
          TIM_Cmd(TIM4, ENABLE);
}
点赞  2012-8-29 18:11
好像这段不一样
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
点赞  2012-8-29 18:17
您好!请问一下你这个程序是用什么软件打开的??
点赞  2012-9-11 09:56
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复