Log in

View Full Version : Easy interrupt setup


steven114
01-02-2004, 18:02
I made this code to ease using interrupts - I spent most of a day reading documentation and testing things, so I figured that I'd share the code.

RobotUtils.h
/* RobotUtils.h *\
|* Interface with the more *|
|* advanced controller features *|
|* Created 2004 by *|
\* Steven Schlansker */

#include "ifi_picdefs.h"
#include "printf_lib.h"

typedef void (*int_func)(void);


typedef enum { falling, rising } edge;

void interrupt_pin1(int_func,edge which);
void interrupt_pin2(int_func,edge which);
void interrupt1();
void interrupt2();

RobotUtils.c
#include "RobotUtils.h"
#define bitbyte(c) (1 << c)

int_func pin1,pin2;

void interrupt_pin1(int_func func,edge which){
pin1 = func;
if(func){
INTCON |= bitbyte(7);
if(which == rising)
INTCON2 |= bitbyte(4);
else
INTCON2 &= ~bitbyte(4);
INTCON3 &= ~bitbyte(7);
INTCON3 |= bitbyte(4);
}else{
INTCON3 &= ~bitbyte(4);
}
}

void interrupt_pin2(int_func func,edge which){
pin2 = func;
if(func){
INTCON |= bitbyte(7);
if(which == rising)
INTCON2 |= bitbyte(3);
else
INTCON2 &= ~bitbyte(3);
INTCON2 &= ~bitbyte(1);
INTCON3 |= bitbyte(5);
}else{
INTCON3 &= ~bitbyte(5);
}
}

void interrupt1(){ pin1(); }
void interrupt2(){ pin2(); }

user_routines_fast.c (Only modify the lines which are shown here)
void InterruptHandlerLow ()
{
unsigned char int_byte;
if (INTCON3bits.INT2IF) /* The INT2 pin is RB2/DIG I/O 1. */
{
interrupt1();
INTCON3bits.INT2IF = 0;
}
else if (INTCON3bits.INT3IF) /* The INT3 pin is RB3/DIG I/O 2. */
{
interrupt2();
INTCON3bits.INT3IF = 0;
}
else if (INTCONbits.RBIF) /* 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. */
}

Good luck.

Ryan M.
03-02-2004, 10:41
Thanks. :D

SeanCassidy
03-02-2004, 19:30
Glad you posted this, but could you clairify what exactly RoboUtils.c does? It has a lot of bitshiffting and other bitwise operations which are hard to follow. Maybe a few comments? :)

steven114
04-02-2004, 00:07
Yeah, it changes a few registers around, specifically the INTCON registers. For a description of the bits, refer to the Microchip documentation on the PIC. I might write comments eventually if I get a chance...