- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 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
}
}
//-------------------------------------------------------------------------------------------------------------------------------------//
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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