Well the 16F676 hasn't got a PortB, so you're going to be looking for a long time for an Interrupt on that Port. But this chip has an Interrupt on PortA (Datasheet section 2.2.2.3, 9.4.1 and 9.4.3).
Let's assume in this instance PortA is configured for Input... somewhere at the start when we initialise our interrupts, we first have to clear the interrupt Flag RAIF, so at that time read the contents of PortA to be used for later comparative purposes...
OldPortA=PortA
INTCON=10001000
On Interrupt goto InterruptServiceRoutine
ENABLE
Compare my Register settings above with 2.2.2.3 and with the Interrupt Logic Diagram at Figure 9-10.
Now we go and do whatever we're going to do in our code... when an interrupt happens, in our Interrupt Service Routine (ISR) we first need to know if PortA caused it... if it's the ONLY interrupt configured in our PIC (ie other peripherals like the Timer, Comparators etc are not configured for interrupt), this IF INTCON.0 THEN (below) can be omitted...
DISABLE
If INTCON.0=1 then
We now need to read the content of PortA and determine which pin caused the interrupt...
NewPortA=PortA
If NewPortA.0<>OldPortA.0 then goto PortA0CausedInterrupt
If NewPortA.1<>OldPortA.1 then goto PortA1CausedInterrupt
If etc etc...
and finally when all is done, just prior to exiting our ISR, remember to reset your PortA Interrupt Flag and Read the Port once more...
OldPortA=PortA
INTCON.0=0
RESUME
ENABLE
The description is a little crude just as an example, but you'll get the idea of the kind of things you need to do. If further Interrupts could happen whilst you are in your ISR, then your code needs to be a little more recursive, but that's a topic for another time...
Example in a typical Rover Robot application... when sensors hit an object, the Rover performs an opposite action to clear from the obstruction...
Code:DISABLE InterruptHandler: If PIR1.3=1 then gosub LowBatteryWarning PIR1.3=0 endif If INTCON.0=1 then NewPortA=PortA If NewPortA.2<>OldPortA.2 then gosub MotorForward If NewPortA.3<>OldPortA.3 then gosub MotorBackward If NewPortA.4<>OldPortA.4 then gosub TurnLeft If NewPortA.5<>OldPortA.5 then gosub TurnRight OldPortA=PortA INTCON.0=0 endif RESUME ENABLE




Bookmarks