Page 1 of 1

Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 10, 2016 5:14 pm
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:

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 10, 2016 5:50 pm
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 ...

Re: Pic 16 F 1939 program starts to hang.

Posted: Sat Feb 13, 2016 2:30 pm
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:

Re: Pic 16 F 1939 program starts to hang.

Posted: Sat Feb 13, 2016 5:51 pm
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?

Re: Pic 16 F 1939 program starts to hang.

Posted: Sun Feb 14, 2016 6:08 pm
by Creative25
I don't think Watchdogtimer is resetting, numbers like temperature and other numbers like other counters are not affected.

Re: Pic 16 F 1939 program starts to hang.

Posted: Sun Feb 14, 2016 9:21 pm
by medelec35
Hi Creative25,
I have modified flwochart to display Hours.TenthsOfHours
Do you want to see if it works any better?

Re: Pic 16 F 1939 program starts to hang.

Posted: Sun Feb 14, 2016 9:45 pm
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.

Re: Pic 16 F 1939 program starts to hang.

Posted: Sun Feb 14, 2016 9:58 pm
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

Re: Pic 16 F 1939 program starts to hang.

Posted: Mon Feb 15, 2016 9:12 pm
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:

Re: Pic 16 F 1939 program starts to hang.

Posted: Mon Feb 15, 2016 9:29 pm
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?

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 7:18 am
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.

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 11:31 am
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.

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 12:50 pm
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:

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 2:17 pm
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

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 7:14 pm
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:

Re: Pic 16 F 1939 program starts to hang.

Posted: Wed Feb 17, 2016 8:52 pm
by kersing
You can use MOD in other macros without any issues. Just not in the interrupt macros.