Suppose I detect an error during execution that does not crash the program. Something like a bad return condition from a function. I might want to end the program at that point. I searched the docs for something like Stop or End but the best I could do is Exit. Exit can only get you out of one level of the program (i.e. Exit Sub ) but that does not terminate the program. I see ExitTask but don't get the impression that it will do what I want. If I am NOT running multiple tasks does it end the program?
Any enlightenment will be appreciated.
Vic
Ending a program on error
Ending a program on error
Vic Fraenckel
KC2GUI
windswaytoo ATSIGN gmail DOT com
KC2GUI
windswaytoo ATSIGN gmail DOT com
There are multiple ways to end your program. For example:
1. Use ExitTask (or Exit Sub) on Main if you only have one task
2. Use SetJmp()/LongJmp() to jump to some exit routine - see http://www.zbasic.net/doc/ZBasicSysLib/ ... ib116.html
3. Use some kind of infinite loop such as:
1. Use ExitTask (or Exit Sub) on Main if you only have one task
2. Use SetJmp()/LongJmp() to jump to some exit routine - see http://www.zbasic.net/doc/ZBasicSysLib/ ... ib116.html
3. Use some kind of infinite loop such as:
Code: Select all
Sub Main()
again:
goto again
End Sub
Mike Perks
Wow, this is VERY old topic.
The new requirements forced me to add a lot of safeguarding code into my programs recently. Sensor inputs out of range, logical inconsistencies in data, stuff like that. So, I started looking for a way to unconditionally and immediately exit the program from any point, including non-Main tasks.
The answers in the previous post are rather workarounds than a solution. Having system level Exit() routine would help a lot here.
I understand that "exiting a program" is weird concept for micro-controllers and any possible implementation would in the end do exactly that - loop indefinitely somewhere. However being a system routine it can also do some additional stuff like disabling interrupts, tri-stating pins, going into hardware sleep mode... basically making sure that controller turns into harmless brick that hardly consumes any power, does not output anything and does not react to inputs.
The new requirements forced me to add a lot of safeguarding code into my programs recently. Sensor inputs out of range, logical inconsistencies in data, stuff like that. So, I started looking for a way to unconditionally and immediately exit the program from any point, including non-Main tasks.
The answers in the previous post are rather workarounds than a solution. Having system level Exit() routine would help a lot here.
I understand that "exiting a program" is weird concept for micro-controllers and any possible implementation would in the end do exactly that - loop indefinitely somewhere. However being a system routine it can also do some additional stuff like disabling interrupts, tri-stating pins, going into hardware sleep mode... basically making sure that controller turns into harmless brick that hardly consumes any power, does not output anything and does not react to inputs.
Disabling interrupts and entering sleep mode could be done but changing the state of I/O pins would probably not be a good idea because the "safe" state for a given I/O pin can't be known.wwwoholic wrote:[...] basically making sure that controller turns into harmless brick that hardly consumes any power, does not output anything and does not react to inputs.
Further, it might be a good idea for an application to have an LED or other indicator that conveys the fact that the device has been idled.
It would be simple enough to write a subroutine that disables interrupts and enters sleep mode. That subroutine could also do whatever additional configuration is appropriate for the target application.
Code: Select all
Sub vegetate()
Dim intFlag as Byte
intFlag = DisableInt()
' do other preparation here
Call CPUSleep(&H04)
End Sub
- Don Kinzer
If you wanted to ensure that the vegetate() sub never returns it could be modified thus:
Code: Select all
#pragma warning(8: off)
Sub vegetate()
Dim intFlag as Byte
intFlag = DisableInt()
' do other preparation here
Call CPUSleep(&H04)
Do
Loop
End Sub
#pragma warning(8: default)
- Don Kinzer
Ending a program on error
> ... "safe" pin state for specific application...
Isn't this the same concern of uncontrolled function while in the Reset
state, i.e. all floating pins? If the design tolerates all floating
pins (which, I suggest, it must) can't that be a predictable and safe
state in which to "halt"?
Tom
Isn't this the same concern of uncontrolled function while in the Reset
state, i.e. all floating pins? If the design tolerates all floating
pins (which, I suggest, it must) can't that be a predictable and safe
state in which to "halt"?
Tom
Tom
Yes, Stop is a reserved word, listed in Appendix A of the ZBasic Language Reference Manual. The IDE highlights it because it is in the value list of keywordclass.bas, defined in bas.properties (which can be opened from the Options menu.wwwoholic wrote:IDE highlights "stop" as reserved word
You may edit the keyword list in the bas.properties file but you'll have to remember to do so again when a new version of ZBasic is installed. Alternately, you can copy the definition of keywordclass.bas to your UserOptions file (again, accessible from the Options menu) and edit it there.
- Don Kinzer
Re: Ending a program on error
That is correct.GTBecker wrote:Isn't this the same concern of uncontrolled function while in the Reset state, i.e. all floating pins?
- Don Kinzer