我想用编码器测量角度,TIM3的CH1和CH2读不出编码器的脉冲

2019-08-16 23:21发布

我想用编码器测量角度,我用的是欧姆龙 E6B2-CWZ6C 500线编码器,我就把A和B接到了tim3的ch1和ch2上(Z相没接),然后设置了浮空输入,我用万用表测这两脚的电压都是3.3V,调试时TIM3_CNT寄存器只会从0变到1.又从1变到0。但是如果独立把a相和b相用示波器测,是可以看到脉冲的,这是怎么回事?求各位大神帮帮忙!我的程序如下:
#include "encoder.h"
void TIM3_Mode_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;                 
NVIC_InitTypeDef NVIC_InitStructure;
/*----------------------------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //????????????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);             //????GPIOA
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;           //????PA6 A7????A?à??B?à
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????????????
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);                             //??????GPIOA

/*----------------------------------------------------------------*/
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure);
 /*---------------------------------------------------------------------*/
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period =0x07cf;       // ??×°???? 500*4-1
  TIM_TimeBaseStructure.TIM_Prescaler =0;       //?è???¤·?????
  TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; //?è???±??·???????????·???
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //?ò??????????
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);               //??????TIM3

/*-----------------------------------------------------------------*/
//±à??????                      
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);  //TIM_ICPolarity_Rising??????????
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter =6;         //±??????¨?÷
  TIM_ICInit(TIM3, &TIM_ICInitStructure);
  
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);   //????????
  //Reset counter
  TIM3->CNT =0;

TIM_Cmd(TIM3, ENABLE);   //?????¨?±?÷3
}
void TIM_Init(void)
{
  TIM3_Mode_Config();
}

----------------------------------------------------------------------------------------------------------------------------------------
#include "delay.h"
#include "delay.h"
#include "GUI.h"
#include "sys.h"
#include "Lcd_Driver.h"
#include "usart.h"
#include "encoder.h"

 int main(void)
 {
  uint16_t encoder_num,a[5],t,d;
  float jiaodu,temp;  
delay_init();     //???±??????????  
NVIC_Configuration(); //?è??NVIC????·?×é2:2??????????????2???ì????????
uart_init(115200); //?®??????????9600
LCD_LED_SET;      //?¨??IO????±?????
Lcd_Init(0);
TIM_Init();
Lcd_Clear(GRAY0);
Gui_DrawFont_GBK16(0,20,GREEN,GRAY0,"????????!");
Gui_DrawFont_GBK16(0,40,BLUE,GRAY0,"?ù????????:");
Gui_DrawFont_GBK16(42,60,RED,GRAY0,"000.00??");
  while(1)
{
delay_ms(500);   
    encoder_num=TIM_GetCounter(TIM3);
jiaodu=0.72*encoder_num;
d=jiaodu;
t=d/10;
a[0]=t/10;

Gui_DrawFont_Num(42,60,RED,GRAY0,a[0]+0x10);
 a[1]=t%10;
Gui_DrawFont_Num(50,60,RED,GRAY0,a[1]+0x10);
a[2]=d%t;

Gui_DrawFont_Num(58,60,RED,GRAY0,a[2]+0x10);

temp=jiaodu-d;
temp=temp*10;
a[3]=temp;

Gui_DrawFont_Num(74,60,RED,GRAY0,a[3]+0x10);
temp=temp-a[3];
temp=temp*10;
a[4]=temp;

Gui_DrawFont_Num(82,60,RED,GRAY0,a[4]+0x10);
}

 }
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。