|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Encoder Help with VEX and MPLAB
Greetings,
I am trying to set up some reference encoder for my team. We are using MPLAB with Vex Controller. Is there a way that I can get the interrupt to be triggered on the raising or falling edge of the resulting encoder square wave? Right now my code (below) just looks to see if the signal is high (0) or low (1), but if the encoder happens to stop when the signal is high , the encoder count just runs. Code:
if(rc_dig_int1)
{
encoder1++;
PORTBbits.RB2=0; //Set Interrupt = 0
}
|
|
#2
|
|||
|
|||
|
Re: Encoder Help with VEX and MPLAB
Kevin Waston provides some modular code for setting up encoders on the IFI hardware. The EDU hardware is closest to Vex, so I'd download that workspace. Any of the code can be easily ported to work with Vex, so I guess it doesnt matter.
That code will work as long as rc_dig_int1 is defined as the interrupt flag you want to look at and it is placed in the interrupt handler found in user_routines_fast.c. I'm guessing you are looking directly at the input pin, which is causing the code inside the If to execute every time. Last edited by Tom Bottiglieri : 10-03-2007 at 01:47. |
|
#3
|
||||
|
||||
|
Re: Encoder Help with VEX and MPLAB
The VEX encoders send a binary string. You should only increment distance when the latest value is different from the previous value.
Code:
unsigned int distance = 0; char encOld = 0; . . . if((char)rc_dig_int1 != encOld) distance++; encOld = rc_dig_int1; |
|
#4
|
|||
|
|||
|
Re: Encoder Help with VEX and MPLAB
Thank you that worked great
|
|
#5
|
||||
|
||||
|
Re: Encoder Help with VEX and MPLAB
Quote:
To get the encoder values, just look at the two globals. It would be better to wrap that lookup in code that disable interrupts, so you don't risk having the value change in the middle of reading it. I can post that bit of code later, but I need to run right now. user_routines.c --------------- in the function "User_Initialization" add this line: Code:
EnableInterrupts(); --------------- in the Function Prototypes section, add these lines: Code:
extern volatile int g_distMotorLeft; extern volatile int g_distMotorRight; void EnableInterrupts(void); -------------------- below this line: /*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/ add these lines: Code:
volatile int g_distMotorLeft = 0; volatile int g_distMotorRight = 0; Code:
void InterruptHandlerLow ()
{
unsigned char int_byte;
if (INTCON3bits.INT2IF && INTCON3bits.INT2IE) /* The INT2 pin is RB2/DIG I/O 1. */
{
INTCON3bits.INT2IF = 0;
++g_distMotorRight;
}
else if (INTCON3bits.INT3IF && INTCON3bits.INT3IE) /* The INT3 pin is RB3/DIG I/O 2. */
{
INTCON3bits.INT3IF = 0;
++g_distMotorLeft;
}
else if (INTCONbits.RBIF && INTCONbits.RBIE) /* DIG I/O 3-6 (RB4, RB5, RB6, or RB7) changed. */
{
int_byte = PORTB; /* You must read or write to PORTB */
INTCONbits.RBIF = 0; /* and clear the interrupt flag */
} /* to clear the interrupt condition. */
}
void ResetEncoders()
{
g_distMotorLeft = 0;
g_distMotorRight = 0;
}
void EnableInterrupts()
{
// interrupt 2, which is pin 1
TRISBbits.TRISB2 = 1; // make sure pin is configured as an input
INTCON3bits.INT2IP = 0; // interrupt is low priority
INTCON2bits.INTEDG2 = 1; // trigger on rising edge
INTCON3bits.INT2IF = 0; // make sure interrupt flag is clear before enabling
INTCON3bits.INT2IE = 1; // enable the interrupt
// interrupt 3, which is pin 2
TRISBbits.TRISB3 = 1; // make sure pin is configured as an input
INTCON2bits.INT3IP = 0; // interrupt is low priority
INTCON2bits.INTEDG3 = 1; // trigger on rising edge
INTCON3bits.INT3IF = 0; // make sure interrupt flag is clear before enabling
INTCON3bits.INT3IE = 1; // enable the interrupt
}
|
|
#6
|
|||
|
|||
|
Re: Encoder Help with VEX and MPLAB
How would do you make encoders work with interrupt pins 3-6?
I plan on allowing the code to utilize 4 encoders and 2 ultra-sonics. Thanks in advance |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with MPLAB | burningpig | Programming | 9 | 17-01-2007 13:10 |
| Kevin Watson and programming VEX in MPLAB | Doug Leppard | Programming | 4 | 24-08-2006 21:05 |
| Vex Programming with MPLAB and IF loader | Joohoo | Programming | 3 | 27-07-2006 18:18 |
| VEX UltraSonic and Encoder SOLD OUT... | Chris_Elston | Electrical | 5 | 09-01-2006 13:22 |
| Need Help with Encoder - Won't Count Clicks | Kingofl337 | Programming | 5 | 16-02-2005 18:30 |