box, you’ll need to find and correct the problem.) Close the
FIRMWARE and OPTIONS boxes to return to the Editor.
WORD variables W0-W13 are made of two BYTE
variables, and can have a value from 0-65535. Likewise,
If your PICAXE is brand new, you should see the SERIAL
TERMINAL window displaying a nonstop stream of “Hello, I
am your PICAXE-08M2” messages, which is the factory-loaded test program. Your first program will overwrite it. If
you don’t see anything, press F8 to open the SERIAL
TERMINAL window. Now, close the Terminal window and
type in the classic first program:
65535 + 1 = 0, 2 - 3 = 65535, etc. Also, W0 is made from
and overwrites BYTE variables B0 and B1, and vice versa.
Changing B0 or B1 changes W0.
Adjusting W0 changes either (or both) B0 and B1 and
vice versa. Likewise, W1 is made from B2 and B3, W2 is
made from B4 and B5, etc.:
W0=B1:B0 W2=B3:B2 W3=B5:B4
W4=B7:B6... W12=B25:B24 W13=B27:B26
sertxd (“hello world”,13,10)
You can give a variable a text name: symbol COUNTER
= B0 defines “COUNTER” as variable B0.
Click the PROGRAM button to download and run the
program. If you now see “hello world” messages in the
Serial Terminal window, you’re a programmer and a
hardware whiz too. Fairly obviously, you could change
“hello world” to any message you’d like. The 13 is the
return character, and 10 is the line feed character. SERTXD
is the command for Serial Transmit Data. Memorize it.
You’ll use that command a lot.
Math functions include:
+ (add) - (subtract) *(multiply)
/ (divide) // (remainder after division)
plus AND OR and more, in the PICAXE Editor (see
Have some fun programming before hooking up any
circuits. You can’t permanently damage the PICAXE by
programming errors alone. The worst you might be able to
do is disconnect it when a hard reset is required. (Simpler
than it sounds.) If things ever get stuck or you can’t
download a program, just turn the PICAXE power off. Click
“PROGRAM” first, then turn on the power. Done.
The word “LET” is optional but implied in commands
LET B2= 4*B1+B3/7 which is identical to B2= 4*B1+B3/7
The expression is evaluated simply left to right; there is
no hierarchy of operations. In the equation above, if B1 =
10 and B3 = 13, then B2 will be calculated as 7. That is:
4*10=40 +13=53 /7=7 53/7=7.57
Writing programs is a numbers game, no doubt about
it. If you’re just starting out, some of this next material may
seem totally Greek, but as you play around it will become
clearer. Keep at it!
(Using integer math, the fraction is dropped.)
Although, LET will be required in operations like:
IF B1>10 THEN LET B4=0
PICAXE chips use integer math only — no decimals, no
negatives. Fractions and remainders are dropped/truncated.
Three types of general-purpose variables are used to store
numbers in a program. All GP variables are initialized to
zero at power-up/reset.
B0 = B0/4 is a valid command, meaning the new value
of B0 is the old value divided by four.
BYTE variables B0-B27 are mainly used. These can store
a positive number from 0-255. Note that 255 + 1 = 0 since
numbers wrap around; 250 + 10 = 4; 2 - 3 = 255; etc.
Know it, embrace it!
SERIAL TERMINAL. As we saw with “hello world,” the
Serial Terminal window (F8) allows the PICAXE and PC to
send messages and data through the programming cable.
Remember that the PICAXE is doing all the math, and your
PC is just a display. Variable values can be displayed using
the Serial Terminal too:
BIT variables bit 0-bit 15 have a value of either 0 or 1,
and are often used as simple flags in programs. BYTE
variable B0 is made from and overwrites bit 0-bit 7, and
vice versa. Changing B0 will change one or more of bit 0-bit
7 and vice versa. Likewise, BYTE variable B1 is made from
and overwrites bit 8-bit 15:
SERTXD (“The value of B0 is “,#B0, 13,10)
Anything in quotes is a text string, printed just as
typed. #B0 shows the value of B0; again, 13 is the return
character, and 10 is the line feed character.
Here are three different looping programs to type in
and modify to learn about manipulating numbers. Play
around and see what happens. Ignore comments after the
52 SERVO 09.2015