Pic 16 F 1939 program starts to hang.
Moderator: Benj
-
- 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.
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:
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:
- 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.
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 ...
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 ...
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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.
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:
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 621 times
-
- 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.
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?
Sounds like watchdog could be resetting?
Can yo try with watchdog disabled and see if results are the same?
Martin
-
- 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.
I don't think Watchdogtimer is resetting, numbers like temperature and other numbers like other counters are not affected.
-
- 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.
Hi Creative25,
I have modified flwochart to display Hours.TenthsOfHours
Do you want to see if it works any better?
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 607 times
Martin
-
- 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.
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.
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.
-
- 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.
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.
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
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.
I did know that's what you wanted to do.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.
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
-
- 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.
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:
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:
-
- 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.
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?
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
-
- 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.
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.
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.
- 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.
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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.
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:
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:
-
- 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.
I have done a quick test.
I added within both main and a timer interrupt.
Then compiled to hex.
The results are:
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:
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:
Just look to see if there is a corruption warning.
Martin
I added
Code: Select all
Test = Test / 10
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)
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
Have a look at the compiler messages (if closed then select Build, Compiler messages) scroll down to:
Code: Select all
Building CASM file
Martin
Martin
-
- 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.
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:
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:
-
- 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.
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
― C.S. Lewis