Part Number:TM4C123GH6PM
我使用TM4C的两个qei读取正交编码器,最开始两边都出现方向不断改变的问题(实际往一个方向转,但是反馈的方向一直在1和-1之间跳),后面发现其中一个是引脚被锁定了,解锁之后问题解决,但是另一个还是跟原来一模一样的状态,解锁引脚也试过但是没有用,这是什么问题?求解,代码如下:
void Encoder_Init()
{
// QEI Config
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // We are using PD7 for PhB0, QEI module 0 phase B
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // PC5 – A,PC6 – B
SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI1);
// /*
//Unlock GPIOD7 – Like PF0 its used for NMI – Without this step it doesn't work
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; //In Tiva include this is the same as "_DD" in older versions (0x4C4F434B)
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) |= 0x80; // better to use GPIO_PIN_7
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = 0;
// */
/*
HWREG(GPIO_PORTD_BASE+0x520) = 0x4C4F434B;
HWREG(GPIO_PORTD_BASE+0x524) |= GPIO_PIN_7;
HWREG(GPIO_PORTD_BASE+0x520) = 0;
// */
//Set Pins to be PHA0 and PHB0 for QEI 0
GPIOPinConfigure(GPIO_PD6_PHA0); // QEI module 0 phase A
GPIOPinConfigure(GPIO_PD7_PHB0); // QEI module 0 phase B
//Set Pins to be PHA1 and PHB1 for QEI 1
GPIOPinConfigure(GPIO_PC5_PHA1); // QEI module 1 phase A
GPIOPinConfigure(GPIO_PC6_PHB1); // QEI module 1 phase B
//Set GPIO pins for QEI. PhA0 -> PD6, PhB0 ->PD7. I believe this sets the pull up and makes them inputs
GPIOPinTypeQEI(GPIO_PORTD_BASE, GPIO_PIN_6 | GPIO_PIN_7);
GPIOPinTypeQEI(GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6);
// GPIOPadConï¬gSet (GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet (GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
// GPIOPadConï¬gSet ();
//Disable peripheral and int before configuration
QEIDisable(QEI0_BASE);
QEIIntDisable(QEI0_BASE,QEI_INTERROR | QEI_INTDIR | QEI_INTTIMER | QEI_INTINDEX);
// Configure quadrature encoder, use an arbitrary top limit of 1000 (debug)
QEIConfigure(QEI0_BASE, (QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_NO_RESET | QEI_CONFIG_QUADRATURE | QEI_CONFIG_NO_SWAP), 9216);
// Enable the quadrature encoder.
QEIEnable(QEI0_BASE);
//Set position to a middle value so we can see if things are working
QEIPositionSet(QEI0_BASE, 0);
QEIDisable(QEI1_BASE);
QEIIntDisable(QEI1_BASE,QEI_INTERROR | QEI_INTDIR | QEI_INTTIMER | QEI_INTINDEX);
// Configure quadrature encoder, use an arbitrary top limit of 1000
QEIConfigure(QEI1_BASE, (QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_NO_RESET | QEI_CONFIG_QUADRATURE | QEI_CONFIG_NO_SWAP), 9000);
// Enable the quadrature encoder.
QEIEnable(QEI1_BASE);
//Set position to a middle value so we can see if things are working
QEIPositionSet(QEI1_BASE, 0);
QEIVelocityConfigure(QEI0_BASE, QEI_VELDIV_1, SysCtlClockGet()/100);
QEIVelocityEnable(QEI0_BASE);
QEIVelocityConfigure(QEI1_BASE, QEI_VELDIV_1, SysCtlClockGet()/100);
QEIVelocityEnable(QEI1_BASE);
// filter the inputs
QEIFilterConfigure (QEI0_BASE,
QEI_FILTCNT_17);
QEIFilterEnable (QEI0_BASE);
QEIFilterConfigure (QEI1_BASE,
QEI_FILTCNT_17);
QEIFilterEnable (QEI1_BASE);
}
Yale Li:
看一下这个帖子有没有帮助:
https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1174682/tm4c123-qei0-module-not-working