Stamp
for sending a string is now a single-step process. At the top
of our control program, we’ll define a bunch of zero terminated strings (z-strings) in DATA statements, much in the
way we do it in the BASIC Stamp:
TX_STR
SUB
2
Prompt:
DATA CR, LF, “>> “, 0
Version:
DATA CR, LF, “ PC_PORT16 Version 1.0”, CR, LF, 0
Pad:
DATA CR, LF, “ “, 0
CRLF:
DATA CR, LF, 0
PortStatus:
DATA CR, LF, “ Ports = “, 0
As you can see, a subroutine that handles a string
requires two bytes: the base address and character offset
(these will be handled by the compiler when we make the
call to TX_STR). The reason for this is that the SX’s
[native] IREAD instruction will be used to pull a character
and it requires a 12-bit address. Here’s the code for
TX_STR:
Note that the strings also contain constant values for
carriage return (CR) and line feed (LF) that are also
defined in the program (i.e., they are not built into SX/B).
It is our responsibility to write the subroutine that handles the string because SX/B has no idea where it’s going
to go. In this program, we’ll send it to the PC using
SEROUT. First, of course, we need to define the subroutine for the compiler:
TX_STR:
temp3 = __PARAM1
temp4 = __PARAM2
DO
READ temp3 + temp4, temp5
IF temp5 = 0 THEN EXIT
TX_BYTE temp5
INC temp4
temp3 = temp3 + Z
LOOP
RETURN
We start by saving the base address and character offset in variables temp1 and temp2. Then we enter a loop
that uses READ to pull a character and, if the character
value is not zero, we send it to the PC with TX_BYTE. By
using variables for the base and offset, both can be updated, allowing the string to cross SX page boundaries. This
makes our life simple, and the 1.4 compiler even lets us do
this:
TX_STR “Hello, World!”
Yes, we can embed a string right into the program
code. A note of caution, however: the string will be embedded in place (the terminating zero is added by the compiler); if we’re going to use the same string more than once,
then using this style is not the best choice. Just to clarify,
when a string is going to be used in more than one place
in the program, then the best thing to do is put that string
into a DATA statement.
As we just saw, TX_STR calls TX_BYTE to send the
character to the PC at the specified baud rate (115.2
kBaud in this program). Let’s have a look at that code:
F
o
r
E
l
e
c
t
r
o
n
i
c
s
NUTS & VOLTS
E
v
e
r
y
t
h
i
n
g
TX_BYTE:
temp1 = __PARAM1
IF __PARAMCNT = 1 THEN
temp2 = 1
ELSE
temp2 = __PARAM2
IF temp2 = 0 THEN
temp2 = 1
ENDIF
ENDIF
DO WHILE temp2 > 0
SEROUT SOut, Baud, temp1
DEC temp2
LOOP
RETURN
70
Circle #111 on the Reader Service Card.
SEPTEMBER 2005