Pic 16 F 1939 program starts to hang.

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi I have written a quite complex program for temperature control and energy saving.
The problem I have Is this.
Suddenly the Displays light goes off and the program hangs. (display light intensity is controlled by an Interrupt, with a software pwm)
I have enabled watchdog timer. I have only one place where I put MX_Clear watchdog timer.
If I disable this icon then the program keeps resetting as expected so I know the watchdog timer is working.
Now however the program gets stuck and watchdog timer does not reset.
If I cut off power supply and switch on it works again sometimes it can take days until the Program hangs again.
Could there be a Stack overflow problem? Or could it be a hardware problem.
How can I know how many stack levels my program is using?

Best Regards:

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: Pic 16 F 1939 program starts to hang.

Post by Benj »

Hello,

Do you get any warnings when you compile. If you have stack overflows or functions that may collide due to interrupts etc then you usually get a warning highlighting the issue.


I have also seen users doing this before which will eventually fail when the stack falls over.

Main -> Macro1 -> Macro2 -> Macro1 -> Macro2 etc....


Ideally your program should always return back to the main by returning from the macros.

Main -> Macro1 -> Macro2 -> Macro1 -> Main -> Macro1 -> Macro2 ...

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi Benj.
I found some code of concern.
Attached is the code of concern.
This code is about a timer in Hours with two decimals.
When I use the calculation (hours=seconds/36)
So I want to keep the calculation outside the timer interrupt and put it into the main Program.
But it gives me only random Numbers.
If I put the calculation within the timer Interrupt macro. Then it works fine.
Is there any conflict?

Best Regards:
Attachments
Timer.fcf
(16.02 KiB) Downloaded 602 times

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: Pic 16 F 1939 program starts to hang.

Post by medelec35 »

You said you're getting random numbers.
Sounds like watchdog could be resetting?
Can yo try with watchdog disabled and see if results are the same?
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

I don't think Watchdogtimer is resetting, numbers like temperature and other numbers like other counters are not affected.

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: Pic 16 F 1939 program starts to hang.

Post by medelec35 »

Hi Creative25,
I have modified flwochart to display Hours.TenthsOfHours
Do you want to see if it works any better?
Attachments
Timer hours V2.fcf
(14.24 KiB) Downloaded 600 times
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi
Thanks for the modified code.
You basically put all the calculations back into the Timer macro. Except the (dislpay=0)
And then it all works fine. Also I would not want to split the Hour an tenth. Because I want to use that number to calculate average amp usage in real time.
Now I am trying to find out how I could have some calculations outside the timer macro so that it becomes shorter, without having the program become unstable.
Because in my program the timer macro is already long, because I have different timers in there.

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: Pic 16 F 1939 program starts to hang.

Post by medelec35 »

The MOD function within timer macro could cause issues.
So I just removed mod function altogether.
If you specifically require MOD then it should not be within timer interrupt as its using a division function.
Creative25 wrote:Also I would not want to split the Hour an tenth. Because I want to use that number to calculate average amp usage in real time.
I did know that's what you wanted to do.
If you post a flowchart that does all the functions but hangs after a while, I can see if I can modify it to stop hanging.

Martin
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi
Thanks for the info.
So I just learned that you should not use the division function within the timer interrupt.
Could you please point me to some more info what functions I should not use in timer interrupt?
Is division function anything like this (hours=minutes/60) etc.

Best Regards:

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: Pic 16 F 1939 program starts to hang.

Post by medelec35 »

It's a case of you should not use the same function within interrupts as you use in any of call macros including Main.
For example if you use a delay ms in main then any component containing ms delays like LCD should not be used within interrupts.

The MOD function uses division function, hence you can only use MOD within interrupt if division is used anywhere else (which is /).
If you need to use MOD within interrupt then see if you can use shift right as division within call macros instead?
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi
I do not understand fully.
If I use the plus function like (+) In an interrupt I must not use the plus function at all in the main macro?
Or I must not use the plus function for the same variable? Etc.

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: Pic 16 F 1939 program starts to hang.

Post by Benj »

Hello,

The 8-bit PIC compiler is a bit special in that it uses functions to do the more advanced mathematical operations such as floating point. I'm not sure if divide and mod are also in this list. This is essentially what is causing the problem. Operations like + and - should be fine.

For all our other chip packs the compiler is more advanced and is able to do maths without using dedicated functions so this problem is not an issue.

We are working on changing the 8-bit PIC compiler for the next Flowcode version to get around this problem and others.

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi Benj.
So basically it would be best to be safe would be to just use the + and minus in a timer interrupt and do the rest of the calculation all in the main program.
Will it also help to use a different variable outside the interrupt. For example you have the variable "Minutes" in the interrupt. Then put an other variable in the main program and make a calculation (Minutes_out= Minutes) and then in the main program only use the Minutes_out variable except for resetting the minutes variable from within the main program? Or is this not necessary?

Best Regards:

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: Pic 16 F 1939 program starts to hang.

Post by medelec35 »

I have done a quick test.
I added

Code: Select all

Test = Test / 10
within both main and a timer interrupt.
Then compiled to hex.
The results are:

Code: Select all

Building CASM file
Serious Warning: Possible sw stack corruption, function '__div_8_8' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
I have noticed that if the variables are different then there is no stack corruption warning.

However with the mod function if mod even had different variable names yet still got:

Code: Select all

Building CASM file
Serious Warning: Possible sw stack corruption, function '__rem_16_16' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
Memory Usage Report
So if I was you I would develop Flowchart as normal, then just compile to hex.
Have a look at the compiler messages (if closed then select Build, Compiler messages) scroll down to:

Code: Select all

Building CASM file
Just look to see if there is a corruption warning.

Martin
Martin

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Pic 16 F 1939 program starts to hang.

Post by Creative25 »

Hi Benj
Thanks for all the info.
Now I am getting a bit more understanding and I am busy changing the code.
Works great so far.
However I ran into problems without getting any warning in the "Building CASM file"

Just one more question.
The MOD function is a no no in interrupt macros. And others macros on the same time. What about in other macros like in a macro to calculate numbers etc. Can it be in more than one macro as long as they are not in an interrupt or should I move all those calculations into the main macro? I use it in quite a few macros.

Best Regards:

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: Pic 16 F 1939 program starts to hang.

Post by kersing »

You can use MOD in other macros without any issues. Just not in the interrupt macros.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Post Reply