The address is very important. If your address is off by a few bytes then you will lose those bytes from the front or back of your string. No errors happen. This 16 * tmp5 – 1 looks OK. It is not OK. Basic Stamps are strictly left to right math. Basic Stamps 2s allow parentheses as 16 * (tmp5 – 1). With this 16 * tmp5 – 1 is actually (16 * tmp5) – 1 and you would be off by 1 address. In this case I lost the first letter of the string I was editing.
Math can be fun. The Basic Stamp 2s support negative numbers, but you must be careful with them. If you have two variables counting down and you are comparing them tmp3 >= tmp4. If tmp3 reaches 0 and you subtract 1 it will be negative and more than tmp4. Put a debug statement on tmp3 and see where it goes. In this case my target is tmp5. I changed it to tmp4 >= tmp5 and let tmp3 go to 0. Then I adjusted tmp5 by – 1 for the 0 based array of strings. Zeros are not good for math or comparing.
The main thing you need for an interactive thing is memory. The cheapest and easiest memory to use is a serial E-prom. What is an E-prom? It is an eclectically programmable and eclectically erasable memory with a serial interface. E-Proms keep their memory when the power is shut off. E-Proms let you re-program the memory. Usually ten million or so cycles. Read your manual. You can store just about any type of data into an E-Prom. I wish I had friends to share this with. I will be working on LCD displays and kind of a dumb terminal to show how easy it is to connect your ideas to your people.
The Code file is EPromEW6.txt. You must change it to EPromEW6.bs2 for it to run. The Zip file has the code file and pictures.
Most E-Proms are Byte sized memory arrays. The biggest user of memory is stings like the quinquennial “Hello World”. Strings are an array of bytes in the processor’s memory. Strings are well suited to E-Proms. In this example my serial E-Prom is 2048/8 or 16K with 128 16 Byte pages. Yes, 128 strings. Like most serial devices E-Proms have a processor that is set up to write and read the memory array. You control them with commands and input and output leads. Timing is everything.
You need a lot of pins to use a serial E-Prom when you are writing to the memory. The pins: SO Bytes Output, SI Bytes Input, CK Clock and CS Chip Select. I use separate pins for SO & SI to protect my E-Prom. You use commands to tell the E-Prom what you want it to do. To read a Byte you lower CS and send in a Read command. “ShiftOut epRead\8, erAddr\16.” The \8 tells ShifOut to send 8 bits total. So 32 is 100000 is only 6 bits. The \8 makes it 00100000 and that is 8 bits. That is what the E-Prom needs. Reading is a continuous stream of Bytes as long as you provide clock pulses. “ShiftIn myStr(tmp1).” You can use an address with the read command. Each page is 16 Bytes. So 32 is page 1. Remember the E-Prom memory is Zero based and strings are zero terminated.
Writing is a bit more complicated. You hold Write Protect and Hold pins high then you set CS low and send in a Write Enable command. “SHIFTOUT epSi, epCk, MSBFIRST, [epWrtEN\8]”. Then you raise CS pause 5ms and then lower CS pause 5ms. That enters the command into the E-Prom. Then you send in a Write command. “SHIFTOUT epWrite\8, erAddr\16.” In writing you usually need to write all 16 bytes in one command and terminate it correctly. In a loop “SHIFTOUT myStr(tmp1).” Then I pause 5ms and raise my CS to tell the E-Prom to write my data. You must wait or the E-Prom may crash. To reset the E-Prom you lower CS wait 5ms then raise CS wait 5ms then lower CS and continue with your commands. I keep my CS low with a 10K resistor to ground.
The E-Prom also has a Write Protect pin. With this pin low you can not write to the E-Prom. It also has a Hold pin that when it is low the E-Prom is off. You can write protect one quarter, half or all of the memory with status flags. I keep Write Protect and Hold connected to VDD.
Addressing is mandatory and easy. My address is on 16 Byte boundaries so it is easy. You can write any 16 byte page of memory that you need to and nothing else is affected. Truly random access memory on 128 16 byte strings. Remember the memory is zero based and strings are zero terminated. String 1 is actually string 0. All of my string numbering is 1 based. My program takes care of the details. In my program epAddr is the string edit point and myStrS is how many strings I have. Displaying the strings starts at 0 and runs though myStrS numbering each string. Concatenated strings only the first string gets a number. Sub-strings only the first one gets a number.
One thing about string handling is that most systems use Zero terminated strings. That means that the last Byte in “Hello World0” is a Zero Byte. A 16 Byte array holds 15 characters and a terminating Zero. Also most Byte arrays are Zero based. That is the first position is Zero and the last is 15. For stings to be recognized you have to make sure that the zero is in the right place. You can use just one 16 byte array for everything. For short strings just move them to the front and terminate them. The rest of the string will be ignored. Keep track of your position. When you are concatenating a string to the next string you can not use all 16 bytes. Why? The output routine still needs the first string terminated.
Other data needs to be formatted to fit into a Byte array. A Word has 16 bits or two Bytes. A high Byte and a low Byte. Longs or doubles have 32 Bits or four Bytes. Byte 3, Byte 2, Byte 1 and Byte 0. You build formatters to take care of this type of data. Store them Byte by Byte in an order you like. Like high Byte then low Byte. Load them back the same way. The memory of most processors is 16 bit pages. Word sized memory that can be broken down into Bytes and on some down to the Nibs or Bits for variables. When you store a Word variable you usually have access to its Bytes as variables to.
Some processors have good string handling functions. I am using a Parallax.com Basic Stamp 2 for this example. The commands used are Debug, DebugIn, SerIn and SerOut. Debug and serOut are the same function in most respects they just go to different places. To input a string you use a STR formatter that looks for character strings. “DebugIn STR time\15\13” Looks for and inputs a string up to 15 characters long that it loads into a Byte array time when the user presses Enter(13). If the string is less than 15 characters then the remaining characters are filled with zeros. The string “Hello” would be “Hello00000000000” in memory. “Debug STR time” prints “Hello”. The first Zero terminates the string and the rest are ignored.
Numbers can be fun. The input formatters are for character strings. “DebugIn DEC time” looks at the string “hello123bye” and converts it to the number 123 and puts it into the variable time. It ignores the rest. Useful for a lot of interactive things. For Bytes arrays you use control characters like Zero as a terminator at the end of strings. You create other control characters to fit your needs. I use the ‘+’ to separate short sings and join long string in my output routines. Short strings can be put together usually in the front of the E-Prom. Fifteen characters are never enough. You can create all sorts of controls in output routines.
My program: The button enters command mode. Enter a command and press enter on the keyboard. You can not keep these small processors waiting for an input. They are prone to crashing. You run an active loop waiting for a button to be pressed. My program stores the last address and the string count into the processor’s EEPROM and loads it at start up or re-boot. Set myStrs to zero for new strings and 1 for your strings to be loaded. My program is a fast string loader for an E-Prom.
My program uses the debug terminal and a standard button. You push the button to enter command mode. Type your input and press enter. The led should flash when waiting for a command. Remember to set the processors EEprom when you end a session.
To edit your strings after a re-boot use command 60 first to load your address and string count into my program. Command 10 will enter a new string after the last string. Command 40 edits a string. When you are done with your strings use command 50 to write the address and string count to the EEprom. Use the editor to change myStrs to zero for new strings or 1 or higher to load your strings and re-load the program.
The Arduino is just too much fun. Excellent string handling.
Picaxe Systems: They do not have arrays. Only the x2 parts have a ShiftOut and ShifIn commands. The scratchpad is a temporary memory area for storage of data such as arrays. Only on the following Picaxe chips PICAXE-28X1, 40X1, 20X2 parts have 128 scratchpad bytes (0-127) and PICAXE-28X2, 40X2 parts have 1024 scratchpad bytes (0-1023). To access the Scratchpad use: Set the pointer is ptr = var, Indirect addressing is ‘@ptrinc’ (post increment) and ‘@ptrdec’ (postdecrement). Every time the ‘@ptrinc’ variable name is used in a command the value of the scratchpad pointer is automatically incremented by one. This makes it ideal for storage of a single dimensional array of data. See the example in the manual.
ShiftIN and ShiftOut: The spiin (shiftin also accepted by the compiler) command is a ‘bit-bang’ method of SPI communication on the X1 and X2 parts ONLY. All other parts must use the sample program included overleaf to duplicate this behavior. For a hardware solution for X1/X2 parts see the ‘hshin’ command. Or you can use my doLights and checkLights from EasyLights.