Basic Starter Package - Tutorial #6

copyright, Peter H. Anderson, Dept of EE,
Morgan State University, Baltimore, MD, Nov 28, '97

Note.

This has not been proofed. The routines have been tested.

How to Use the Basic Stamp (BS2) EEPROM - Part 1

Overview

The Basic Stamp consists of a PIC and a 2048 X 8 EEPROM.

This tutorial briefly discusses the EEPROM and how it relates to the Stamp's operation. However, the user may use EEPROM locations which are not used by the program. Examples illustrate how to write data to EEPROM, and read from EEPROM and display the values on a PC. This is useful in logging data.

The user may also initialize EEPROM prior to run time. In Part 2 of this tutorial, simple examples are presented showing how the Stepping Motor program discussed in Tutorial #2 might be implemented using EEPROM rather than the LOOKUP command.

Finally, in Part 2, an event counter is presented which stores the current count to EEPROM. Thus, if power to the Stamp is momentarily lost, the program picks up with the current count, rather than initializing the total count to zero.

PIC and EEPROM

When you write a program, each instruction is converted to a token and these tokens are downloaded to the Stamp and saved in the EEPROM. The PIC then fetches each of these tokens in turn, interprets them and performs the action.

Thus, the PIC has the intelligence to communicate with the PC, to store the tokens in EEPROM, to fetch and act on each token. A simple concept, but I have to give Parallax a great deal of credit as they have come up with a device which is remarkably robust.

It is important to note that in writing a BS2 routine, you are not actually programming the PIC. Rather, the BS2 programming package is downloading tokens which are being written to EEPROM.

It is also useful to note that the programming area is bounded by the capacity of this EEPROM, but also by the capabilities of the tokens. The question always when a user runs out of memory; "Can't I just replace that 2K X 8 EEPROM with an 8K X 8 and quadruple my programming capacity". The answer is, "No".

However, 2K permits you to do some pretty amazing things and still have EEPROM left over to store data. For example, even if you have used three fourths of the Stamps programming are, you still have 512 bytes which might be used to store such data as temperature readings. Assumming you desire to perform a temperature every 15 minutes. 512 bytes permits 128 hours of storage, a bit over five days. You can then upload this data to a PC and perform statistical analysis using all of the capabilities of the PC.

The progam memory begins at location $7FF and grows toward $000. Dta memory, that is, EEPROM which you may use begins at location $000 and continues toward $7FF. Of curse, the two cannot overlap one another.

To avoid doing so, the Stamp Programming software permits you to examine the variable space, data memory and program memory using the ctrl-m command.

In the following discussion, I assume that the EEPROM data locations from $000 to $1FF are available.

Use of the EEPROM.

' Program EEPROM_1.BS2
'
' Shows how to write to and read from EEPROM.
'
' P. H. Anderson, Nov 28, '97

