comments refer to the corresponding
numbers along the right edge of the
program listing:
[1] We’re using the same
hsersetup command we used
previously. The mode parameter is
configured as follows: background
receive to scratchpad, true serial input
and output, hserin and hserout pins
enabled.
[ 2] We’ve already discussed the
setintflags command that we’ll be
using this month, so there’s no need
to elaborate further.
[ 3] If you have used the setint
command in any of your projects, you know that the first
statement in the interrupt subroutine is usually setint off.
The purpose of that statement is to make sure the
interrupt subroutine itself will not be interrupted while it’s
being executed. If that were to happen, the program
would behave erratically.
In the current program, it’s highly unlikely that
additional background serial data would be received while
the current data is being processed, but including the
setintflags off statement protects the program from that
remote possibility. Of course, if a second microprocessor
were transmitting the data, you would need to make sure
to include a brief pause between data transmissions.
[ 4] At this point, it’s important to remember that the
interrupt is triggered as soon as the first data byte is
received in the background. Even at a rate of 115,200
baud, a string of data that’s possibly as long as 128
characters will still be in the process of being received at
the beginning of the interrupt. Therefore, a program delay
is needed at this point to make sure all the data has
arrived before we begin to process it.
In order to determine how long the delay should be,
we need to do a little approximate arithmetic.
• Each incoming byte contains one bit: 1 start + 8 data +
1 stop
• Therefore: 128 bytes = 1280 bits
• Form a proportion: 115,200 bits / 1 sec = 1280 bits / x
sec
• Therefore: 115,200 x = 1280 1
• So, x = 0.011 seconds, or 11 mS
(I decided to increase the delay to 15 mS, just to be
safe.) If you want to run the program using a different
baud rate, the necessary delay will need to be
recalculated. To save you the trouble, Figure 3 gives the
necessary delay for selected alternate baud rates.
[ 5] When the program first begins to run, hserinptr is
automatically initialized to 0 (as are all PICAXE variables).
Therefore, when the interrupt occurs,
the first received byte is stored at
location 0 in the scratchpad, and
hserinptr is automatically incremented
to 1 in preparation for the next byte.
This process continues for every byte
that’s received in the background.
As a result, when all the data has
been received, hserinptr points to the
location in the scratchpad that is one
greater than that of the last received
character. Therefore, at this point, we
decrement hserinptr by 1, so that it
points to the location of the last
received byte.
[ 6] Here, we loop through the
scratchpad locations from location 0 to the location of the
last received byte using direct addressing to get each
character, and then echo it back to the terminal program.
[ 7] Before returning from the interrupt subroutine, we
need to reset hserinptr to 0, so that storage of the next
serial input data will again begin at location 0 in the
scratchpad.
[ 8] We also need to reset the hserinflag to 0 in
preparation for the next background serial transmission.
[ 9] The very last thing we need to do before returning
from the interrupt subroutine is to re-enable the interrupt
so that the next background serial transmission will again
trigger it.
At this point, we’re ready to test the Hser20X2
IntDirect.bas program, but before we do that, I want to
correct a mistake I made in the hserial20X2.bas program
we used in the previous Primer. In that program, I
included a note stating that pin A.0 is “undocumented” on
the 20X2 (as it is on the 20M2). However, that’s not the
case. If you refer to the 20X2 pinout, you will see that pin
A.0 is, in fact, documented. In the present program, I
corrected that error.
Before running the Hser20X2IntDirect.bas program,
make sure your terminal software is set up as described
previously, and that your breadboard circuit is set up with
a 20X2 processor in place (see Figure 2).
When you’re ready, download the program to your
breadboard setup and test several different input strings.
An exact duplicate of each string should be echoed back
to the terminal. If not, you will need to check your
breadboard setup for wiring errors.
Experiment 2: Hserin with an Interrupt
and Indirect Addressing
This experiment essentially replicates Experiment 1,
except this time we’re using indirect addressing to echo
the data back to the terminal. In Experiment 1, the
September 2015 13
■ FIGURE 3. Interrupt pause needed
for various baud rates.