![]() |
Reading an encoder with interrupts
I started with EasyC, and then moved to MPLab using EasyC libraries. Now I am trying to get the same functionality by making the necessary changes to the VexCode source (user_routines.[ch] and user_routines_fast.c). I am trying to count clicks of the shaft encoder using an interrupt and I've been analyzing the user_routines_sensor_test_fast.c code as an example. The comment at the top that "Encoder not ready yet" had me worried but I saw a lot of code associated with it so I read on :) With few comments in the code associated with the encoder, its tough for a beginner to work out the behavior. VexLabs makes it clear that they don't support the code so I thought I would ask and see if anyone here can lend some guidance.
Has anyone here used this example successfuly? Would you mind explaining how the encoder interrupt handler works with the timer2 interrupt handler? Does anyone have another (possibly clearer) example they would like to share? Can someone explain why they would make this assignment: rc_dig_out11 ^= 1; rather than rc_dig_out11 = 1; ? Its a digital output so would there be a difference? I see quite a few uninitialized variables. EncoderDataTickCounter is an example. It is declared and incremented but never initialized. Must not be an issue for some reason. In the interrupt handler, EncoderDataFlag.bit1 is set but I don't see anywhere that it is cleared. The comment says it indicates that data is ready to be read. I will keep working on the problem and report back if I have anything useful for others in the same situation. Thanks for your time, -Glenn |
Re: Reading an encoder with interrupts
Ok, I've been doing more searches through the messages. This time focusing on timers (rather than encoders) and interrupts. Lots of good stuff and the IFI white paper has been useful ( http://www.ifirobotics.com/docs/time...004-jan-14.pdf ).
Answered one question I had. I see that ^= is an exclusive OR assignment rather than OR so it will toggle the value. I'll be spending some time writing some code to exercise the timer interrupt. Hopefully that will shed some light on why it is being used in conjunciton with the encoder interrupt. -Glenn |
Re: Reading an encoder with interrupts
Quote:
|
Re: Reading an encoder with interrupts
Quote:
the timer interrupt occurs when the timer overflows, so that you will know if and how many times that has occurred between encoder interrupts. can't look at the code right now, but will if i have a minute later. |
Re: Reading an encoder with interrupts
Thanks, yes, it does look like the timer is used to measure the interval between interrupts.
I made some good progress last night working with the white paper on using timer interrupts as well as the data sheet for the controller. I first got the timer working and then was able to hook up a switch to the #2 interrupt port and react to it. The switch was REALLY noisy so I ended up coding an example that disables the switch interrupt for a short time after it triggers to let it settle by using the timer interrupt. Tonight I will replace the switch with the encoder. I might hook up the scope to see how noisy the transitions from low/high/low are. In the hopes that this thread might be helpful to someone later doing a search on this subject, here are snippets of my code (use the whitepaper and data sheet mentioned above for context): Add to User_Initialization() in user_routines.c Code:
// Example of how to set up a timer to be handled through interruptCode:
/*** DEFINE MY ROUTINES AND VARIABLES ***/Code:
void InterruptHandlerLow () Code:
/*Code:
void Process_Data_From_Local_IO(void) |
| All times are GMT -5. The time now is 12:39. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi