PICAXE Serial Interfacing



Connecting a PICAXE processor to a PC's serial port is easy; if you know how.



One of the most daunting challenges facing a newcomer to the PICAXE is working out how to connect a PICAXE up to a PC's serial port.

This article explains how serial interfacing should be implemented for the PICAXE, and clarifies the terminology used in the PICAXE data sheets for those familiar with serial interfacing, but not the PICAXE itself.

The first thing to note is that there are two distinct serial interfaces for the PICAXE - The program Download Interface, and serial interfaces under the control of the user's program.

Understanding the Download interface, described in detail below, will make understanding User Controlled Serial Interfacing much easier.

This article is based upon information provided by Revolution Education Ltd, the manufacturer of the PICAXE, and from practical experience using PICAXE devices.



AXE025

CON038

AXE026

CON039

ADA010


Index


The Download Interface

The program Download Interface is used to download programs for the PICAXE using the serial interface. There are two legs of all PICAXE's which are generally dedicated to this function. Although some PICAXE's may allow at least one of those pins to be used for other functions, we will ignore those additional functions for now.

The Download Interface uses two legs on the PICAXE which are labelled as "Serial In" and "Serial Out" and ultimately connect to a PC's serial port. The Serial In line receives data from the PC and Serial Out sends data to the PC; both are required to achieve successful programming.

The legs used for Serial In and Serial Out depend on the PICAXE variant used, and are as follows ...

Function

08, 08M

18, 18A, 18X

28, 28A, 28X

40X

Serial In 2 3 6 6
Serial Out 7 2 7 7

PICAXE Pin-Outs

Pin-outs indeed, and the first issue to address is why this article refers to "Legs" rather "Pins" as is more traditional when discussing connections to integrated circuits ( IC's ).

The reason "Pin" has not been used is because the PICAXE documentation refers to "Input Pins" and "Output Pins" which are numbered in a manner which can be confused with the pin numbering of the actual device, and these are often referred to simply as "Pins". To avoid confusion between whatever "Pin" may mean, we will refer to the physical pins on the device as "Legs" in the rest of this article.

The pin-outs ( or perhaps, to be consistent, leg-outs ) of the PICAXE variants are as shown below. The legs used for Serial In and Serial Out have been explicitly identified as "SI" and "SO" respectively, along with all other connections which must be made to create a minimal, working PICAXE system. The 0V and +V legs must be connected to a power supply, the two XT legs must be connected to a crystal or resonator, and RST is the active low Reset line which should be wired via a 4K7 resistor to +V and can be activated by shorting the leg to 0V via a push button.

      08, 08M              18, 18A, 18X             28, 28A, 28X                40X
   .----..----.            .----..----.             .----..----.            .----..----.
  -| +V    0V |-          -|          |-           -| RST      |-          -| RST      |-
2 -| SI    SO |- 7      2 -| SO       |-           -|          |-          -|          |-
  -|          |-        3 -| SI       |-           -|          |-          -|          |-
  -|          |-          -| RST      |-           -|          |-          -|          |-
   `----------'           -| 0V    +V |-           -|          |-          -|          |-
      8-Pin               -|          |-         6 -| SI       |-        6 -| SI       |-
                          -|          |-         7 -| SO       |-        7 -| SO       |-
                          -|          |-           -| 0V       |-          -|          |-
                          -|          |-           -| XT    +V |-          -|       +V |-
                           `----------'            -| XT    0V |-          -|       0V |-
                              18-Pin               -|          |-          -| +V       |-
                                                   -|          |-          -| 0V       |-
                                                   -|          |-          -| XT       |-
                                                   -|          |-          -| XT       |-
                                                    `----------'           -|          |-
                                                       28-Pin               =          =
                                                                           -|          |-
                                                                            `----------'
                                                                               40-Pin

The Download Cable

The following diagram shows how to connect the Download interface to a PC which has a 9-way serial port connector, through a 3-way Molex header. Tech-Supplies Ltd sells a "keyed" 3-way Molex Header, as part number CON038, which is ideal for use as it prevents an appropriately keyed Molex Plug being inserted into the header incorrectly. Tech-Supplies Ltd, also supply a pre-built Download Cable, as part number AXE025, which will connect to the "keyed" 3-way Molex Header wired as below ...

       .---.                                                                  .---------------.
      /    |                  .------.     .----.                             |               |
GND  |   O |--5------.   .----|   ===|  1  | O  |-----------------------------| Serial Out    |
     | O   |         |   |    | .----|     |   ||               .------.      |               |
     |   O |     .---|---|----| | ===|  2  | O ||----------.----|      |------| Serial In     |
     | O   |     |   |   |    | `----|     |   ||          |    `------'      |               |
TX   |   O |--3--'   `---|----|   ===|  3  | O  |---.     _|_      22K     .--| 0v            |
     | O   |             |    `------'     `----'   |    |   |             |  |               |
RX   |   O |--2----------'                          |    |   | 10K         |  `---------------'
     | O   |                                        |    |   |             |        PICAXE
     |   O |                                        |    `-.-'             |
      \    | 9-Way Female                           |      |               |
       `---' Socket (DCE)                         --^------^---------------^-- 0v

Note that there are circumstances where plugging the Molex plug into the header the wrong way round can lead to the PICAXE having its Serial Out line shorted to 0v which may cause irreparable damage to the PICAXE chip. It is therefore recommended that both "keyed" Molex plugs and headers are used to prevent this from happening, and to take special care when using a Molex header or plug which isn't keyed.

The "TX" and "RX" designations on the PC connector refers to the names used by the PC serial interface. TX, "transmit" sends data from the PC and thus connects to the PICAXE's Serial In through the resistor network, and RX, "receive" correspondingly receives data into the PC and thus connects to the PICAXE's Serial Out.

In some documentation and on PICAXE oriented web sites, you may hear of "Serial Out" being referred to also as "TX" or Transmit and "Serial In" as "RX" or "Receive" which can cause confusion as to which "TX" and "RX" being referred to. Sometimes "Serial In" may be called "TX" because it connects to the "TX" from the PC. This can cause further confusion because the Serial In line - implicitly "receiving" is labelled as a "transmit" line used for sending. It is the same when Serial Out is referred to as "RX". My recommendation is to refer to "TX to the PC", "TX to the PICAXE", "RX from the PC" and "RX from the PICAXE" when there is any likely confusion.

If you want to use a 25-way Serial Port connection instead of a 9-way connector, you can either use a 9-to-25 converter ( ADA010 ) or use the wiring as shown below. Note that the functions of pins 2 and 3 on the 25-way connector are different to those on the 9-way connector.

       .---.
      /    |
     |   O |
     =     =
GND  |   O |--7------.                                                        .---------------.
     | O   |         |        .------.     .----.                             |               |
     |   O |         |   .----|   ===|  1  | O  |-----------------------------| Serial Out    |
     | O   |         |   |    | .----|     |   ||               .------.      |               |
     |   O |     .---|---|----| | ===|  2  | O ||----------.----|      |------| Serial In     |
     | O   |     |   |   |    | `----|     |   ||          |    `------'      |               |
     |   O |     |   `---|----|   ===|  3  | O  |---.     _|_      22K     .--| 0v            |
     | O   |     |       |    `------'     `----'   |    |   |             |  |               |
RX   |   O |--3--|-------'                          |    |   | 10K         |  `---------------'
     | O   |     |                                  |    |   |             |        PICAXE
TX   |   O |--2--'                                  |    `-.-'             |
     | O   |                                        |      |               |
     |   O |                                      --^------^---------------^-- 0v
      \    | 25-Way Female
       `---' Socket (DCE)

DCE or DTE ?

The definition of DCE ( Data Communication Equipment ) and DTE ( Data Terminal Equipment ) is fairly complex, but, at the end of the day, boils down to the type of connector used and which pins of that connector are used for the serial input and output lines.

The two circuits above show DCE connections, using female sockets, which allows a PC to be connected using a straight-through ( or 'one-to-one' ) cable with a male plug on one end and a female plug on the other. The Pre-built download connector cable is a DCE oriented connection.

If you wish to use male sockets ( DTE ) instead of female sockets ( DCE ), you will have to use a cable with female plugs at each end, the cable will have to be, what is known as, a 'crossover cable', and the sockets will have to be wired up as below ...

                                                         .---.
                                                        /    |
                                                       |   O |
                                                       =     =
                                                  GND  |   O |--7----------.
                                                       | O   |             |
       .---.                                           |   O |             |
      /    |                                           | O   |             |    .------.
     |   O |--5----------.                             |   O |         .---|----|   ===|  1
     | O   |             |    .------.                 | O   |         |   |    | .----|
     |   O |     .-------|----|   ===|  1              |   O |     .---|---|----| | ===|  3
     | O   |     |       |    | .----|                 | O   |     |   |   |    | `----|
TX   |   O |--3--'   .---|----| | ===|  2         RX   |   O |--3--'   |   `----|   ===|  3
     | O   |         |   |    | `----|                 | O   |         |        `------'
RX   |   O |--2------'   `----|  ====|  3         TX   |   O |--2------'
     | O   |                  `------'                 | O   |
     |   O |                                           |   O |
      \    |  9-Way Male                                \    | 25-Way Male
       `---' Socket (DTE)                                `---' Socket (DTE)

Note that the connections on the Molex now go to different pins on the connectors but that the "TX" and "RX" designations have not changed; this is what can make connecting serial devices so difficult, as was described earlier - For DTE to DCE, TX goes to TX, RX goes to RX, while in DTE to DTE, TX goes to RX, and RX goes to TX. Note that the functions of pins 2 and 3 continue to differ for 9-way and 25-way connections.

Alternative Inter-Connections

The Molex connection can be left out entirely if desired, with the connections going directly to a suitable 9 or 25-way socket, or the Molex connection can be replaced by using 3.5mm Jack Plugs and Sockets with the following wiring ...

                                               1/S   2/R   3/T                .---------------.
.----------.   1/S     2/R   3/T           .--------------------.             |               |
|          |---------.-----.-----.       .-|  .--.  .--.  .--.  |   .---------| Serial Out    |
|          |         |     |      \      | |  |  |  |  |  |  |  |   |         |               |
|          |         |     |      /      | |  |  |  |  |  |  |  |   |  .------| Serial In     |
|          |---------^-----^-----'       `-|  |  |  |  |  |  |  |   |  |      |               |
`----------'    |      |      |            `--|  |--|  |--|  |--'   |  |  .---| 0v            |
                |      |      |               `--'  `--'  `--'      |  |  |   |               |
PC RX ----------'      |      |                |     |     |        |  |  |   `---------------'
                       |      |                `-----|-----|--------'  |  |         PICAXE
PC TX -----------------'      |                      |     |  .-----.  |  |
                              |                      }-----|--|     |--'  |
GND --------------------------'                     _|_    |  `-----'     |
                                                   |   |   |    22K       |
                                               10K |   |   |              |
                                                   |_ _|   |              |
                                                     |     |              |
                                                   --^-----^--------------^-- 0v

Tech-Supplies Ltd sell a PCB mount 3.5mm Jack Socket, as part number CON039, which can be wired as in the circuit below. Note that the Jack Socket is shown as being viewed from above, that is the pins of the socket shown, will be underneath the socket when mounted on a PCB or veroboard.

                                                  .---------------.
 Viewed from Above                                |               |
                             .--------------------| Serial Out    |
  .--------------.           |       .-----.      |               |
.-|  +  +      O |--2/R--.---|---.---|     |------| Serial In     |
| |  .--.      O |--2/R--'   |   |   `-----'      |               |
| |  |  |   O    |--1/S------'   |     22K    .---| 0v            |
| |  `--'      O |--3/T--.      _|_           |   |               |
`-|  +  +      O |--3/T--{     |   |          |   `---------------'
  `--------------'       |     |   | 10K      |         PICAXE
                         |     |   |          |
                         |     `-.-'          |
                         |       |            |
                       --^-------^------------^-- 0v

Tech-Supplies Ltd supply a pre-built Download Cable, as part number AXE026, which has a 3.5mm Jack Plug and will connect to a 3.5mm Jack Socket wired as above. The wiring to the tip, ring and sleeve of the Jack Plug conforms to that used in the pre-built Download Cable.

Note that there are circumstances where plugging this Jack Plug into the Socket can lead to the PICAXE having its Serial Out line shorted to 0v which may cause irreparable damage to the PICAXE chip. Additionally, the TX line from the PC can be shorted to the Serial Output from the PICAXE which may cause irreparable damage to the PICAXE chip, the PC's serial port, or both.

It is therefore recommended that the plug is only inserted or removed when the PICAXE power supply is turned off, or the Jack Plug should only be inserted or removed when the cable is disconnected from the PC's serial port.

My personal recommendation is to use the "keyed" 3-way Molex header described earlier, as this is much easier to install and is usually more accessible when used on a PCB or in a veroboard design, and has none of the above risks; providing that the Molex plug is connected to the header the correct way round.

The Download Interface can be left out of circuits where there will be no need to download programs into the PICAXE, but to ensure correct operation of the PICAXE, it is necessary to wire Serial In to 0v. My recommendation is to fit the few components that are needed just in case it does become necessary to download a later version of the program. This is particularly important if you are soldering the PICAXE into place rather than using a socket.

The PICAXE-08 and PICAXE-08M Download Interface

The PICAXE-08 Download Interface is as described above, with the added complication that Leg 7, Serial Out, can also be used as a user controlled pin once the program download has been completed.

If Leg 7 is to be used as a user controlled pin, then it is best to take the Serial Out line to a 3-way Molex so that a 2-pin Molex Link can be used to route the Leg's output to either the PC serial interface or the electronics it will control. Alternatively, a single pole, single throw ( SPST ) switch can be used.

Always remember to check the Molex Link or switch setting when downloading, as the program download will fail if the setting is wrong.

Download Cable Colour Coding Standards

It is recommended that the following colour coded wiring scheme is used when wiring up your own Download Cables, as this conforms with the manufacturer's standard colour scheme ...

Colour

Function

Molex Pin

Jack

9-Way Pin

25-Way Pin

Orange Serial Out to PC RX in 1 Sleeve 2 3
Red Serial In from PC TX out 2 Ring 3 2
Brown 0v / GND 3 Tip 5 7


The Download Interface Resistor Network

At first glance, the 10K and 22K Resistor Network in the Serial In interface looks wrong; surely, if it's a voltage divider to get 12V down to 5V the 10K ought to be between the Serial In leg and 0V, not the PC end of the 22K ?

If it were a voltage divider, you would be correct. But it isn't.

Microchip PICmicro devices have clamping diodes built in which stop the voltages applied to the pins going much above the power supply or much below 0V, and the 22K resistor is not there to drop the voltage at all, but to limit the amount of current which will flow into the PICAXE leg.

There is a small element of voltage dividing going on between the 22K and the internal resistance between the Serial In leg and 0V, but that is not the primary purpose of the 22K resistor.

The 10K is there to ensure that the Serial In leg is pulled to 0V when the PICAXE runs without a download cable connected. That is its only purpose.

When the PICAXE runs, it monitors the Serial In leg to look for programs being downloaded, and if it were left floating, there is every possibility that it would misinterpret what it sees on the Serial In leg and jump to its internal program download routines rather than execute your previously downloaded program. Pulling the Serial In line down to 0V with the 10K prevents this from happening.

Were the 10K to be placed across the Serial In leg and 0V, then, in conjunction with the 22K, it would act as a voltage divider, and when the circuit was used with serial ports which only output +5V, as is the case with many Laptops, the voltage would be dropped considerably, to a level which would not necessarily be read correctly by the PICAXE, which would make program downloading unreliable.

So, what looks to be a badly designed or wrongly drawn circuit at first glance, turns out to be an ideal solution for most serial interface applications with the PICAXE, and most PICmicro devices in general.


User Controlled Serial Interfacing

The User Controlled Serial Interface is very similar to the Download Interface except that the legs used to receive and transmit serial data can be specified by the user in their program.

The PICAXE-08 and 08M can use up to four of its five I/O lines for serial input ( pins 1, 2, 3 or 4 ) or output ( pins 0, 1, 2 or 4 ), the PICAXE-18, 18A and 18X, can support up to 5 serial inputs and 8 serial outputs, while the PICAXE-28, 28A, 28X and 40X can support up to 8 serial inputs and up to 8 serial outputs.

The PICAXE-08M, 18X, 28X and 40X can also support an additional serial output line using the SERTXD command which will be dealt with later.

The Physical Interface

The nature of the PICAXE, which is actually a pre-programmed PICmicro device, is such that serial interfacing can be accomplished, in most cases, using a minimal number of resistors and without additional active electronics.

Such interfacing is suitable for use with most PC serial ports, and, if it isn't, then it will not be possible to download programs into the device. In my own experimentation, I have not found a single serial port which does not work with this interface, and this has been tested on a number of desktop PC's, battery powered Laptops, PDA's and other devices.

The main difference between this interface and an industry standard RS232 one, is that the PICAXE will only generate 0v and power supply level signals ( normally 5V ) whereas RS232 nominally has an output of -15V/+15V ( although -12V/+12V is usually used ). This issue is discussed further in the "Effects of Power Supply Voltage" section later.

The interface we will use allows any voltage between -15V and +15V to be sent to the PICAXE without any problem.

If the serial port to which you connect a PICAXE will not work with this simple interface, and requires full RS232 compatibility, then you will need to use one of the MAX232-style interface chips to create a usable serial interface. Because most users of the PICAXE will be interfacing to a PC's serial port, and full RS232 compatibility adds considerable complexity to a project, this article does not consider such interfacing, and will only deal with the simpler passive interfacing.

This section deals only with the electrical connections required to create a usable serial interface. Please see the PICAXE Basic Commands documentation for full details of using the SEROUT, SERIN and SERTXD commands for serial communications, and the later section in this article on "Baud Rate and Over-Clocking".

It is recommended that the same "keyed" 3-way Molex header configuration is used as is described in the "Serial Download Interface" earlier, as this allows the same cables used for downloading to be used for User Controlled Serial Interfacing. As with the Download Interface, the Molex connection can be replaced with 3.5mm Jack Plugs and Sockets, or can be omitted entirely with the connections being made directly to 9-way or 25-way PC connectors.

To simplify the circuit diagrams below, only the 9-way socket and Molex connection examples are shown.

Serial Output - SEROUT

Serial output can sent from a PICAXE under user program control by using the SEROUT command. The SEROUT command will allow the program to send serial data out through any of the PICAXE legs that are defined as its Digital Output Pins; for the PICAXE-08 this can be any of its five I/O pins, and for others, any of the the Digital Outputs.

Only normally designated Digital Outputs can be used to send serial data; which means that Port A and Port C on the PICAXE-28X, and Port A, Port C and Port E on the PICAXE-40X, cannot be used to transmit serial data.

The circuit and connections necessary to send serial out using SEROUT are as shown in the diagram below ...

       .---.                                                  180R            .---------------.
      /    |                  .------.     .----.           .------.          |               |
GND  |   O |--5------.   .----|   ===|  1  | O  |-----------|      |----------| Digital Out   |
     | O   |         |   |    | .----|     |   ||           `------'          |               |
     |   O |         |   |    | | ===|  2  | O ||                             |               |
     | O   |         |   |    | `----|     |   ||                             |               |
     |   O |         `---|----|   ===|  3  | O  |---.                      .--| 0v            |
     | O   |             |    `------'     `----'   |                      |  |               |
RX   |   O |--2----------'                          |                      |  `---------------'
     | O   |                                        |                      |        PICAXE
     |   O |                                        |                      |
      \    | 9-Way Female                         --^----------------------^-- 0v
       `---' Socket (DCE)

The astute will have noticed that this circuit is almost exactly the same as that used to connect Serial Out in the Download Interface, with the addition of the 180R resistor.

Those of you who are familiar with the PICmicro, which is what a PICAXE is at its heart, will be wondering why the 180R needs to be included when the leg used for User Controlled Serial Interfacing is electrically no different to the Serial Out leg.

The answer is, that I don't know, but both circuits are those specified by the manufacturer's data sheet. Because the legs used are electrically identical, and so is the serial port to which they can be connected, it would be expected that if the 180R is necessary here then it would also be necessary in the Download Interface, and if it's not necessary there, then it needn't be a necessity here.

Because the legs are electrically identical, those who are concerned over the discrepancy should be able to fit a 180R resistor in the Serial Out line of the Download Interface without causing any adverse effects.

Serial Input - SERIN

Serial input can be received by a PICAXE under user program control by using the SERIN command. The SERIN command will allow the program to receive serial data through any of the PICAXE legs that are defined as its Digital Input Pins; for the PICAXE-08 and 08M this can be any of its five I/O pins, and for others, any of the Digital Inputs.

Only normally designated Digital Inputs can be used to receive serial data; which means that Port A and Port C on the PICAXE-28X, and Port A, Port C and Port E on the PICAXE-40X, cannot be used to receive serial data.

The circuit and connections necessary to receive serial in using SERIN are as shown in the diagram below ...

       .---.
      /    |                  .------.     .----.
GND  |   O |--5------.        |   ===|  1  | O  |                             .---------------.
     | O   |         |        | .----|     |   ||               .------.      |               |
     |   O |     .---|--------| | ===|  2  | O ||----------.----|      |------| Serial In     |
     | O   |     |   |        | `----|     |   ||          |    `------'      |               |
TX   |   O |--3--'   `--------|   ===|  3  | O  |---.     _|_      22K     .--| 0v            |
     | O   |                  `------'     `----'   |    |   |             |  |               |
     |   O |                                        |    |   | 10K         |  `---------------'
     | O   |                                        |    |   |             |        PICAXE
     |   O |                                        |    `-.-'             |
      \    | 9-Way Female                           |      |               |
       `---' Socket (DCE)                         --^------^---------------^-- 0v

Serial Input to a PICAXE-08

If you require Serial Input to a PICAXE-08 and want to use Leg 3 you need to add a small signal diode, such as a 1N4148, between Leg 3 and +V, the power supply to the PICAXE, as shown in the diagram below ..

                                    --.-------.-- +V
                                      |       |
                                    __|__ +   |
                            1N4148   / \      |   .---------------.
   .----.                           /   \     |   |               |
1  | O  |                           --.--     `---| +V            |
   |   ||               .------.      |           |               |
2  | O ||----------.----|      |------^--------3--| Digital In    |
   |   ||          |    `------'                  |               |
3  | O  |---.     _|_      22K                .---| 0v            |
   `----'   |    |   |                        |   |               |
            |    |   | 10K                    |   `---------------'
            |    |   |                        |       PICAXE-08
            |    `-.-'                        |
            |      |                          |
          --^------^--------------------------^-- 0v
This diode is required because the electrical design of Leg 3 on the PICAXE-08 is different to its other legs, and different to legs on the other PICAXE processors.

If you are using any leg other than Leg 3 on a PICAXE-08, or using a PICAXE other than a PICAXE-08, this diode is not necessary.

Serial Input and Output

The above descriptions and circuits show how Serial Input and Serial Output can be done separately, but in many cases, it will be desirable to have both Serial Input and Serial Output connections between the PC and the PICAXE. This can be achieved by simply combining the two circuits above together, as below ...

                                                                  180R
       .---.                                                    .------.      .---------------.
      /    |                  .------.     .----.          .----|      |---.  |               |
GND  |   O |--5------.   .----|   ===|  1  | O  |----------'    `------'   `--| Digital Out   |
     | O   |         |   |    | .----|     |   ||               .------.      |               |
     |   O |     .---|---|----| | ===|  2  | O ||----------.----|      |------| Digital In    |
     | O   |     |   |   |    | `----|     |   ||          |    `------'      |               |
TX   |   O |--3--'   `---|----|   ===|  3  | O  |---.     _|_      22K     .--| 0v            |
     | O   |             |    `------'     `----'   |    |   |             |  |               |
RX   |   O |--2----------'                          |    |   | 10K         |  `---------------'
     | O   |                                        |    |   |             |        PICAXE
     |   O |                                        |    `-.-'             |
      \    | 9-Way Female                           |      |               |
       `---' Socket (DCE)                         --^------^---------------^-- 0v

If you require Serial Input to a PICAXE-08 and want to use Leg 3 you need to add a small signal diode, such as a 1N4148, between Leg 3 and +V, the power supply to the PICAXE, as described earlier.

You may notice the remarkable similarity of this circuit diagram to that of the Download Interface, and apart from the addition of the 180R resistor in the PC's RX connection, and the use of legs other than Serial In and Serial Out, they are effectively the same.

Serial Output - SERTXD

In addition to the User Controlled Serial Output which can be sent through any of the Digital Outputs using the SEROUT command, the PICAXE-08M, 18X, 28X and 40X have a SERTXD command which allows serial data to be sent, under user program control, through the Serial Out leg part of the Download Interface.

In order to receive the data sent using SERTXD, the PC serial Port should simply be connected to the Download Interface.

Although data can be sent through the Serial Out leg, it is not possible to receive serial data through the Serial In leg when the user program is running; there is no SERRXD command, and it is recommended that data is not sent to the Serial In leg while the PC serial port is connected to the Download Interface.

Whereas the baud rate of the serial output transmitted through a Digital Output can be chosen as required, the baud rate of tha transmitted through the Serial Out leg using SERTXD is fixed at 4800 for 4MHz operation, 9600 for 8MHz operation, and at 19200 for 16MHz operation.

Serial data transmitted through the Serial Out leg still uses the same one start bit, 8 data bits and one stop bit as all other serial transmissions from the PICAXE use.

Data Format

Although not part of the electronic interface, the data format of serial communications is an important part of the protocol relating to serial interfacing.

The PICAXE can transmit and receive at a variety of baud rates, and can transmit and receive on entirely different baud rates, however, for PC communications, it is almost a certainty that you will want to transmit and receive at the same baud rate when connected to a single PC serial port.

The baud rate specifications used in the SERIN and SEROUT commands also specify the polarity of the serial output; that is, in its most simplistic form, whether the serial output line is normally kept at 0v or at the power supply voltage when nothing is being sent. In order to communicate with the PC the polarity of the serial output must be set to what the PC expects, and using the interface we have described in this article it must be one of the "N" ( Inverted Polarity - "Not" ) baud rates; N300, N600, N1200, N2400 or N4800. These must be used in both the SERIN and SEROUT commands, rather than the "T", "ON" or "OT" baud rates.

While the baud rate chosen can be selected by the user, the actual data format cannot; serial communications is always performed using 8 data bits, no parity bit and 1 stop bit, and your PC application must be configured to use the same settings.

The PICAXE will be able to receive data from a serial port configured to send 1, 1.5 or 2 stop bits, but you may need to add delays after every SEROUT or SERTXD command, and send every byte individually, for the PC to receive the data correctly.

While the SEROUT command, which sends serial data from a Digital Output Pin, can have the baud rate to use chosen and specified, the SERTXD command, which sends serial data through the Serial Out leg used during program download, has its baud rate fixed and cannot be changed except by running the PICAXE at a higher speed than normal. Please see the later section on "Baud Rate and Over-Clocking" for further details.

Handshaking

The PICAXE does not require "handshaking" on the Download Interface as this is all handled by the Program Editor, however connection to serial ports for User Controlled Serial may require handshaking; signalling between each side of the interface that there is data ready to be transmitted, and indicating that data can be sent.

This handshaking can be done either by hardware or in software. The PICAXE supports neither; if you want to use handshaking then you must implement it yourself. As this requires considerable knowledge of serial interfacing and the associated protocols, this will not be dealt with in this article.

Most serial interfaces can be configured to use no handshaking, neither hardware nor software ( commonly called "XON/XOFF" ), however some may still require the hardware handshaking lines to be connected; the Amstrad NC100 Notepad computer is a particular case in point.

In order to make a serial port usable with a PICAXE, it may be necessary to make a link between the RTS ( Ready To Send ) and CTS ( Clear To Send ) lines on the serial port connector. It may also be necessary in some cases to connect the DTR ( Data Terminal Ready ) line to the DSR ( Data Set Ready ) and DCD ( Data Carrier Detect ) lines.

The additional wiring which may be required on the serial port connectors is given in the diagram below. The connections are the same whether female ( DCE ) or male ( DTE ) sockets are used ...

                                         .------------------.
                                         |                  |
                                         |        .---.     |
                                         |       /    |     |
                                         |      |   O |     |
                                         |      =     =     |
                                         |      |   O |<-8--{  DCD
                                    DTR  `--20--| O   |     |
             .---.                              |   O |     |
            /    |                              | O   |     |
           |   O |                              |   O |<-6--'  DSR
           | O   |                              | O   |
           |   O |--4--.  DTR                   |   O |<-5--.  CTS
CTS  .--8->| O   |     |                        | O   |     |
     |     |   O |     |                        |   O |--4--'  RTS
RTS  `--7--| O   |     |                        | O   |
           |   O |     |                        |   O |
DSR  .--6->| O   |     |                        | O   |
     |     |   O |<-1--{  DCD                   |   O |
     |      \    |     |                    14  | O   |
     |       `---'     |                        |   O |  1
     |                 |                         \    |
     `-----------------'                          `---'

Although it may not be necessary to include all the hardware handshaking lines bove, it does no harm to include them anyway, and it is recommended that all handshaking connections are made to allow the PICAXE to connect to all types of serial interfaces.

Without handshaking between the PICAXE and the other device, each may send data whenever it wants to, regardless of whether the receiver is ready for it or not. It is therefore important that both sides are ready to receive data when it is sent, or the sent data will either be missed or incorrect data will be received.

This is particularly important for the PICAXE which is a relatively slow device, and may not be ready to receive more data until it has finished processing the data it last received. Data bytes sent "back-to-back", with no delays between the data sent, will prove particularly problematic for the PICAXE, especially at higher baud rates.

It is usually possible to configure a PC's serial port to buffer incoming characters, so they are not lost no matter what else the PC may be doing, but this is not possible with the PICAXE.

The only solution to avoiding lost or corrupted data is to make sure that the PC does not send serial data to the PICAXE until it is ready for it, either by implementing a handshaking protocol in hardware or software, or by adding delays between sending data from the PC.

The simplest mechanism is to have the PICAXE send a "Ready" data byte to the PC to indicate that it is ready for a new character, with a SERIN command immediately afterwards.


Combined Download and User Controlled Serial Interface

Sometimes it is necessary, or convenient, to use a single PC serial interface to perform program downloads and to be used for communicating with the program once it is running. This could be done by unplugging the serial cable from the Download Interface and into another Serial Interface, but can often be achieved by using a simple switch. Note that you will still need two sets of resistor networks for both the Download Interface and the user serial interface.

                                                                     .---------------.
                                                                     |               |
                    O------------------------------------------------| Serial Out    |
           .----O /                                    .------.      |               |
           |      . O-------.   .-------.--------------|      |------| Serial In     |
           |      .         |   |       |     180R     `------'      |               |
  .----.   |      .     .---|---'       |   .------.     22K         |               |
1 | O  |---'      .     |   `-----------|---|      |-----------------| Digital Out   |
  |   ||          . O---'               |   `------'   .------.      |               |
2 | O ||--------O /        .------------|---------.----|      |------| Digital In    |
  |   ||            O------'            |         |    `------'      |               |
3 | O  |---.                           _|_       _|_     22K      .--| 0v            |
  `----'   |                          |   |     |   |             |  |               |
           |        O Download        |   | 10K |   | 10K         |  `---------------'
           |    O /                   |   |     |   |             |        PICAXE
           |        O User Serial     `-.-'     `-.-'             |
           |                            |         |               |
         --^----------------------------^---------^---------------^-- 0v

If you are using SERTXD for user controlled serial output, then the interface is even simpler ...

                                                                     .---------------.
                                                                     |               |
           .---------------------------------------------------------| Serial Out    |
           |                                           .------.      |               |
           |            .---------------.--------------|      |------| Serial In     |
  .----.   |            |               |              `------'      |               |
1 | O  |---'            |               |                22K         |               |
  |   ||            O---'               |              .------.      |               |
2 | O ||--------O /        .------------|---------.----|      |------| Digital In    |
  |   ||            O------'            |         |    `------'      |               |
3 | O  |---.                           _|_       _|_     22K      .--| 0v            |
  `----'   |                          |   |     |   |             |  |               |
           |        O Download        |   | 10K |   | 10K         |  `---------------'
           |    O /                   |   |     |   |             |        PICAXE
           |        O User Serial     `-.-'     `-.-'             |
           |                            |         |               |
         --^----------------------------^---------^---------------^-- 0v

Always remember to check the switch setting when downloading, as the program download will fail if the setting is wrong.


Serial Interfacing for Multiple PICAXE's

A single serial output from a PC can be used to communicate with a number of PICAXE's simply by wiring all the user serial input lines together.

Any number of PICAXE's can send user serial output to a PC by 'diode mixing' the user serial output lines together.

Any low power signal diode, such as the 1N4148, can be used to perform this 'diode mixing', and the technique has been proven to work, however it may not work with all PC serial ports or those on other devices. In some cases it may be necessary to use pull-up or pull-down resistors to make the interface work. These will have to be carefully chosen, to ensure that damage is not caused to the device that is being connected to.

Note that all PICAXE's must be configured to use the same baud rates and each must be able to determine whether any incoming serial data is for itself or another PICAXE. Every PICAXE must also make sure that it does not send out any serial data while another PICAXE is doing so.

Preventing serial output clashes, which would lead to the PC receiving corrupt data, can be accomplished by either using hardware arbitration to determine which can access the serial interface - each PICAXE requests access and some other hardware ( possible another PICAXE ) signals that the requesting PICAXE has gained access - or by only sending data in response to serial input received, which puts the onus on scheduling serial transfers upon the PC program which communicates with the PICAXE's.

                        |                   |   |   .---------------.
                        |      |/|          |   |   |               |
                        }------< |----------|---|---| Digital Out   |
                        |      |\|          |   |   |               |
                        |     1N4148        }---|---| Digital In    |
                        |                   |   |   |               |
                        |                   |   }---| 0v            |
                        |                   |   |   |               |
                        |      |/|          |   |   `---------------'
                        }------< |------.   |   |       PICAXE #2
               180R     |      |\|      |   |   |   .---------------.
   .----.    .-----.    |     1N4148    |   |   |   |               |
1  | O  |----|     |----'               `---|---|---| Digital Out   |
   |   ||    `-----'         .------.       |   |   |               |
2  | O ||---------------.----|      |-------^---|---| Digital In    |
   |   ||               |    `------'           |   |               |
3  | O  |---.          _|_      22K             }---| 0v            |
   `----'   |         |   |                     |   |               |
            |         |   | 10K                 |   `---------------'
            |         |   |                     |       PICAXE #1
            |         `-.-'                     |
            |           |                       |
          --^-----------^-----------------------^-- 0v

Multiple Serial Interfacing with PICAXE-08's

If you are supplying serial input into Leg 3 of a PICAXE-08, you may need to add a diode between Leg 3 and +V as described earlier in the "User Controlled Serial Interface" section.

Only one diode will be required no matter how many PICAXE-08's are used, and it may be left out if the serial input also goes to any other leg than Leg 3 on any PICAXE-08 or is connected to any PICAXE other than a PICAXE-08, however, if the PICAXE-08 using Leg 3 for serial input is ever to be the only chip installed then a diode must be used.

The diode can be any small signal diode, such as a 1N4148, and fitting it, even if not required, will not adversely affect the circuit's operation.


Serial Links Between PICAXE's

If you want to use serial interfacing to connect two or more PICAXE's together, you do not need to use the circuits which are required to interface to a PC serial port; it is a simple matter of connecting one PICAXE's Digital Output to Digital Inputs on the ones you wish to send serial data to.

A single PICAXE can send serial data to multiple PICAXE's by simply connecting the Digital Output from the PICAXE which is transmitting to Digital Inputs on those PICAXE's which are receiving, as shown below ...

                                           |   |   .---------------.
                                           |   |   |               |
                                           }---|---| Digital In    |
                                           |   |   |               |
                                           |   }---| 0v            |
                                           |   |   |               |
                                           |   |   `---------------'
                                           |   |
