5
« on: June 07, 2011, 01:45:12 PM »
I am using a pic for modbus communications and everything works except the mpg. I feed the tickcount and mpg1 count with the following interrupt code:
defs
INTCON2bits.INTEDG1 = 0; //Interrupt on falling edge (INT1)
INTCON3bits.INT1IP = 1; //INT1 External Interrupt Priority bit (Set to High)
INTCON3bits.INT1IF = 0; //Clear interrupt flag bit
INTCON3bits.INT1IE = 1; //Enable the INT1 external interrupt
INTCON2bits.INTEDG2 = 0; //Interrupt on falling edge (INT2)
INTCON3bits.INT2IP = 1; //INT2 External Interrupt Priority bit (Set to High)
INTCON3bits.INT2IF = 0; //Clear interrupt flag bit
INTCON3bits.INT2IE = 1; //Enable the INT2 external interrupt
RCONbits.IPEN = 1; //Enable priority levels on interrupts
INTCONbits.GIEH = 1; // Enables all unmasked interrupts
INTCONbits.GIEL = 1; // Enables all unmasked peripheral interrupts
then int code itself
void
handle_high_isr(
void
) {
if (INTCON3bits.INT1IF)
{
INTCON3bits.INT1IE = 0;
mpg_count++;
mpg_tickcount = (unsigned char) ReadTimer0();
WriteTimer0( 0 );
INTCON3bits.INT1IF = 0; // clear int flag
INTCON3bits.INT1IE = 1;
}
if (INTCON3bits.INT2IF)
{
INTCON3bits.INT2IE = 0;
mpg_count--;
mpg_tickcount = (unsigned char) ReadTimer0();
WriteTimer0( 0 );
INTCON3bits.INT2IF = 0; // clear int flag
INTCON3bits.INT2IE = 1;
}
// Handle any pending USB tasks
USBDeviceTasks();
}
If I spin the mpg quickly it always just registers the last movement + or -. If I move it slowly it appears to register movement at random. It is always in the right direction but may not register no matter how slow.
Any ideas?