READING	VAR BYTE
VAL	VAR BYTE
EE_PTR	VAR WORD


	EE_PTR = $000
	READING = 0

	FOR EE_PTR=0 TO $1FF		' write some readings to EEPROM
	   GOSUB MEASURE
	   WRITE EE_PTR, READING
	NEXT
	   
	FOR EE_PTR=0 TO $1FF		' now dump the results to the PC
	   READ EE_PTR, VAL
	   DEBUG HEX2 VAL, " "
	   IF ((EE_PTR = 0) | ((EE_PTR+1) // 8 <>0)) THEN SKIP_CR	   
	   ' else
	   DEBUG CR
SKIP_CR:
	NEXT       

DONE:	GOTO DONE

MEASURE:	' this routine might be a routine to perform an A/D 
		' calculation or temperature measurement using the
		' DS1620 with the result returned to the calling program
		' in variable READING.  In this routine, READING is 
		' artificially generated by adding 5 to the current value.

	READING = READING+5
	RETURN
This program is in two parts; writing to and reading from EEPROM.

In the "write" portion, subroutine MEASURE is called. This might be a routine to perform a temperature measurement or A/D reading using the DS1620 or the ADC0831 supplied with the Basic Starter Package. In this program, READING is simply the previous READING plus 5.

The results of these each of these measurements are written to EEPROM at sequential locations beginning at $000. Note that the form of the WRITE command is;

	WRITE adr, value
Note that variable EE_PTR was declared as a word as it was to take on values from 0 to 511 ($000 to $1FF). Recall that a byte is limited to values in the range of 0 to 255 ($00 to $FF).

In writing to EEPROM a delay of nominally 25 msecs is required for the PIC to "burn" each data byte and in running the program, you will a noticeable delay of perhaps one second. Fortunately, the WRITE command takes care of this required delay for you.

The data is then sequentially read from EEPROM and displayed on the terminal using the DEBUG command.

A few of the statements in this dump routine deserve some explanation

	DEBUG HEX2 VAL, " "
The DEBUG is an extremely sophisticated command. Note that I didn't use the words "difficult" or "complex". Rather, there are many options, much like the printf command in the C programming language.

The HEX2 modifier causes variable VAL to be displayed in hexadecimal format with two characters; e.g. 81.

Other similar modifiers include the following.

	BIN		' Print value in binary 
	BIN1 .. BIN16	' Print value in binary with 1 to 16 digits
	IBIN1 .. IBIN16	' Same as BIN1 .. BIN16 except adds "%" character

	DEC		' Print value in decimal
	DEC1 .. DEC5	' Print value in decimal with 1 to 5 digits

	HEX		' Print value in hex
	HEX1 .. HEX4	' Print value in hex with 1 to 4 digits 
	IHEX1 .. IHEX4	' Same as HEX1 .. HEX4 except adds "$" character
There are seven others that cause a variable to be displayed in signed format which will be discussed in another tutorial, and there are a few others as well.

Before you are overcome with an anxiety attack, a few words.

The nature of our educational system tends to approach all knowledge as being important, to be committed to memory forever. Thus, many teachers might give their students exams that includes parroting back such trivia in a "closed book" environment.

In my mind, a much better approach is for the student to read and understand the capabilities of each of these and don't worry if you can parrot them back. Commit the concepts to memory. Commit where you store your Stamp manual to memory. This is why manuals are written.

What if you are stranded on a desert island, which is the usual arguement for rote memorization. Try not to get stranded, but if you do, remember to bring PC, Stamp2.exe, cable, power supply, the Stamp itself and by all means, don't forget the manual.

On that cheery note, consider;

	IF (((EE_PTR+1) // 8) <>0)) THEN SKIP_CR	   
        ' else
	DEBUG CR

SKIP_CR:
The function is to print eight values on a line and then insert a new line character. Recall the // is the "mod" operator; i.e, what your 5th grade teacher called the "remainder". The <> operator is "not equal". Thus for values of EE_PTR in the range of 0, 1, 2, .. 6, EE_PTR is 1, 2, 3, .. 7. In all cases, this value divided by 8 is 0 with a remainder which is not equal to zero. Thus, the Boolean expression is true and the GOTO which skips sending the new line character is executed.

However, when EE_PTR is 7, 15, 23, EE_PTR +1 is 8, 16, 24, etc. These values divided by 8 are something with a remainder of zero (or not nonzero). Thus, the Boolean expression is false and a new line character is sent to the terminal.

What about all of those parenthesis. I find them mighty useful. In this case, first, add one to EE_PTR. Second, perform the mod operation. Then do a relational test. With the parenthesis, there is no question in my mind as to what happens first, second or third.

In fact, PBASIC evaluates right to left. Thus;

	IF EE_PTR+1 // 8 <>0 THEN SKIP_CR
is in this instance the same.

However, in going right to left, unexpected things can happen;

	A = 1 + 2*3
This unexpectedly evaluates to 9, not 7. The 1 is added to the 2 and the result is multiplied by 3.

Thus if you really wanted the multiplication to first be performed;

	A = 1 + (2*3)
But, who can remember all of this trivia and what is the point. Parenthesis do not take up any program memory. Parenthesis are "free", and I suggest you always use them.

Program EEPROM_2.BS2.

The following program simply dumps the first 512 bytes of EEPROM to the PC using the DEBUG command.

Thus, if you have run EEPROM_1.BS2, you might turn off the Stamp and then run this routine and verify the data stored in EEPROM_1.BS2 is indeed still there.

' Program EEPROM_2.BS2
'
' Dumps from EEPROM when pushbutton PB11 is depressed.
'
' P. H. Anderson, Nov 28, '97

VAL	VAR BYTE
EE_PTR	VAR WORD

SCAN:	IF(IN11 <>0) THEN SCAN		' wait until PB11 goes to zero
	   
	FOR EE_PTR=0 TO $1FF		' now dump the results to the PC
	   IF (EE_PTR <>0) THEN SKIP_INIT
	   DEBUG CLS, HEX3 EE_PTR, ": "
SKIP_INIT:
	   READ EE_PTR, VAL
	   DEBUG HEX2 VAL, " "
	   IF (((EE_PTR+1) // 8) <>0) THEN SKIP_CR	   
	   ' else
	   DEBUG CR, HEX3 EE_PTR+1, ": "
SKIP_CR:
	   IF (((EE_PTR+1) // 128) <> 0) THEN SKIP_PAUSE
	   PAUSE 5000 		' pause to inspect the data
SKIP_PAUSE:
	NEXT       

	GOTO SCAN
Note that this "dump" routine has been improved.

The use of pushbutton PB11 controls when the data is to be sent to the PC. When released, the switch is open and the Stamp sees a logic one on IN11. Thus, the program continues to scan the pushbutton.

When depressed, the state seen by the Stamp is a logic zero and the data is dumped to the terminal.

If EE_PTR is zero, the terminal screen is cleared using CLS and the current value of EE_PTR is output with 3 digits in hexadecimal, followed by a colon and a space. Thus;

	000: 
The eight values are then output in two digit hexadecimal with a space spearator. After each eight values have been output, a new line character is sent, followed by the next value of EE_PTR. The result is a format like the following;
	...
	008: AF 10 00 12 34 56 78 3C
	010: 20 22 23 3C 45 78 3C 00
	...
(Note that I made up the data values!).

After each 64 values or 8 lines, a five second pause is inserted to allow the user to inspect the data.

Summary of EEPROM_1 and EEPROM_2.

These routines have illustrated the use of the WRITE and READ commands. Additional features of the DEBUG command have been introduced along with some relatively complex Boolean expressions. Use parenthesis to be certain your Boolean expression is doing what you want.

Clearly, the two routines could be combined, such that when the pusbutton is released, the unit is in the log mode, periodically performing measurements and saving the data to EEPROM.

Five days later, the user might retrieve the unit, connect it to a PC running QBASIC or a terminal emulator package. Once ready, the user might depress the pushbutton and the data is dumped to the PC.

We aren't quite there yet. There is a slight problem if the Stamp resets when in the log mode. When it reboots, it will begin to write over old data, which may not be desireable.

And, of course, we have yet to interface with such devices as the DS1620 and ADC0831 to generate real data. Rather, the current data is simply generated by adding five to the previous value.

One other item. It is not realistic to field a product that requires the end user to execute the Stamp2 program so as to view the data. You would probably rather they use a QBASIC or Visual Basic Routine, perhaps even import the data into spreadsheet and draw fancy plots. This electrical engineer can't do all of this fancy PC programming, but in a future tutorial I will show how you can use the DEBUG command to interface with any computer having a serial port capable of 9600 Baud. Thus, all of the sophisticated formatting capabilities of the DEBUG command may be harnessed, not simply for debugging, but for actual operation.