Controlling Picaxe microcontrollers with a computer or let them communicate
On general demand (ok, Fritsl asked me...), I will show and explain you how to let several Picaxe microcontrollers communicate with each other or how to control a picaxe microcontroller with your computer.
This is actually fairly easy with the serrxd and sertxd commands. Serrxd reads the serial in port and sertxd writes to the serial out port. Now, when you check the Picaxe manual for these commands you will see this:
What this means is:
- Qualifier: when this is used, the Picaxe will wait until it reads the value that has been entered as qualifier and then continue with its operation
- Variable: b0... you know these ;)
Little example: serrxd ("LMR"),b1 will wait until it reads the letters LMR and then put the first byte after thos three letters in b1.
For sertxd you find:
- Data: this can be anything... Sentences (always place text that needs te be read as text by the receiver between ""), variables (b0, w1 and so on) or special orders (more on that later)
Now, to control the outputs on a Picaxe 28X1 I used this code:
serrxd("dev0"),b0,b1 'Read serial in port and wait for string dev0 then put next two bytes in b0 and b1
if b0 = 49 then
sertxd("Light ",b1," switched on. ") 'Send a message to serial out port
else if b0 = 48 then
sertxd("Light ",b1," switched off. ")
sertxd("Waiting for command...",13,10)
(I used this codes to turn on LEDs that's why you see "light switched on/off")
Now, I think some explanation wouldn't be bad...
- serrxd("dev0"),b0,b1 ==> Reads the serial in port. The qualifier "dev0" makes sure the controller waits until dev0 is read on the serial in port. After it has read the qualifier the command will put the next two bytes the controller receives in variables b0 and b1.
- sertxd("Light ",b1," switched on. ") ==> Sends 3 messages to the serial out port, when received by the terminal in the programming editor on the computer these 3 messages will be displayed right next to each other and form a sentence
- sertxd("Waiting for command...",13,10) ==> Sends the message Waiting for command... to the serial out port. 13 and 10 are special commands and will be explained later on.
This program will do the following: when I send dev011 the controller will enable output one, when I send dev001 the controller will disable output one. This can be doen for outputs 0 till 7 by altering the last number in the code that is being sent.
Now, before you start programming you controller, read this paragraph very carefully! The serrxd and sertxd don't work with numbers but with ASCII values... This means that when dev012 is sent to the controller running the program above, the controller will put value 49 in b0 and value 50 in b1. This is because the character 1 is value 49 in the ASCII table. This also means that it is possible to send special function: if you look at ASCII value 13 you will see that it corresponds to the character 'carriage return', this makes sure the next characters will be displayed at the beginning of the line. ASCII value 10 corresponds to 'line feed' and places the cursor on a new line. So if you follow the logic, executing the code sertxd(13,10) will start a new line at the beginning (or do the same as if you would hit 'return' when typing in a text editor).
This also implies that you can't use high b1 straight away in the program above, because that would try to enable output 48 or higher (character zero corresponds to ASCII value 48, char. 7 to 55). That's why I've put in a line of code that subtracts 48 out of b1 and puts the result in b2, that way I can use the variable b2 to enable or disable outputs and variable b1 to give feedback to the computer.
Now, if you apply the same logic to two Picaxe microcontrollers instead of a Picaxe and a PC, you can let the microcontrollers communicate with each other.
The Picaxe microcontrollers also have the commands serin and serout, these do exactly the same as serrxd and sertxd but let you decide what pin to use.
Any questions will be answered with pleasure.