Flowcode v4 port interrupts help

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Flowcode v4 port interrupts help

Post by gnoe »

Hello, this is my first time posting here, I'm a student and we've been given a project for flowcode which counts for a good part of our grades, now I've been working on this quite hard but I'm nowhere near a programming expert..

The problem I've been having is the following; I have been trying to use interrupt function but we pretty much had to find out ourselves (by research forums etc) how these work.
Now I've gotten them to work the problem is, it only does the interrupt once. What I want it to do is for example program is running and I press either B6 or B4 the program needs to be interrupted but NOT go back to where it was afterwards, I solved this by calling a different macro in the interrupt macro itself which pretty much does the exact same as the main program only from scratch again.
now this is where the problems arise, when I get back into this so called new main program the interrupt no longer functions at all, it just does nothing anymore, anyone have any idea how to solve this?
I can try to post my program if that helps.

Thanks in advance!

ps I tried posting this in the flowcode v4 forum but it wouldn't let me, I do have a registered version of it though (through my school)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Flowcode v4 port interrupts help

Post by kersing »

Hi,

On small PIC microcontrollers only one interrupt can be active at a time. As you are not returning from that interrupt new interrupts will be ignored. A solution for your problem would be to set a variable to a predefined value (a flag) in the interrupt routine and check for that value in the main program. If the value is found in the main routine you start executing the code that should be executed once the interrupt occurred. Do not forget to reset the variable so a new interrupt will be able to set the value.

The general rules of thumb for interrupt routines:
- keep processing as short as possible to prevent missing the next interrupt generated,
- don't call other routines (macros) or at least keep the number of calls to the absolute minimum,
- always return from the interrupt.

Hope this helps.

Good luck,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

kersing wrote:Hi,

On small PIC microcontrollers only one interrupt can be active at a time. As you are not returning from that interrupt new interrupts will be ignored. A solution for your problem would be to set a variable to a predefined value (a flag) in the interrupt routine and check for that value in the main program. If the value is found in the main routine you start executing the code that should be executed once the interrupt occurred. Do not forget to reset the variable so a new interrupt will be able to set the value.

The general rules of thumb for interrupt routines:
- keep processing as short as possible to prevent missing the next interrupt generated,
- don't call other routines (macros) or at least keep the number of calls to the absolute minimum,
- always return from the interrupt.

Hope this helps.

Good luck,

Jac
well the problem is, it's a traffic light situation and for example, I interrupt because a cyclist pressed a button at one of the traffic lights, now the interrupt should occur and all traffic lights should go red while the cyclist's light goes green. if I then return from the interrupt... it will start from where it left off possibly making a mess out of things for example I'd go from a red light to orange stead of green etc... I have no idea how to return out of my interrupt macro without this possibly happening :( maybe I should try achieving it without interrupts, I just don't know how.

Thanks for taking the time to answer though

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Flowcode v4 port interrupts help

Post by medelec35 »

The reason why interrupt only works once is because if an interrupt is triggered then all interrupts are disabled including portB until the interrupt macro is exited correctly at the bottom of the interrupt macro then continue running from the next address down (obtained from the stack) from where interrupt was started from.
So the problem you will have is: Since you have called another macro from within your macro so Global interrupt enable (GIE = bit 0 of intcon) will stay disabled since the macro did not leave at the correct exit so to speak. I don't believe its as easy as re-enabling GIE and resetting portB interrupt Flag (RBIF) since the stack will be out and corruption will occur.

Why would you not want the interrupt to continue form the place it was interrupted from?

Martin

Edit: just noticed there has already been a new post since I first started typing this reply, so sorry in advance if I am repeating any of what Jac has said.
Martin

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

medelec35 wrote: Why would you not want the interrupt to continue form the place it was interrupted from?

Martin

Edit: just noticed there has already been a new post since I first started typing this reply, so sorry in advance if I am repeating any of what Jac has said.
I have tried to answer that question as best as I could in the post above, but I see I will have to exit the interrupt regardless then, i'll try to find a way around it then

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

kersing wrote:Hi,

On small PIC microcontrollers only one interrupt can be active at a time. As you are not returning from that interrupt new interrupts will be ignored. A solution for your problem would be to set a variable to a predefined value (a flag) in the interrupt routine and check for that value in the main program. If the value is found in the main routine you start executing the code that should be executed once the interrupt occurred. Do not forget to reset the variable so a new interrupt will be able to set the value.

Jac
I didn't first understand this solution but I think I know how to apply it now! Thanks a bunch I hope it will work and will update here if it does!! =)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Flowcode v4 port interrupts help

Post by kersing »

gnoe wrote: well the problem is, it's a traffic light situation and for example, I interrupt because a cyclist pressed a button at one of the traffic lights, now the interrupt should occur and all traffic lights should go red while the cyclist's light goes green. if I then return from the interrupt... it will start from where it left off possibly making a mess out of things for example I'd go from a red light to orange stead of green etc... I have no idea how to return out of my interrupt macro without this possibly happening :( maybe I should try achieving it without interrupts, I just don't know how.

