This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123 QEI0 module not working

Other Parts Discussed in Thread: TM4C123GH6PM

Hi i am using TM4C123GH6PM, i am using QEI module. with the example code QEI1 module is working... with the same code it is not working for QEI0 module....any suggestions..

  • Hello Anuprash,

    Can you post both the working QEI1 code and the not working QEI0 code so I can compare them?

    Best Regards,

    Ralph Jacobi

  •  FOR QEI1 MODULE TAKEN FROM THE EXAMPLE CODE OF CCS
     
     
    /* -----------------------          Include Files       --------------------- */
    #include <stdint.h>                         // Library of Standard Integer Types
    #include <stdbool.h>                        // Library of Standard Boolean Types
    #include "inc/tm4c123gh6pm.h"               // Definitions for interrupt and register assignments on Tiva C
    #include "inc/hw_memmap.h"                  // Macros defining the memory map of the Tiva C Series device
    #include "inc/hw_types.h"                   // Defines common types and macros
    #include "inc/hw_gpio.h"                    // Defines Macros for GPIO hardware
    #include "inc/hw_qei.h"                     // Macros used when accessing the QEI hardware
    #include "driverlib/debug.h"                // Macros for assisting debug of the driver library
    #include "driverlib/sysctl.h"               // Defines and macros for System Control API of DriverLib
    #include "driverlib/interrupt.h"            // Defines and macros for NVIC Controller API of DriverLib
    #include "driverlib/gpio.h"                 // Defines and macros for GPIO API of DriverLib
    #include "driverlib/qei.h"                  // Prototypes for the Quadrature Encoder Driver
    #include "driverlib/pin_map.h"              // Mapping of peripherals to pins for all parts
    #include "driverlib/rom.h"                  // Defines and macros for ROM API of driverLib
    
    #define VEL_INT_FREQ    10000               // Macro to store the Interrupt frequency of QEI1
    #define QEI1_PPR        4000             // Macro to store the PPR of the QEI1
    
    /* -----------------------      Global Variables        --------------------- */
    volatile uint32_t ui32Qei1Vel;              // Variable to store the velocity of QEI1
    volatile uint32_t ui32Qei1Pos;              // Variable to store the position of QEI1
    volatile int32_t i32Qei1Dir;                // Variable to store the direction of QEI1
    volatile uint16_t ui16Qei1Rpm;              // Variable to store the RPM of QEI1
    
    /* -----------------------      Function Prototypes     --------------------- */
    void QEI1IntHandler(void);
    unsigned long int a;
    /* -----------------------          Main Program        --------------------- */
    int main(void){
    
       // a++;
        // Set the System clock to 80MHz
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 |SYSCTL_USE_PLL |SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
    
        // Enable the clock for peripherals PortC and QEI1
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI1);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    
        // Configure the PC5, PC6 for QEI signals
        ROM_GPIOPinTypeQEI(GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6);
        ROM_GPIOPinConfigure(GPIO_PC5_PHA1);
        ROM_GPIOPinConfigure(GPIO_PC6_PHB1);
    
        // Configure the QEI1 to increment for both PhA and PhB for quadrature input with "QEI1_PPR" PPR
        ROM_QEIConfigure(QEI1_BASE, QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_QUADRATURE, QEI1_PPR);
        // Configure the QEI1 for Velocity Calculation, Predivide by 1 at "VEL_INT_FREQ" Hz
        ROM_QEIVelocityConfigure(QEI1_BASE, QEI_VELDIV_1, ROM_SysCtlClockGet() / VEL_INT_FREQ);
        ROM_QEIVelocityEnable(QEI1_BASE);
    
        // Enable the Interrupts for Velocity Timer Expiration of QEI1
        void (*QEI1IntHandler_ptr)(void) = &QEI1IntHandler;
        QEIIntRegister(QEI1_BASE, *QEI1IntHandler_ptr);
        ROM_QEIIntEnable(QEI1_BASE, QEI_INTTIMER);
    
        // Master interrupt enable API for all interrupts
        ROM_IntMasterEnable();
        // Enable the QEI1
        ROM_QEIEnable(QEI1_BASE);
    
        while (1);
    }
    
    /* -----------------------      Function Definition     --------------------- */
    void QEI1IntHandler(void){
        // ISR for Quadrature Encoder Interface 1
    
        // Clear the Interrupt that is generated
        ROM_QEIIntClear(QEI1_BASE, ROM_QEIIntStatus(QEI1_BASE, true));
        // Calculate the number of quadrature ticks in "1 / VEL_INT_FREQ" time period
        ui32Qei1Vel = ROM_QEIVelocityGet(QEI1_BASE);
        // Update the position reading of the encoder
        ui32Qei1Pos = ROM_QEIPositionGet(QEI1_BASE);
        // Update the direction reading of the encoder
        i32Qei1Dir = ROM_QEIDirectionGet(QEI1_BASE);
        // Calculate the velocity in RPM
        ui16Qei1Rpm = ui32Qei1Vel * VEL_INT_FREQ * 60 / QEI1_PPR;
    }
    
    
    
    
    
    
    
    /* -----------------------          Include Files       --------------------- */
    #include <stdint.h>                         // Library of Standard Integer Types
    #include <stdbool.h>                        // Library of Standard Boolean Types
    #include "inc/tm4c123gh6pm.h"               // Definitions for interrupt and register assignments on Tiva C
    #include "inc/hw_memmap.h"                  // Macros defining the memory map of the Tiva C Series device
    #include "inc/hw_types.h"                   // Defines common types and macros
    #include "inc/hw_gpio.h"                    // Defines Macros for GPIO hardware
    #include "inc/hw_qei.h"                     // Macros used when accessing the QEI hardware
    #include "driverlib/debug.h"                // Macros for assisting debug of the driver library
    #include "driverlib/sysctl.h"               // Defines and macros for System Control API of DriverLib
    #include "driverlib/interrupt.h"            // Defines and macros for NVIC Controller API of DriverLib
    #include "driverlib/gpio.h"                 // Defines and macros for GPIO API of DriverLib
    #include "driverlib/qei.h"                  // Prototypes for the Quadrature Encoder Driver
    #include "driverlib/pin_map.h"              // Mapping of peripherals to pins for all parts
    #include "driverlib/rom.h"                  // Defines and macros for ROM API of driverLib
    
    #define VEL_INT_FREQ    10000               
    #define QEI0_PPR        4000            
    
    /* -----------------------      Global Variables        --------------------- */
    volatile uint32_t ui32Qei0Vel;              
    volatile uint32_t ui32Qei0Pos;              
    volatile int32_t i32Qei0Dir;                
    volatile uint16_t ui16Qei0Rpm;              
    
    /* -----------------------      Function Prototypes     --------------------- */
    void QEI0IntHandler(void);
    unsigned long int a;
    /* -----------------------          Main Program        --------------------- */
    int main(void){
    
    
        // Set the System clock to 80MHz
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 |SYSCTL_USE_PLL |SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
    
        // Enable the clock for peripherals PortD and QEI0
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI0);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    
        // Configure the PC5, PC6 for QEI signals
        ROM_GPIOPinTypeQEI(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);
        ROM_GPIOPinConfigure(GPIO_PD6_PHA0);
        ROM_GPIOPinConfigure(GPIO_PD7_PHB0);
    
        // Configure the QEI1 to increment for both PhA and PhB for quadrature input with "QEI0_PPR" PPR
        ROM_QEIConfigure(QEI0_BASE, QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_QUADRATURE, QEI0_PPR);
        // Configure the QEI1 for Velocity Calculation, Predivide by 1 at "VEL_INT_FREQ" Hz
        ROM_QEIVelocityConfigure(QEI0_BASE, QEI_VELDIV_1, ROM_SysCtlClockGet() / VEL_INT_FREQ);
        ROM_QEIVelocityEnable(QEI0_BASE);
    
        // Enable the Interrupts for Velocity Timer Expiration of QEI0
        void (*QEI0IntHandler_ptr)(void) = &QEI0IntHandler;
        QEIIntRegister(QEI0_BASE, *QEI0IntHandler_ptr);
        ROM_QEIIntEnable(QEI0_BASE, QEI_INTTIMER);
    
        // Master interrupt enable API for all interrupts
        ROM_IntMasterEnable();
    
        ROM_QEIEnable(QEI0_BASE);
    
        while (1);
    }
    
    /* -----------------------      Function Definition     --------------------- */
    void QEI0IntHandler(void)
    {
        // ISR for Quadrature Encoder Interface 1
    
        // Clear the Interrupt that is generated
        ROM_QEIIntClear(QEI0_BASE, ROM_QEIIntStatus(QEI0_BASE, true));
        // Calculate the number of quadrature ticks in "1 / VEL_INT_FREQ" time period
        ui32Qei0Vel = ROM_QEIVelocityGet(QEI0_BASE);
        // Update the position reading of the encoder
        ui32Qei0Pos = ROM_QEIPositionGet(QEI0_BASE);
        // Update the direction reading of the encoder
        i32Qei0Dir = ROM_QEIDirectionGet(QEI0_BASE);
        // Calculate the velocity in RPM
        ui16Qei0Rpm = ui32Qei0Vel * VEL_INT_FREQ * 60 / QEI0_PPR;
    }
    
    

  • with the same code it is not working for QEI0 module....any suggestions..

    The code has the following to configure the QEI0 pins, which contains a mix of references to using ports C and D:

        // Configure the PC5, PC6 for QEI signals
        ROM_GPIOPinTypeQEI(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);
        ROM_GPIOPinConfigure(GPIO_PD6_PHA0);
        ROM_GPIOPinConfigure(GPIO_PD7_PHB0);

    Assuming you want to use PD6 for PhA0 and PD7 for PhB0 then try changing the code to:

        // Configure the PD6, PD7 for QEI signals
        ROM_GPIOPinTypeQEI(GPIO_PORTD_BASE, GPIO_PIN_6 | GPIO_PIN_7);
        ROM_GPIOPinConfigure(GPIO_PD6_PHA0);
        ROM_GPIOPinConfigure(GPIO_PD7_PHB0);

  • can it be problem in the start up file

  • Hello Anuprash,

    Sorry for the delay as I was out on holiday.

    You are using PD7 which is an NMI pin and is locked by default on the device. You will need to unlock the pin to configure it for QEI0 operation.

    Please see the following FAQ for details on how: https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1020814/faq-how-to-get-locked-gpio-pins-on-tm4c123-devices-to-work

    Beyond that, comparing your code aside from what Chester observed with the GPIO_PORTC_BASE being incorrect for your QEI0 GPIO configuration, everything else seems to be in order with the transition. I believe once you add the unlock routine, you will have an operational QEI0 peripheral.

    Best Regards,

    Ralph Jacobi