■ BY RON HACKETT PICAXE PRIMER
www.jrhackett.net
PICAXE-PC Serial Communication
— Part 3
PICAXE BASIC includes two scratchpad-related
commands: get and put. The get command is used to read
data from the scratchpad. (In other words, the get
command “gets” data from the scratchpad.) The full syntax
is get location, variable, variable, WORD wordvariable ...
where variable is the name of a byte variable that receives
the data, and WORD wordvariable refers to the name of a
word variable. (To use a word variable, the keyword
WORD must be used before the wordvariable name.)
In Part 2 of our PICAXE-PC serial
communication project, we moved up to
the 20X2 processor and experimented with
one way of speeding up the response time
for incoming serial data that has been
received in the background. At the end of
that article, I mentioned there’s an
interrupt-based method of improving the
20X2 response time, so that it’s even faster
than our previous approach. This month,
we’ll experiment with that approach, but
first we need to discuss some of the details
of the commands and built-in variables that
we’ll be using. Let’s get started.
We used a simple form of the get command in the
previous Primer column, but this month I want to include
a little more detail. The following example statements
should clarify the syntax. In each example, loc refers to a
location in the scratchpad:
get 10, b1
‘ value at loc 10 is copied to b1
get 0, b1, b2, b3
‘ value at loc 0 is copied to b1
‘ value at loc 1 is copied to b2
‘ value at loc 2 is copied to b3
As we’ve already discussed, only the X1-class and X2- class processors contain a scratchpad memory area:
the 28X1, 40X1, and 20X2 processors contain 128 bytes
of scratchpad memory (numbered 0-127); and the 28X2
and 40X2 processors contain 1024 bytes of scratchpad
memory (numbered 0-1023). We can use the scratchpad
memory area for the temporary storage of data in any
program, and it’s especially useful for arrays.
get 5, b0, word w3
‘ value at loc 5 is copied to b0
‘ value at loc 6 is copied to low-byte of w3
‘ value at loc 7 is copied to high-byte of w3
However, if we configure a program to use the
hardware serial input (hserin) pin, the processor
automatically uses the scratchpad to store the serial data
that’s received in the background. As a result, we would
need to be careful if we wanted to store temporary data in
the scratchpad and also use it for hardware serial input.
The put command is used to write data to the
scratchpad. (In other words, the put command “puts” data
into the scratchpad.) The full syntax is put location,
variable, variable, WORD wordvariable, where variable is
the name of a byte variable whose value is being written to
the scratchpad, and WORD wordvariable is the name of a
word variable. (Again, to use a word variable, the keyword
WORD must be placed before the wordvariable name.)
The following example code statements should clarify
the syntax. Also, loc again refers to a location in the
scratchpad:
For example, we could decide to use the first 64
locations in the 20X2 scratchpad (0-63) for serial input,
and the remaining 64 locations (64-127) for temporary
storage of other data, but we would need to be sure that
we never receive more than 64 bytes of data. If that
happened, the incoming bytes would over-write our stored
data, and the program would behave erratically.
put 10, b1
‘ value of b1 is copied to loc 10
put 0, b1, b2, b3, b4
‘ value of b1 is copied to loc 0
‘ value of b2 is copied to loc 1
‘ value of b3 is copied to loc 2
‘ value of b4 is copied to loc 3
Scratchpad-Related Commands
put 5, b0, word w3, b1
‘ value of b0 is copied to loc 5
‘ low-byte of w3 is copied to loc 6
‘ high-byte of w3 is copied to loc 7
‘ value of b1 is copied to loc 8
10 September 2015