Page 1 of 1

Explanation in detail of Port B Interrupts.

Posted: Wed Aug 10, 2011 2:42 pm
by daveb0360
Hi,
Can anyone please provide an explanation of how port B interrupts are used.
For example, if port B is set as interrupt port, does this encompass all bits of the port or can 2 bits of port be used as interrupts and others used normally?

Sorry if this sounds dumb but I really am new to this programming world and am hampered in my design at the moment by routines being delayed in loops and not responding to buttons etc. I need a way to react to triggers at any point of program execution to implement an emergency stop routine etc.

Thanks in advance.

dave

Re: Explanation in detail of Port B Interrupts.

Posted: Wed Aug 10, 2011 3:23 pm
by Benj
Hi Dave,

It really depends on the device.

Early PICs used the PORT interrupt for the top half of port b.

Some devices use the entire port b register.

Others allow for single/multiple ports and may have maskable pins.

For devices without maskable pins you can mask in software by retaining a copy of what the register looked like last time an interrupt was fired.

Re: Explanation in detail of Port B Interrupts.

Posted: Wed Aug 10, 2011 3:40 pm
by daveb0360
Hi again Ben,
I will use 16F876 or 913,
I am lost by your explanation re: "mask in software by retaining a copy of what the register looked like last time an interrupt was fired"; can you point to any examples on the forum as I can't find anything that makes sense to me.

Cheers
Dave

Re: Explanation in detail of Port B Interrupts.

Posted: Thu Aug 11, 2011 10:42 am
by fotios
Hi Dave
May i reply instead Ben? Moreover Ben is my mentor on this issue. The PORT B interrupt (or IOC = Interrupt On Change state according to Microchip) is available from RB.4 - RB.7 regarding your micros P16F876 and 913. In new generation PICs, like P16F887, it is available on all pins of PORT B, from RB.0 up to RB.7. When you enable the PORT interrupt in FlowCode, by default is applicable on all pins of PORT B. You can exclude some pins from this interrupt by masking them. But this can been done only by using a C code icon. For example, if you want to apply interrupt only in RB.6 and RB.7 pins you have to put a "C code" icon exactly after the "Enable PORT B" icon and to write this command:
iocb = 0b11000000;
In this way, b7 and b6 pins of Port B are masked and used as interrupt sources, while all other pins are excluded and can be used for other tasks. Please don't forget to delete everything is written inside the "C code" icon when you open it for first time, to place the command in the first line and the ";" at the end of command.
Regards
Fotis

Re: Explanation in detail of Port B Interrupts.

Posted: Thu Aug 11, 2011 10:51 am
by Benj
Hi Dave,

Thanks for posting Fotios :D

The 16F876 does not have the iocb register, the 913 does so you can use Fotios' example c code for this.

To mask the port interrupt using software you can do the following in the interrupt macro when a port interrupt is triggered.

//store variable
tempvar = input: portb

//mask off unwanted pins, here we are selecting the top 2 bits of the port
calc: tempvar = tempvar & 0xC0

//check to see if the wanted pins have changed since the last interrupt
decision: tempvar != oldtempvar
yes: masked pin has changed
no: non masked pin has changed

//At the end of the macro store the value for the next iteration
calc: oldtempvar = tempvar

Re: Explanation in detail of Port B Interrupts.

Posted: Thu Aug 11, 2011 12:33 pm
by daveb0360
Thanks guys,
I am really starting to understand. As it happens, I only need two interrupts so the 876 will suffice and I have a few in stock to play with.

This is the type of feedback we need (by we, I mean us newbies and code phobics) I am now progressing.....I wondered what 'IOC' meant, saw it used in numerous posts..


Thanks muchly.
Dave

Re: Explanation in detail of Port B Interrupts.

Posted: Thu Aug 11, 2011 9:24 pm
by fotios
Hi Dave
Here are two FlowCode examples for helping you. You can run both on simulator. In the first example "DAVE_INT" i have arranged the program according to Ben's instructions. In the second example "DAVE" you can see a different method that i use many times instead interrupts when strange things hapen (mainly stack overflow using PIC16Fxxx micros which have small memory) in program execution using them. Interrupts are not necessary in all cases, you can use sub-loops like this in "DAVE".
First of all, we need a scenario for the actual hardware: The two momentary switches "SENSOR1" and "SENSOR2" represent two external devices, say two limit switches that can activate two relays. The two relays are represented by the two LED "D0" and "D1". Try both examples in simulator to see the difference in operation. "DAVE_INT" works exactly like an interrupt, you have to release the switch to enable again the interrupt. In "DAVE", if the switch is firmly pressed the corresponding output remains ON untill we release the switch.
Good luck.
Fotis

Re: Explanation in detail of Port B Interrupts.

Posted: Fri Aug 12, 2011 9:33 pm
by daveb0360
Thanks again for all this assistance. Am busy putting all the differing elements together. A little bewildered at the moment but it's all starting to make sense.



Many Thanks guys