.---------------.                          |   |   .---------------.
|               |                          |   |   |               |
|   Digital Out |--------------------------^---|---| Digital In    |
|               |                              |   |               |
|            0v |---.                          }---| 0v            |
|               |   |                          |   |               |
`---------------'   |                          |   `---------------'
                    |                          |
                  --^--------------------------^-- 0v

Multiple PICAXE's can communicate serially by using 'diode mixing' as was described earlier with multiple PICAXE's communicating with a PC serials port, as shown in the circuit below ...

.---------------.   |            |         |   |   .---------------.
|               |   |    |\ |    |         |   |   |               |
|   Digital Out |---|----| >|----{         }---|---| Digital In    |
|               |   |    |/ |    |         |   |   |               |
|            0v |---{   1N4148   |         |   }---| 0v            |
|               |   |            |         |   |   |               |
`---------------'   |            |         |   |   `---------------'
                    |            |         |   |
.---------------.   |            |         |   |   .---------------.
|               |   |    |\ |    |         |   |   |               |
|   Digital Out |---|----| >|----^----.----^---|---| Digital In    |
|               |   |    |/ |        _|_       |   |               |
|            0v |---{   1N4148      |   |      }---| 0v            |
|               |   |            ?K |   |      |   |               |
`---------------'   |               |   |      |   `---------------'
                    |               `-.-'      |
                    |                 |        |
                  --^-----------------^--------^-- 0v

It is also possible to create a 'multi-drop' network, where every PICAXE can communicate with every other using a single common line ...

.---------------.   |            |         |            |   .---------------.
|   Digital In  |---|------------{         }------------|---| Digital In    |
|               |   |    |\ |    |         |    | /|    |   |               |
|   Digital Out |---|----| >|----{         }----|< |----|---| Digital Out   |
|               |   |    |/ |    |         |    | \|    |   |               |
|            0v |---{   1N4148   |         |   1N4148   }---| 0v            |
|               |   |            |         |            |   |               |
`---------------'   |            |         |            |   `---------------'
                    |            |         |            |
