BASIC Stamp bugs & gotchas

(c) 1998, 2000 EME Systems, Berkeley CA U.S.A.
<stamp index> <home>

Contents: (updated 11/18/2000)


BS2 sleep bug

top

The BS2 is advertised to have a current drain of less than 50µamps in sleep mode, and experience has shown that current production units are running a best sleep current of around 30 µamps. However, there is a subtle bug in the BS2 memory circuits, which can result in a sleep current of as much as 350 microamps. It depends on exactly where in memory the SLEEP instruction is encountered by the interpreter. The SLEEP instruction has to fall at a "good" position in the eeprom or else the sleep current drain will be greater than 300 microamps instead of less than 50 microamps. If you just let it fall where it may, you have something like a 50-50 chance for it to fall in a good position. In some applications, such as remote data logging, the system can spend a great proportion of its time in sleep mode, and the sleep current is a major factor in the battery life. It is better not to leave that to chance.

I had thought to put my one SLEEP instruction near the beginning of my programs, so as to fix its position in the eeprom for once and for all to minimize current. But that didn't work as easily as planned, because each GOSUB instruction adds code at the top, above the program in eeprom. In fact, each GOSUB adds exactly 14 bits at the top of memory. Nevertheless, it is still worthwhile to put the SLEEP instruction in a routine near the top of the program. Then the adjustment only needs to be made when the total number of GOSUBS in the program changes, and by counting the number of gosub's, it can be kept constant by adding extras. Here's a skeleton of a program, to demonstrate how the sleep routine fits in near the top, and how the exact position of the SLEEP command is adjusted:

zb var bit ' bit variable for pre-sleep adjustment
well var byte ' sleep duration
' define other variables/constants/data

start:
goto main0 'skip over the bed time code

bed: ' bed time subroutine
' it goes here to fix it near the top in eeprom
zb=1 ' play with zb to minimize current
' case 0) use zb=15
' case 1) use zb=1
' case 2 & 3) comment it out ' zb=.
' see below for explanation
sleep well ' no driven loads! no floating inputs!
return

main0:
' initializations
main:
' program goes here
well = 10 ' how long to sleep
' no driven outputs, no floating inputs!
' application dependent
gosub bed ' do it, and return here
' more program
goto main
end
'gosub bed
'gosub bed
'gosub bed

Here's the trick, hardware and thinkware versions:

1) Measure the stamp current during sleep. Try zb=1, zb=15 or comment out the zb= statement. One of these 3 possibilities will result in <50 microamps current. (Assuming no driven loads, floating inputs etc.) If the current is around 300 microamps, try another possibility.

or

2) Count the number of gosubs in the program, divide by 4 and look at the remainder.

That's it. The above program as it stands has one gosub, so zb=1.

How to count gosubs? Use the ALT-F find "gosub"; ALT-N again and again; keep a mental tally. Don't count remarks of course! The FIND function in the BS2 editor is case sensitive. If you're not sure that all of them are cased the same, you might want to use a more capable text editor to do the count.

As a practical matter, there are installations where it is difficult to measure the sleep current, so counting gosubs is an attractive alternative. I wish the bug would just go away! The above method of counting GOSUBs may not always work. I tried it on some shipments of BS2s and it did not work as expected. But the business of measuring the sleep current, and trying either zb=15. zb=1 or no zb at all was always able to yield the minimum sleep current.


Note that this workaround is effective only if the BS2 has the Microchip EEPROM. For a short period of time, Parallax was shipping BS2s with an ATMEL EEPROM, for which the above workaround is not effective at all. Parallax stopped using the ATMEL parts as soon as the problem was discovered.

 

The Sleep Bug, less a factor in the BS2sx & BS2e

top

In the BS2, the effect of the sleep bug is quite dramatic and can result in sleep currents that range from 25 microamps at best up to 450 microamps at worst. This can have a huge effect on the averaged sleep current. On the other hand, for the BS2sx, the sleep current (not averaged) ranges from 46 microamps at best up to about 58 microamps at worst, and has no appreciable effect on the averaged sleep current.

In either the BS2 or the BS2sx, the current can be minimized by padding the instructions before the sleep with a "dummy" instruction that adds just enough bits to move the SLEEP or END instruction up to a favorable position in the eeprom. In the above program the sleep command is placed in a subroutine at the very top of the program code. This does not assure that it stays in one place, because every GOSUB command in the program adds a pointer at the top of memory. Nevertheless, each GOSUB adds exactly 14 bits, and so it is easy to predict the position of the SLEEP command. A value of bitty=63 or a value of bitty=3, one or the other, will always minimize the sleep current. In fact, if the number of GOSUBs, modulo 4, is equal to 1 or 2, then use bitty=63. If it is 3 or 4, then use bitty=3.

The original BS2 is still the champion when it comes to low current operation where the SLEEP instruction is concerned. The BS2 sleeps at an averaged current of around 50 microamps, while the BS2SX sleeps at around 525 microamps. To achieve the lowest current using the BS2, you must take care to position the SLEEP command at a favorable position in the eeprom. With the BS2SX or the BS2e, the position of the SLEEP command in eeprom doesn't make much difference.

Stamp

operating current

sleep current

averaged sleep current

BS2

8 milliamps

30 microamps

70 microamps

BS2e

28 milliamps

50 microamps

250 microamps

BS2sx

65 milliamps

50 microamps

530 microamps


Brownout behavior, a bug?

top

BASIC Stamps operate with a Vdd power supply of nominally 5.0 volts, but they can operate on slightly higher and slightly lower voltages. What is of concern here is low voltages as might come about, say, as a battery voltage dips in a solar charge cycle, or in a system that occasionally has brief power failures. Sags in voltage are called brownouts., while a complete and sustained loss of power is called a blackout.

The following discussion applies only to the BS2sx and the BS2e. The original BS2 has an external brownout detector, whereas the BS2sx and BS2e depend on the brownout detector that is built into the SX28 chip. That built-in brownout detector has some characteristics that can only be described as a "bug". But it is only a bug if you are using the SLEEP, NAP or END instructions and expect your system to behave well in those low-power modes as the battery voltage drops.

The BASIC Stamp operates fine down to 4.2 volts, but at that point a brownout detector puts the SX28 chip in a reset state. What happens at that point depends on what the Stamp was doing when it hits the brownout threshold, and on how low the voltage dips. Here are a few observations:

What of it? The troubling thing for the BASIC Stamp 2e or 2sx is that the chip does not reset as a result of brownouts that occur during sleep intervals. There is a possibility that variables would be corrupted during the brownout, and there would not be a clue as to why that had occured. My observation is that the chip hangs onto its variables quite well, even at low voltages, but it is certainly not guarenteed. The integrity of the variables would be more sensitive to influences of noise (EMF) and extreme temperatures. Another troubling aspect of this behaviour is that the current drain becomes quite high if the chip goes into brownout. Usually a system uses the SLEEP mode to conserve battery power, but the higher current in brownout could deal a final blow to a marginal battery.


<top> <index> <home> logo < mailto:info@emesystems.com >