6 Replies Latest reply on Oct 20, 2020 11:30 PM by YatheeshK_36

    Problems with reading data from rotary encoder via port B of FX2LP

    adru_4655986

      Hello, I am trying to implement a rotary encoder using Port B of FX2LP. The data from the rotary encoder is to be read and a counter will increase or decrease depending upon the direction of rotation. I have been unable to read any data from the port i/o pins. Please find the code in the attachment .

       

      Any guidance on the matter would be greatly appreciated !!  Thank you!

       

      /************************************************************************************************/

      #define TURN_ON_PUSHBUTTON       PB0  //Encoder button

       

      #define ENCODER_Output_A                PB1  //Encoder Output Pin A

      #define ENCODER_Output_B                PB2  //Encoder Output Pin B

       

      //-------------------------------------------------------------------------------------------------------------------------------------//

      BOOL AP338XProcessVendorRequest(BYTE VR)

      {

          switch(VR)

          {

              case VR_RESET_ALL:

                  EncoderInit();

                  break;


              case VR_READ_Enocoder:

                  EP0BUF[0] = ReadEncoder(&EP0BUF[0]);

                  EP0BCH = 0x00;

                  EP0BCL = 0x02;

                  break;             


              default:

                  return(TRUE);

          }

          return(FALSE);

      }

       

       

      //--------------------Called repeatedly while the device is idle---------------------------//

      void Encoder_Poll()        //this function is called from TD_poll()

      {

        EncoderButtonState();

      }

       

       

       

      //-------------------------------------------------------------------------------------------------------------------------------------//

      void EncoderInit()        //this function is called from TD_Init()

      {

          unsigned char i;


          // Port B[7..0]                                  

          // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |

          // | X | X | X | X | X | I | I | I |

          OEB = 0x00;            //Port B set to input (rotary encoder 3 pins:Output B/Output A/switch)


          // Port D[7..0]                                 

          // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |

          // | X | X | X | X | I | X | X | X |

          OED = 0x10;                                            //For the LED status 


              //Enabled only EP6

              EP1OUTCFG &= 0xFF;

              SYNCDELAY;

              EP1INCFG &= 0xEF;

              SYNCDELAY;

              EP2CFG &= 0x7F;

              SYNCDELAY;

              EP4CFG &= 0x7F;

              SYNCDELAY;

              EP6CFG &= 0xE0;

              SYNCDELAY;

              EP8CFG &= 0x7F;

              SYNCDELAY;

             

          // EP6 is quad buffered.

          for(i = 0; i < 4; i++)

          {

              EP6BCH = 0x00;

              SYNCDELAY;

              EP6BCL = 0x00;

              SYNCDELAY;                 

          }

          // EP0 is double buffered.

          for(i = 0; i < 2; i++)

          {

              EP0BCH = 0x00;

              SYNCDELAY;

              EP0BCL = 0x00;

              SYNCDELAY;                 

          }

      }

      //------------------------------------------------------------------------------------------------------------------------------------------//

      void ReadEncoder(unsigned char *counter)

      {

           //  BYTE pinData;

           //   pinData = IOB;                 // Reading the Port B pins


            unsigned char aState,aLastState;   

            aState = ENCODER_Output_A; // Reads the "current" state of the outputA     

          

           // If the previous and the current state of the outputA are different, that means a Pulse has occured

           if (aState != aLastState)

           {    

               // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise

             if (ENCODER_Output_B != aState) {

                counter ++;

              }

           else {

                    counter --;

                   }

             }

         aLastState = aState; // Updates the previous state of the outputA with the current state

      }

      //-------------------------------------------------------------------------------------------------------------------------------------//

      void EncoderButtonState()

      {

          if(TURN_ON_PUSHBUTTON==1) //Encoder push button status

          {

             IOD|=0x10;             //turn on led

             EZUSB_Delay(1000);

          }

          else

          {

             IOD=0x00;             //turn off led

          }

      }

      //-------------------------------------------------------------------------------------------------------------------------------------//