您好!
我们是连接到端口 H 的编码器。编码器实际上是一个3位旋钮、2位用于识别编码器是按时钟还是按时钟旋转、第3位用作按钮、在的过程和微调之间切换 电源输出电流。 旋钮零件号为427-121251AL121。
我们将在两个中断之后运行;
- GPIO_PIN_0上的中断1:用于检测旋钮的输出电流设置中断顺时针旋转或反时钟。 该中断工作正常、没有弹跳效应
- 因为信号上有一个1kRx0.1uC 滤波器。
- GPIO_PIN_2上的中断2: 粗略/精细调节中断、用于定义要使用粗略分辨率或精细分辨率进行调整的输出电流。 该中断触发两次。 由于信号上有一个1kRx0.1uC 滤波器、因此不会出现抖动效应。
中断1工作正常。 它被设置为在脉冲的两个边沿上触发。 如定义 为 RSW_D0引脚的 GPIO_PIN_0引脚的以下代码所示;
void EncoderPulses_interrupt_enable(void)
{
IntMasterDisable();
GPIOPinTypeGPIOInput(ENC_PORT, RSW_D0);
GPIOPinTypeGPIOInput(ENC_PORT, RSW_D1);
GPIOPadConfigSet(ENC_PORT, RSW_D0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(ENC_PORT, RSW_D1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // Enable weak pullup resistor
GPIOIntDisable(ENC_PORT, RSW_D0); // Disable interrupt
GPIOIntClear(ENC_PORT, RSW_D0); // Clear pending interrupts
GPIOIntRegister(ENC_PORT, Encoder_IntHandler);
GPIOIntTypeSet(ENC_PORT, RSW_D0, GPIO_BOTH_EDGES);
GPIOIntEnable(ENC_PORT, RSW_D0); // Enable interrupt
IntMasterEnable();
}
中断2每次被激活时触发两次。 该中断配置为仅在下降沿触发。 以下是在引脚 GPIO_PIN_2上配置中断的代码、该引脚定义为 RSW_D2引脚。
void EncoderMode_interrupt_enable(void)
{
IntMasterDisable();
GPIOPinTypeGPIOInput(ENC_PORT, RSW_D2);
GPIOPadConfigSet(ENC_PORT, RSW_D2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // Enable weak pullup resistor
GPIOIntDisable(ENC_PORT, RSW_D2); // Disable interrupt
GPIOIntClear(ENC_PORT, RSW_D2); // Clear pending interrupts
GPIOIntRegister(ENC_PORT, Encoder_IntHandler);
GPIOIntTypeSet(ENC_PORT, RSW_D2, GPIO_FALLING_EDGE); // Configure falling edge trigger
GPIOIntEnable(ENC_PORT, RSW_D2); // Enable interrupt
IntMasterEnable();
}
以下是在两个引脚(例如 GPIO_PIN_2 和 /或 GPIO_PIN_0)上触发中断时调用的函数
void Encoder_IntHandler(void)
{
SysCtlDelay(1*17000);//Clear the Interrupt after the Delay to avoid multiple Interrupt entries.
if(GPIOIntStatus(ENC_PORT, false) & RSW_D0) //Check if the Interrupt is for RSW_D0 (GPIO_PIN_0) or RSW_D2 (GPIO_PIN_2)
{
uint8_t i,j;
GPIOIntClear(ENC_PORT, RSW_D0);
i = GPIOPinRead(ENC_PORT,RSW_D0);
j = GPIOPinRead(ENC_PORT,RSW_D1);
if (j==2){j = 1;} // When GPIO_PIN_1 is HIGH it GPIOPinRead() will report 0x2H. Replace it to 0x1H
//Detect if the Encoder is moved CC or ACC
if(i == j){EncoderServiceReqType = EncoderCCW;} //Encoder moved Anti-Clockwise
else{EncoderServiceReqType = EncoderCW;} //Encoder moved Clockwise
}
else
{
GPIOIntClear(ENC_PORT, RSW_D2);
EncoderServiceReqType = EncoderMBtn;
}
EncoderServiceReqFlag = 1;
}
定义是:
#define ENC_PORT GPIO_PORTH_BASE #define RSW_D0 GPIO_PIN_0 // Interrupt 1 Pin. #define RSW_D1 GPIO_PIN_1 #define RSW_D2 GPIO_PIN_2 //Interrupt 2 Pin. Encoder Push Button to switch between Fine and Coarse adjustment
感谢大家的帮助。
此致、
Sahil