Thanks for taking the time to answer though
What about logic like this:

Main routine:

Code: Select all

Start, all lights red.
intOccurred = 0
Light A green
count = 30
loop
     delay 1 second
     if (intOccurred <> 0) then
           Light A orange
           delay some seconds
           Light A red
           Lights for cyclists green, delay, cyclists orange, delay, cyclists red (could be a macro)
           intOccurred = 0
           Light A green
     endif
     count = count - 1
until (count == 0)
Light A orange
delay
Light A red

Light B green
count = 10
loop
... (see code above)
until (count == 0)

Light C green
etc etc
In the interrupt:

Code: Select all

intOccurred = 1
This is just a simple example, improvements would be to check intOccurred before entering the loop. Not switching the original light back to green if the remaining count is below a threshold. Another decision could be not to handle the button press before the next light should switch to green. (Motorists would become annoyed if every arriving cyclist would immediately interrupt the normal flow, cyclists would love the priority)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Flowcode v4 port interrupts help

Post by medelec35 »

gnoe wrote: I have tried to answer that question as best as I could in the post above, but I see I will have to exit the interrupt regardless then, i'll try to find a way around it then
No worries bud, you posted your reply so quick after Jacs, I did not even see it lol.
It was only after I posted reply I have seen you have answered it.

I must improve my typing speed, or it could by my age and I just can't keep up :lol:
Martin

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

medelec35 wrote:
gnoe wrote: I have tried to answer that question as best as I could in the post above, but I see I will have to exit the interrupt regardless then, i'll try to find a way around it then
No worries bud, you posted your reply so quick after Jacs, I did not even see it lol.
It was only after I posted reply I have seen you have answered it.

I must improve my typing speed, or it could by my age and I just can't keep up :lol:
My generation grew up with a computer in their lap so lightning speed is pretty much a given! yet I still don't have nowhere near the programming knowledge of your generation ;) then again I still have time to learn!

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

ok the idea jac posted seems to have worked but I encountered a problem and I have no idea what's causing it, i'll upload my program cause you probably won't get what I mean otherwise (don't mock my noob program please ;) ) anyway everything seems to work fine except after I have pressed B6 to stop everything goes great and it gets stuck in the so called loop (until start=1 which is set to b7) now problem is when I actually press start (b7) it jumps to the interrupt again for some reason?? I have set the interrupt to port and then in the interrupt macro put an input with mask and only put the little "v" checks on bit 4 and 6 or am I misunderstanding the way that works?

edit: I will only leave program up here for a shortwhile I don't want any fellow students being copycats =)

EDIT2: Apparently when I press B4 a fault occurs aswell, it jumps into the interrupt macro but upon going out of it, it immediatly jumps back into the interrupt macro takes the no side of the decision and the program gets stuck in the loop it should be in when I press b6 =/ no idea why it jumps into the interrupt macro a second time

EDIT3: Removed program, I have worked out a way to solve all the above issues! Thanks to all of you for the quick aid and thanks jac for giving me the idea for a solution to my problem :D

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

I have an additional question and it would be pointless to open a new thread since it's regarding the same matter pretty much.

Currently if I press one of the buttons to enter my interrupt macro it won't work if my program is currently in the process of going through a delay so for example like my green light has to stay green for X seconds when these X seconds are running and I press the button for interrupt, nothing will happen if I press it at any other moment when no delay is running it will work the way it should, is there any solution to this or is this just the way it works? since it's not a life or death issue really.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Flowcode v4 port interrupts help

Post by Benj »

Hello,

Is the delay currently part of your interrupt macro? If it is then you cannot re-enter a interrupt macro whilst inside.

One way to get around this would be to set a flag in your interrupt macro and then drop back into your main. Inside the main you monitor the flag and if it is set then you start processing your delays. This way you can always go back into the interrupt macro and set a breakout flag to stop/restart the delays process etc.

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

Benj wrote:Hello,

Is the delay currently part of your interrupt macro? If it is then you cannot re-enter a interrupt macro whilst inside.

One way to get around this would be to set a flag in your interrupt macro and then drop back into your main. Inside the main you monitor the flag and if it is set then you start processing your delays. This way you can always go back into the interrupt macro and set a breakout flag to stop/restart the delays process etc.
well I don't want to stop the delays process, it's just that if I press the button to stop my program for example, it normally goes into the interrupt macro sets a variable and once the current traffic light has run its course it will stop the program. but if I press that same stop button when a delay is running it will not go into the interrupt macro at all. so is it same to assume that no actions are possible when a delay is running it's course?

gnoe
Posts: 9
Joined: Sat Feb 25, 2012 5:29 pm
Has thanked: 2 times
Been thanked: 2 times
Contact:

Re: Flowcode v4 port interrupts help

Post by gnoe »

Found my own solution for the last problem, instead of using a 10 second delay for example I now use a 100 ms one in a loop that repeats 100 times. requiring me to press the stop button at the most for 100 ms which is acceptable

Post Reply