|
Re: Gear encoder code
A lot of this will probably sound like what John Gutmann said, but here goes. Basically you can envision the GTS as a little switch that triggers when it is 'hit' by a gear/sprocket tooth. Now, on the software side you have to be able to detect this 'hit' and count them. Unfortunately, so many teeth 'hit' the GTS per second that you can't just check for a 1 or a 0 on their port in the normal program loop. Here is where interrupts come in.
Whenever a certain type of event occurs (encoder ticks, serial port data, and timer overflows are some common ones) something called an Interrupt Flag is set. The controller then stops whatever its doing and goes to call the ISR (Interrupt Service Routine) associated with that flag. (ISRs are called from the InterruptHandlerLow() function in user_routines_fast.c in the default code) In the ISR, you do whatever you need to do with that data, (in this case increment a counter) however, the ISR should be very short and fast executing, otherwise the controller will spend all its time servicing interrupts. The interrupt flag is then cleared, and the controller resumes its usual programming.
So, on to the actual issue at hand. To write your own code, you first have to decide which digital IOs you want to use. IOs 1-6 all can interrupt, but 1 & 2 are easiest to use. So what you'd do first is enable the interrupts in the User_Initialization() function. For exactly what that entails, look at Kevin Watson's initialization code for encoders 1&2. Then you'd write your ISR, lets say these are functions called something like GTS_1_ISR() and GTS_2_ISR(). In these functions, you increment a very large variable. Then you'd call them in the appropriate place in InterruptHandlerLow. In this case the appropriate places are where it checks for the "INTCON3bits.INT2IF && INTCON3bits.INT2IE" flags for digital IO 1 and "INTCON3bits.INT3IF && INTCON3bits.INT3IE" for digital IO 2. Check to make sure the interrupt flag is reset somewhere (either in your ISR or in InterruptHandlerLow()). Then it is time to use the counter.
To access the count, you should probably so something like Kevin Watson does and disable the interrupt before you read the count. (Check out his code for how to do this)
Now, for the easy solution, modifying Levin Watson's encoder code. Go to the ISR for encoders 1 & 2 (Encoder_1_Int_Handler, and Encoder_2_Int_Handler, respectively), and replace the if statement with Encoder_X_Count++; where X is the number of the encoder.
__________________
Team 1219: 2009 - Mentor
Team 587: 2005 - Animator, 2006-2008 - Team Captain
|