Getting NAP to work
mph
Posts: 13
I am using the BS2px, and I have a 2x16 LCD as well as a 4x5 keypad (via a 74C923N decoder) hooked up to P0 through P7. Displaying data on the LCD and accepting inputs from the keypad are working just fine and very stably.
I was now trying to implement a STANDBY function into my program to save power. The idea is that when I press a specific key on the keypad (I call it #72, the STANDBY key) the program would GOSUB to the NapRoutine shown below:
NapRoutine:
NapLoop:
NAP 6
DIRL=%00000000 ' make PINs 0-7 inputs for keypad reading
BUTTON 2,1,255,255,Buttn,0,NapLoop ' Check if keypad button was pressed (data ready)
Char = INL & %11111000 ' read PINs 0-7, only consider high 5 bits (74C923 outputs)
' bitmap of the 4Wx5H keypad from top left key to bottom right key
LOOKDOWN Char,[noparse][[/noparse]%11000000,%01000000,%10000000,%00000000,
%11100000,%01100000,%10100000,%00100000,
%11010000,%01010000,%10010000,%00010000,
%11110000,%01110000,%10110000,%00110000,
%11001000,%01001000,%10001000,%00001000],Char
'assign values to the respective keys
LOOKUP Char,[noparse][[/noparse]65,66,67,68,55,56,57,69,52,53,54,70,49,50,51,71,48,46,13,72],Char
IF Char<>72 THEN GOTO NapLoop
RETURN
This is a modified version of my well-tested routine I use to read keys from the keypad. The main difference is that there is a NAP command in the loop. The BS2px goes NAP for 6 units, then uses the BUTTON command to check if a key was pressed on the keypad, and if not it NAPs again. If a key was pressed it figures out which one, and unless key #72 was pressed it goes NAP again, otherwise it considers itself to be awake again and returns to the program.
The above sub-routine works perfectly for short times (less than 1 minute or so). The BS2px goes NAP and only wakes up if I press the #72 key on the keypad; then it wakes up again. However, if I let the program loop in this routine for a longer time (a minute or more or so) without pressing a key or doing anything to it, the program crashes and becomes completely unresponsive. It looks like it gets hung up in the NAP loop somehow.
Does anyone see what might be going on?? Thanks!
mph
I was now trying to implement a STANDBY function into my program to save power. The idea is that when I press a specific key on the keypad (I call it #72, the STANDBY key) the program would GOSUB to the NapRoutine shown below:
NapRoutine:
NapLoop:
NAP 6
DIRL=%00000000 ' make PINs 0-7 inputs for keypad reading
BUTTON 2,1,255,255,Buttn,0,NapLoop ' Check if keypad button was pressed (data ready)
Char = INL & %11111000 ' read PINs 0-7, only consider high 5 bits (74C923 outputs)
' bitmap of the 4Wx5H keypad from top left key to bottom right key
LOOKDOWN Char,[noparse][[/noparse]%11000000,%01000000,%10000000,%00000000,
%11100000,%01100000,%10100000,%00100000,
%11010000,%01010000,%10010000,%00010000,
%11110000,%01110000,%10110000,%00110000,
%11001000,%01001000,%10001000,%00001000],Char
'assign values to the respective keys
LOOKUP Char,[noparse][[/noparse]65,66,67,68,55,56,57,69,52,53,54,70,49,50,51,71,48,46,13,72],Char
IF Char<>72 THEN GOTO NapLoop
RETURN
This is a modified version of my well-tested routine I use to read keys from the keypad. The main difference is that there is a NAP command in the loop. The BS2px goes NAP for 6 units, then uses the BUTTON command to check if a key was pressed on the keypad, and if not it NAPs again. If a key was pressed it figures out which one, and unless key #72 was pressed it goes NAP again, otherwise it considers itself to be awake again and returns to the program.
The above sub-routine works perfectly for short times (less than 1 minute or so). The BS2px goes NAP and only wakes up if I press the #72 key on the keypad; then it wakes up again. However, if I let the program loop in this routine for a longer time (a minute or more or so) without pressing a key or doing anything to it, the program crashes and becomes completely unresponsive. It looks like it gets hung up in the NAP loop somehow.
Does anyone see what might be going on?? Thanks!
mph
Comments
Another way to implement that on the BS2px is to use the POLLWAIT command:
The napping is more efficient with POLLWAIT, because the pin check is fast within the interpreter microcode instead of your program loop. That code does not debounce the pin, but could be added.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
However, after some 10-15 minutes or so, the program still somehow jumps out of the routine without any obvious external input and without ever getting past the POLLMODE 0 command! In fact, it does not even jump back to the line after the calling GOSUB command, but rather it just goes back to Main routine of the program.
I do not understand that. Any thoughts?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Any other thoughts or suggestions are welcome. Thanks!
You said, "However, after some 10-15 minutes or so, the program still somehow jumps out of the routine without any obvious external input and without ever getting past the POLLMODE 0 command! In fact, it does not even jump back to the line after the calling GOSUB command, but rather it just goes back to Main routine of the program."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 7/29/2010 5:28:03 AM GMT
Any ideas what else could cause the BS2px to just reset?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
- "overwriting code segments": what is that exactly, and how would I get that when I do all my programming from the Stamp editor?
So what else can cause the processor to reset apparently "all by itself":
- overwriting code segments
- ??
One way for a program to reset itself, maybe the most common glitch, is a GOTO where there really should be a GOSUB. When the subroutine hits a RETURN, the call stack is empty and the program starts from the top. Another way happen if CALLs are nested more than 4 deep. The Stamp only allows nesting to 4 levels, and having one more will instigate a RETURN to the top. It differs a bit from a hardware reset in that (I think) the variables are not initialized to zeros. So you can test this scenario with a DEBUG ? myBit at the top, followed by myBit=1. If it prints myBit=1, that means there has been a software reset, but myBit=0 means there was a hardware reset. An external power glitch seems unlikely given your observations.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com