.---------------.   |            |         |            |   .---------------.
|               |   |            |         |            |   |               |
|   Digital In  |---|------------{         }------------|---| Digital In    |
|               |   |    |\ |    |         |    | /|    |   |               |
|   Digital Out |---|----| >|----^----.----^----|< |----|---| Digital Out   |
|               |   |    |/ |        _|_        | \|    |   |               |
|            0v |---{   1N4148      |   |      1N4148   }---| 0v            |
|               |   |            ?K |   |               |   |               |
`---------------'   |               |   |               |   `---------------'
                    |               `-.-'               |
                    |                 |                 |
                  --^-----------------^-----------------^-- 0v


Baud Rates and Over-Clocking

All PICAXE's nominally operate at 4MHz, using either an on-chip "RC" oscillator set to 4MHz, or by using an external 4MHz resonator or crystal.

At 4Mhz, the PICAXE-08, 08M, 18, 18A, 28 and 28A support baud rates of 300, 600, 1200 and 2400, and the PICAXE-18X, 28X and 40X support baud rates of 600, 1200, 2400 and 4800. The PICAXE-08M, 18X, 28X and 40X also support the SERTXD command which transmits serial data at 4800 baud.

For those PICAXE's which can have their operating speeds increased - in the case of the 08M, 18A and 18X by including the SETFREQ command, and for the PICAXE-28, 28A, 28X and 40X, by replacing the 4MHz resonator or crystal with an 8MHz or 16MHz part - the baud rates available are those available at 4MHz, doubled for 8MHz operation and quadrupled at 16MHz. The baud rate used for the SERTXD command will be correspondingly increased for those PICAXE's which support it.

The following table shows the baud rates available for use with each variant of the PICAXE range ...

SERIN / SEROUT SERTXD
4MHz

8MHz

16MHz

4MHz

8MHz

16MHz

PICAXE-08 300, 600,
1200, 2400

- - - - -
PICAXE-08M 300, 600,
1200, 2400

600, 1200,
2400, 4800
[1]

- 4800 9600
[1]
-
PICAXE-18 300, 600,
1200, 2400

- - - - -
PICAXE-18A 300, 600,
1200, 2400

600, 1200,
2400, 4800
[2]

- - - -
PICAXE-18X 600, 1200,
2400, 4800

1200, 2400,
4800, 9600
[1]

- 4800 9600
[1]
-
PICAXE-28 300, 600,
1200, 2400

600, 1200,
2400, 4800
[3]

1200, 2400,
4800, 9600
[3]

- - -
PICAXE-28A 300, 600,
1200, 2400

600, 1200,
2400, 4800
[3]

1200, 2400,
4800, 9600
[3]

- - -
PICAXE-28X 600, 1200,
2400, 4800

1200, 2400,
4800, 9600
2400, 4800,
9600, 19200
4800 9600 19200
PICAXE-40X 600, 1200,
2400, 4800
1200, 2400,
4800, 9600
2400, 4800,
9600, 19200
4800 9600 19200

[1] By using "SETFREQ"
[2] By poking OSCCON - Not recommended by manufacturer !
[3] By changing crystal/resonator - Not recommended by manufacturer !

Although it is possible to achieve relatively high baud rates by increasing the operating speed, the manufacturers do not guarantee correct serial communications at baud rates higher than those provided for 4MHz operation, and add that, "A maximum of 4800 is recommended for complex serial transactions".

The reliability of high speed serial communications depends on a number of factors, especially the stability of the on-chip oscillator which controls the operating speed of the PICAXE. For the PICAXE's which use resonators or crystals, the operating speed will be relatively stable, but those parts which are controlled by an on-chip "RC" oscillator will be less stable, and will be prone to variations in operating speed due to fluctuation of temperature; see the "Effects of Temperature" section below for further details.

Specifying the Baud Rate Required

Although you may have to tell the Programming Editor used to develop and download programs into a PICAXE what the operating speed of the PICAXE is, the operating speed is not taken into account when the SERIN and SEROUT commands are used; specifying a baud rate of "N1200" will generate 1200 baud at 4MHz, 2400 baud at 8MHz and 4800 baud at 16MHz. To actually use 4800 baud at 16Mhz, you would have to specify a baud rate of "N1200".

To avoid problems where the program you are using may be run on PICAXE's with different operating speeds, but you want to use the same baud rate regardless, it is recommended that you create your own 'User Defined Symbols' which specify the baud rate actually required, and are set to the approriate in-built baud rate specifiers dependant upon operating speed. For example -

A PICAXE-28X operating at 4MHz could have the following definitions included ...

    SYMBOL BAUD600   = N600
    SYMBOL BAUD1200  = N1200
    SYMBOL BAUD2400  = N2400
    SYMBOL BAUD4800  = N4800

A PICAXE-28X operating at 8MHz would then have the following definitions included ...

    SYMBOL BAUD1200  = N600
    SYMBOL BAUD2400  = N1200
    SYMBOL BAUD4800  = N2400
    SYMBOL BAUD9600  = N4800

A PICAXE-28X operating at 16MHz would then have the following definitions included ...

    SYMBOL BAUD2400  = N600
    SYMBOL BAUD4800  = N1200
    SYMBOL BAUD9600  = N2400
    SYMBOL BAUD19200 = N4800

Any SERIN or SEROUT command which specifies its baud rate by using one of the defined symbols will therefore use that baud rate no matter what the operating speed is, providing that the symbol is properly defined for the operating speed.

It also means that should you change the operating speed of the PICAXE that your program is running on, you only need to change the baud rate definitions and will not have to search through your program checking that every SERIN and SEROUT command has been correctly specified.

For example ...

    SEROUT LCD, BAUD2400, ( 254, 1, "Hello World !" )

will always send serial data at 2400 baud, no matter what the PICAXE operating speed is, with BAUD2400 appropriately defined.


The Effects of Power Supply Voltage

Because the PICAXE can only drive its Serial Out and Digital Output legs at 0v or at its power supply level, the ability to communicate serially with another device depends upon that device accepting the voltage levels as valid and meaningful.

To provide full RS232 interfacing the PICAXE would need additional electronics to output -15V/+15V voltage levels, but most serial ports will accept any voltage at or below 0v to be the equivalent of -15V and a reasonable positive voltage to be equivalent to +15V; this is what makes the simplistic interface successful in the great majority of cases.

As the supply voltage to the PICAXE drops though, so too does the voltage which the PICAXE can put out on Serial Out and its Digital Output legs and this may become too low for the PC's serial port to recognise properly. This may cause any number of apparent faults with serial communications which may also be intermittent in nature.

The voltage at which difficulties may occur is hard to predict as it is very much dependent upon the serial port to which the PICAXE is connected. Some may accept very low voltages, while others may not even work when the PICAXE is delivering a full 5V.


The Effects of Temperature

While the operating speed of the PICAXE-28, 28A, 28X and 40X is fairly stable across a wide temperature range through the use of a resonator or crystal controlled oscillator used for timing control, the PICAXE-08, 08M, 18, 18A and 18X are implicitly less stable because of their use of a simple internal "RC" oscillator and will be more severely affected by changes in temperature.

The operating speed of the internal" oscillator parts will drift as the temperature changes, whether they are changes in the ambient temperature or through self-heating while operating. The proximity of other components which may be warming up during operation, especially voltage regulators, may also heat up the PICAXE. It is fairly likely that the operating speed will change when a PICAXE is powered-up until it reaches a nominal operating temperature.

As the operating speed drifts then so will the accuracy of the timing of the serial output which the PICAXE is generating, and its ability to accurately read any serial input will be correspondingly affected. The problem can be compounded if the devices at each end of the serial link are drifting in opposite directions, possibly doubling the effect of the error.

Serial communications with a PICAXE are all done using one start bit, 8 data bits and one stop bit; 10 bits in total, with the bits being sent in that order. Should the PICAXE slow down by 6%, when the PC receiving this serial data comes to be reading the last data bit the PICAXE will actually be sending out the last but one data bit. This will result in PC accepting corrupted data or the data being ignored if the following stop bit is not of the correct value. The late arrival of the stop bit itself may also lead to another, non-existent, byte of data being received or further errors, and discarded data.

The resultant affect of temperature change may therefore be that the PICAXE no longer sends valid serial data or is unable to receive it. With large enough drift, it may become impossible to use serial data no matter what baud rate is selected.

It should be noted that this problem affects not just User Controlled Serial Interfaces but also the Program Download Interface.


PICAXE is a trademark of Revolution Education Ltd. PICmicro is a registered trademark of Microchip Inc.





Associated Articles

  The PICAXE Processors
  PICAXE News
  PICAXE Questions & Answers
  PICAXE Comparisons
  PICAXE Pinouts
  PICAXE Serial Interfacing
  PICAXE Infra-Red Interfacing
  PICAXE Wireless Interfacing
  PICAXE LCD Interfacing
  PICAXE LCD Interfacing
  A Real-Time Clock for the PICAXE-18X
  PICAXE Optimisations
  The PICAXE Birthday Box Project
  PICAXE Telephone Exchange Simulator
  The Brainf**ked PICAXE
  The PICAXE Extended Programming Interpreter
  Build Your Own Basic Stamp
  Tech Toys



Sites to Visit

  PICAXE Home Page
  Revolution Education Ltd

  Tech-Supplies Ltd



Site Navigation

  Home Page
  What's New
  Search
  Add Bookmark
  Have Your Say
  Guestbook




First published on Tuesday the 2nd of December, 2003 at 17:09:10
Last upload was on Wednesday the 12th of January, 2005 at 14:36:16