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

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
aditya_r07
Level 2
Level 2
10 sign-ins 5 replies posted 5 sign-ins

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

    }

}

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

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

Hello Yatheesh,

I was unable to extract the .zip file you sent me, but was able to read the encoder values ​​using the following method and it works fine. So, Is there any way to monitor serial output(like an Arduino IDE)  without sending the vendor request.

Please find the attachment.

Thanks,

Aditya R

View solution in original post

0 Likes
8 Replies
YatheeshD_36
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello,

The below lines are not required:

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

    {

        EP0BCH = 0x00;

        SYNCDELAY;

        EP0BCL = 0x00;

        SYNCDELAY;                

    }

BOOL DR_VendorCmnd(void) function will handle all the vendor commands as it is registered in the fw.c file.

pastedImage_5.png

Can you please let me know if the function AP338XProcessVendorRequest() as per your code is present in the fw.c file?

Also, what is the output in the control center when the vendor command VR_READ_Enocoder is sent? please attach a snippet.

Thanks,

Yatheesh

0 Likes
lock attach
Attachments are accessible only for community members.

Hello,

Thank you for replay

I have tried every possible ways but I was not able to send encoder reading to the host through endpoint buffer.

Please find attachment.

This is what I got on Cyconsole whenever I was trying to read value from the encoder.

Encoder_reading.png

0 Likes
lock attach
Attachments are accessible only for community members.

Hello,

I see from the firmware that for ReadEncoder the vendor command is 0xB3. Can you please confirm if you are using this vendor command (0xB3) or 0xB4 to read data from the Encoder?

Can you try to just increment the values in the ReadEncoder() function every time it is called and check if it is working fine?

Also try the project attached in this response.

Password: cypress

Thanks,

Yatheesh

0 Likes

Hello,

I tried the zip file that you sent.the response is in the picture attached below. On rotating the rotary button i am able to see a change in the voltage levels as is expected. However i get 00 as a response on the cyconsole.test2.png

0 Likes

I noticed that another problem arises when I poll the ReadEncoder() function and after uploading the generated .icc into the Cypress.

I couldn't able to detect my Cypress card.

0 Likes
lock attach
Attachments are accessible only for community members.

Hello,

Pointer was not handled properly.

Please try the attached project.

Password: Cypress@1234

Thanks,

Yatheesh

0 Likes
lock attach
Attachments are accessible only for community members.

Hello Yatheesh,

I was unable to extract the .zip file you sent me, but was able to read the encoder values ​​using the following method and it works fine. So, Is there any way to monitor serial output(like an Arduino IDE)  without sending the vendor request.

Please find the attachment.

Thanks,

Aditya R

0 Likes

Hello Aditya,

1. you can regularly monitor it in the TD_poll(). In this case, make sure that the I2C communication does not get stuck in case you have infinite wait time for the slave to respond.

2. you can use the FX2LP external interrupts to trigger the action and then monitor it.

Thanks,

Yatheesh