编码器 mini STM32F103 差分输出

2019-07-20 22:16发布

编码器线速500P/R,程序也能够检测转过的角度,但是需要编码器的转动特别慢才能够检测中断计算,但是编码器一旦加快,就不能中断计数,请问各位路过的大佬造成这样的原因是编码器的线数太多了还是什么其它的原因?

***************************************************************************
#include "time.h"
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "lcd.h"
#include "string.h"
#include "stdio.h"
#include "usart.h"
//编码器
//定时器3通道1和2输入捕获配置
void TIM3_Cap_Init(void)
{  
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef  TIM3_ICInitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能TIM3时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟

GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_6| GPIO_Pin_7;//PA6,7清除之前设置  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//PA6,7 输入  
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7);//PA6 下拉

//初始化定时器3 TIM3  
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler = 71;//计数器的频率
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  
//初始化TIM3输入捕获参数
//通道1
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01  选择输入端 IC1映射到TI1
  TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//通道1和2都是初始化为上升沿捕获
  TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到TI1上
  TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//配置输入分频,不分频
  TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
  TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//通道2
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
  TIM3_ICInitStructure.TIM_ICFilter = 0x00;
  TIM_ICInit(TIM3, &TIM3_ICInitStructure);

//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  TIM_ITConfig(TIM3,  TIM_IT_CC1|TIM_IT_CC2,ENABLE);//使能定时器3输入捕获中断
  TIM_Cmd(TIM3, ENABLE);//开启定时器3

}
//定时器3中断服务程序
u8 Show_Data[30] = { 0 };
vs16 Count_pulse=0;
u8  TIM3CH1_CAPTURE_STA_A=0; //A相电平状态         
u8  TIM3CH2_CAPTURE_STA_B=0; //B相电平状态
u16 Sum=0;
u8 num_pulse=0;
float degree=0;
void TIM3_IRQHandler(void)
{
num_pulse++;
/**********************************************************************************************/
//捕获1发生捕获事件
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清除中断标志位
  if (TIM3CH1_CAPTURE_STA_A == 1)//捕获到一个下降沿 A↓=2
  {
   LED1 = 1;
   TIM3CH1_CAPTURE_STA_A = 0;
   TIM_OC1PolarityConfig(TIM3, TIM_ICPolarity_Rising);//CC1P=0 设置为上升沿捕获
   switch (num_pulse)
   {
   case 1:Sum = Sum + 2000; break;
   case 2:Sum = Sum + 200; break;
   case 3:Sum = Sum + 20; break;
   case 4:Sum = Sum + 2; break;
   }
  }
  else//还未开始,第一次捕获上升沿  A↑=1
  {
   //清空状态
   TIM3CH1_CAPTURE_STA_A = 0;
   LED1 = 0;
   TIM3CH1_CAPTURE_STA_A = 1;
   TIM_OC1PolarityConfig(TIM3, TIM_ICPolarity_Falling);//CC1P=1 设置为下降沿捕获
   switch (num_pulse)
   {
   case 1:Sum = Sum + 1000; break;
   case 2:Sum = Sum + 100; break;
   case 3:Sum = Sum + 10; break;
   case 4:Sum = Sum + 1; break;
   }
  }
}
/********************************************************************************************/
//捕获2发生捕获事件
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);//清除中断标志位
  if (TIM3CH2_CAPTURE_STA_B == 1)//捕获到一个下降沿 B↓=4
  {
   TIM3CH2_CAPTURE_STA_B = 0;
   TIM_OC2PolarityConfig(TIM3, TIM_ICPolarity_Rising);
   switch (num_pulse)
   {
   case 1:Sum = Sum + 4000; break;
   case 2:Sum = Sum + 400; break;
   case 3:Sum = Sum + 40; break;
   case 4:Sum = Sum + 4; break;
   }
  }
  else//还未开始,第一次捕获上升沿  B↑=3
  {
   TIM3CH2_CAPTURE_STA_B = 0;
   TIM3CH2_CAPTURE_STA_B = 1;
   TIM_OC2PolarityConfig(TIM3, TIM_ICPolarity_Falling);
   switch (num_pulse)
   {
   case 1:Sum = Sum + 3000; break;
   case 2:Sum = Sum + 300; break;
   case 3:Sum = Sum + 30; break;
   case 4:Sum = Sum + 3; break;
   }
  }
}
if (num_pulse == 4)
{
  switch (Sum)
  {
  //从轴侧看向右转
  case 1324:Count_pulse++; break;//A↑B↑A↓B↓
  case 3241:Count_pulse++; break;//B↑A↓B↓A↑
  case 2413:Count_pulse++; break;//A↓B↓A↑B↑
  case 4132:Count_pulse++; break;//B↓A↑B↑A↓
  //从轴侧看向右转
  case 3142:Count_pulse--; break;//B↑A↑B↓A↓
  case 1423:Count_pulse--; break;//A↑B↓A↓B↑
  case 4231:Count_pulse--; break;//B↓A↓B↑A↑
  case 2314:Count_pulse--; break;//A↓B↑A↑B↓
  }
  degree=0.72*Count_pulse;
  sprintf((char*)Show_Data, "ROTARY ANGLE:%.2f D", degree); LCD_ShowString(25, 260, 320, 16, 16, Show_Data);
  Sum = 0, num_pulse = 0;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。