PICAXE

From Wikipedia, the free encyclopedia

PICAXE is the name of a UK-sourced microcontroller system based on a range of Microchip PICs. There are 11 PICAXE variants of differing pin counts from 8 to 40 pins. Initially marketed for use in education and by electronics hobbyists, they are also used in commercial and technical fields, including rapid prototype development. All use pre-loaded factory bootstrap interpretation code to allow user generated programs to be downloaded using a simple RS-232 serial connection.

Contents

[edit] Hardware

Based upon a variety of Microchip PICs, the chips come in a number of DIP footprints, from an 8-pin, 128-byte program capacity device through to a 40-pin, 4096-byte program capacity device. With their DIP footprint they are suited for use with solderless breadboards and more traditional PCBs designs, although surface mount versions are available.

The 8-pin PICAXE-08M is priced below GB£2 and is often chosen as an entry-level option as it lends itself to easy prototyping.

All current devices, use an internal 4/8 MHz oscillator, and hence require few components to create a basic hardware platform.

The current recommended parts, as of April 2008, are 08M, 14M, 20M 18X, 28X1, 40X1

The 28, 28A, 28X and 40X have been discontinued as they have been superseded by the X1 parts. The 08, 18, 18A are still available, but not recommended for new designs.

[edit] Minimal configurations

All current devices require nothing more than the connection of power and configuration of the Serial In pin used for downloading.

The 18-pin range requires a pull-up resistor from the Reset pin and the 28 and 40X require the connection of a resonator or crystal.

[edit] Power supply requirements

Power supply voltages are very flexible allowing operation from batteries or regulated power supplies.

[edit] Low power modes

There are a number of commands (SLEEP, NAP, HIBERNATE, DOZE) to put the device into low power operating modes in order to conserve power and extend operational lifetime when powered by batteries.

Many of the variants have controllable clocks, allowing for operation below their nominal operating frequencies, to achieve minimal lower power consumption. Execution speed can be controlled by the user program.

[edit] Non-volatile data storage

All devices contain non-volatile data memory which allows data to be stored and recovered when power is removed. Access to the non-volatile data memory is through the use of the READ and WRITE commands and, on the 28A, the READMEM and WRITEMEM commands can also be used.

The amount of non-volatile data memory available depends upon the device:

  • The 08 and 18 have 128 bytes of EEPROM which is used to store both downloaded program and for non-volatile data memory use.
  • The 08M, 14M, 20M have 256 bytes of EEPROM which is used to store both downloaded program and for non-volatile data memory use.
  • The 18A and 18X have 256 bytes of EEPROM exclusively available for non-volatile data memory use.
  • The 28 has 64 bytes of EEPROM exclusively available for non-volatile data memory use.
  • The 28A has 64 bytes of EEPROM exclusively available for non-volatile data memory use and also has 256 bytes of Flash memory which may be used for non-volatile data storage using the READMEM and WRITEMEM commands.
  • The 28X and 40X have 128 bytes of EEPROM exclusively available for non-volatile data memory use.
  • The 28X1 and 40X1 have double program and variable memory and additional 128 bytes scratch pad memory. It has also many new features for IO etc.

[edit] Software

All programming development is done using the Programming Editor. This free (in the meaning of price) software download (~33MB with [1]) supports the entire development process from source code editing to program downloading. Only Microsoft Windows 98 or later is supported. Historically a serial port was required to download programs, however a USB download cable is now available from the developers. The cable incorporates a USB-serial adapter molded into its plug, eliminating the need for a separate adapter for users of modern USB-only PCs.

The Programming Editor also includes a number of Wizards which aid in program and project development.

Editing can be done in plain text or RTF with color syntax highlighting. Source code can be created outside the Programming Editor and imported for downloading. Programs can be created in a Basic-like language or by using a visual flowcharting tool. The Programming Editor also supports PICmicro Assembly Language programming.

[edit] Simulator

The Programming Editor software includes a comprehensive line by line on-screen simulation of the BASIC program. This enables users to step through the program on-screen, to watch the program in operation, and help identify any programming errors.

[edit] Tune wizard

This Wizard is used to create TUNE commands which can be used with the 08M and 14M. The Wizard includes the ability to import suitable mobile telephone ring tones and convert them to appropriate TUNE commands.

[edit] Serial LCD CGRAM wizard

This Wizard provides a visual means to create custom defined characters to be used with serial LCD displays. The desired character is specified by selecting pixels which will be displayed on a 7x5 grid and the necessary command to program that character within the LCD is generated.

[edit] Datalogger wizard

This Wizard is used to generate datalogging programs.

[edit] PICAXE connect wizard

This Wizard allows the configuration of MaxStream XBee (ZigBee) modules to be configured for use.

[edit] PICAXE net server wizard

This Wizard allows configuration of the PICAXE.net Web Server product. The Wizard provides the ability to create and upload page images and to upgrade the web server firmware.

[edit] Programming language

The programming language is BASIC-like and very similar to that used by the Basic Stamp 1 (BS1). Most programs written for the BS1 should be easily convertible for use. The most notable difference is that the BS1's POT command has been replaced by the READADC command which allows an analogue voltage to be read directly.

The programming language includes high-level support for underlying processor capabilities and additional functionality for various devices. All variants support a common core programming command set but not all support all commands.

[edit] Variables

The programming language provides 28 bytes (X1 parts) or 14 bytes readily available to the programmer. This memory can be manipulated in measures of bits (bit0-15), bytes (b0-13), and 16-bit words (w0-6). The bit, byte, and word variables all overlap in the same 28/14 bytes. Every two byte variables overlap with a word variable, so b0 & b1 make up w0, b2 & b3 compose w1, and so on. The bit values bit1 - bit7 overlap b0, and bit8 - bit15 overlap b1 (which also covers w0). This can be used to carve apart variables, or simply make the most efficient use of the limited memory given.

Variables can be given meaningful names for use within a program through use of the SYMBOL directive. The SYMBOL directive can also be used to create named constants.

In addition to the pre-defined variables, all but the 08 have access to the internal SFR (Special Function Registers) of the PICmicro they are based upon, allowing much of the unused SFR to be used as Random Access Memory (RAM) during execution. This can be used for temporary storage of variable values (using PEEK and POKE) and allows re-use of variables within subroutines and other code sections. Most parts have a 32 byte span free for use.

The PEEK and POKE commands can also be used to implement byte arrays and software stacks.

The X1 parts also have an extra 128 bytes of 'scratchpad' memory available. This can be accessed directly (PUT and GET commands) or indirectly via the scratchpad pointer '@ptr'.

The READ and WRITE commands, which allow data to be stored in and retrieved from non-volatile EEPROM memory, can be used to retain data and settings while powered down.

[edit] Arithmetical manipulation

All arithmetical operations are performed using 16-bit, unsigned, positive only operations. Variables are expanded as required to 16-bits on use by leading zero padding and results of processing are stored by truncating the resultant value to an appropriate number of bits before storage. A byte variable will have the eight least significant bits of the result stored, a bit variable will be set to the least significant bit value of the result.

Care must therefore be taken when manipulating variables and values to consider the effect of wrap-round, underflow and overflow. In particular it must be noted that a value can never be less than zero, and a byte value can never exceed 255. These issues must be considered in the implementation of FOR-NEXT loops and other looping constructs, which may, under some circumstances, never meet their terminating conditions. There is no facility to automatically report or indicate warp-round, underflow or overflow.

All arithmetical expressions in assignments (LET) are evaluated in a strictly left-to-right manner. There is no operator precedence but on the 28X1 it is possible to use bracketing to control calculations

[edit] Program flow control

A number of control constructs are provided to handle program flow -

  • GOTO - Redirects program execution to another location in the program.
  • IF-THEN - Not the usual "IF condition THEN statement". The only statement IF/THEN takes is a Label: , which it does a "GOTO" when the condition is true. IF/THEN falls to the next line when the condition is false.
  • BRANCH - Conditionally redirects program execution to one of a number of locations in the program (same as ON-GOTO).
  • GOSUB - Redirects program execution to another location in the program, then continues after the GOSUB command following the execution of a RETURN in the subroutine.
  • RETURN - Returns program execution to after the most recent GOSUB.
  • SETINT - Enables interrupts and automatically redirects program execution to an interrupt handler when an interrupt is detected. Program execution continues from where the program was diverted from upon execution of a RETURN within the interrupt handler.
  • FOR-NEXT - Repeats a section of code while a variable value is within a specified range.

A number of block structured constructs and constructs found in other BASIC language dialects exist -

  • IF-THEN-ELSE-ENDIF - Selects one of two paths of execution dependent upon conditions. Also supports an ELSEIF clause.
  • SELECT-CASE-ENDSELECT - Selects one of a number of paths of execution depending upon value of a variable matched against a list of conditions.
  • DO-LOOP - Conditionally repeats a section of code. Allows construction of WHILE-DO and REPEAT-UNTIL loops.
  • ON-GOTO - Conditionally redirects program execution to one of a number of locations in the program.
  • ON-GOSUB - Conditionally calls one of a number of subroutines in the program.

[edit] Program size

The size of program permitted is dictated by the on-chip EEPROM or Flash capacity.

  • The 08 and 18 have 128 bytes of program memory allowing programs of approximately 40 lines of source code.
  • The 08M, 18A, 28 and 28A have 256 bytes of program memory allowing programs of approximately 80 lines of source code.
  • The X-range have 2048 bytes of program memory allowing approximately 600 lines of source code.
  • The X1 and X2 ranges have 4096 bytes of program memory allowing approximately 1000 lines of source code.

Because the program downloaded is stored as variable length tokens, it is not possible to easily predict the size of program which will be generated from any given source code. In particular, the program size will vary depending upon what value constants are used and which input and output pins are referenced in various commands. The Programming Editor does however provide a Syntax Check function which allows the size of program generated to be determined without having to download the program. This also allows programs to be developed and checked even without access to target hardware.

The variable length size of tokens, coupled with the inability to predict their alignment within the program memory also means that it is not possible to accurately predict the execution speed of any particular command, although typically this will be a minimum of 250 microseconds at normal 4 MHz operating speed.

[edit] Subroutine nesting

A maximum of 16 subroutine call statements (GOSUB) is supported within a program (15 if interrupts are supported). The X, X1 and X2 ranges support up to 256 GOSUB statements.

Subroutines can be nested to a depth of four levels. One level of depth must be reserved for interrupt handler use when interrupts are enabled.

[edit] Illusion of limited capabilities

Although these are constrained devices, with a limited number of variables, limitations in the programming command structures and, on lower-end devices, limited program memory, these limitations have not prevented many successful projects and applications from emerging. The PICAXE is increasingly being used in (and to support) many commercial products.

Provided as a range of devices, each offering differing capabilities and functionality, projects and designs can be tailored to meet requirements such as cost. Their low cost also allows multiple PICAXE configurations to be used and still be cost effective to alternative single-chip solutions. Using multiple devices also has many advantages in system design and modularisation.

[edit] Points of Interest

[edit] PEEK and READ

Whereas most internal processing is done using 16-bits, a value read using the PEEK or READ commands is returned as an 8-bit value, and when stored in a word variable, only the least significant bits of that word will be altered.

[edit] RANDOM

The RANDOM command provides pseudo-random number generator functionality which updates a variable to a new value when it is used. The new value is determined by the existing value of the variable when the RANDOM command is used.

Because all variables are initialised to zero when power is applied or a reset occurs, the variables to be used with RANDOM should be seeded first to prevent the same sequence of random numbers being generated whenever the program is started. Seeding can be done by storing and updating a seeding value within non-volatile EEPROM memory. A mechanism to avoid seeding is to repeatedly execute RANDOM (such as when waiting for an input condition) so the value it has generated will be unpredictable when it is used later.

Although RANDOM can be used with a byte variable, because such a byte value is expanded to 16-bits before the randomising function is applied, the sequence of 'random results' will be very short and produce only a limited set of values.

[edit] MIN and MAX

The MIN and MAX operators using in assignments are 'limiting operations', ensuring that the result of the expression evaluated to that point never falls below or exceeds a specified value respectively.

Although somewhat counter-intuitive, MIN can also be thought of as returning the highest of two values, and MAX can be thought of as returning the lowest of the two.

[edit] Programming interface

Programs are downloaded from the Programming Editor using a serial link, either a physical serial port or a USB to serial adapter. USB serial ports must be able to support RS232 break signalling for successful use.

The basic programming interface consists of just two resistors and does not need RS232 level converters.

[edit] Debugging facilities

Although the development environment offers neither simulation of programs nor single-stepping of executing programs within a PICAXE device, there are a number of options for debugging downloaded programs.

  • If spare output lines are available, these can be used to control LEDs or Piezo sounders to visually or audibly indicate the program's execution reaching the points where such commands are placed. The Serial Output command (SEROUT) can be used to send execution progress indicators and variable content information to a PC or other terminal device.
  • Many of the variants support the SERTXD command which is equivalent to using the SEROUT command to return information about program execution but it uses the Serial Out line of the download interface alleviating the need to use a Digital Output line for this purpose.
  • The DEBUG command can be used with the Debug Monitoring facilities of the Programming Editor, the contents of variables when the DEBUG command is executed can be observed.

[edit] Interfacing

Being based upon the PICmicro, the PICAXE has great versatility in interfacing. Most variants support the on-chip hardware of the underlying PICmicro.

[edit] Digital outputs

Digital Outputs can each sink and source around 20mA and are capable of driving LEDs and other loads directly.

[edit] Digital inputs

Most Digital Inputs are protected by diode clamps to the power rails, which as well as offering good ESD protection, allows interfacing to high voltage signals often with little more than a current limiting resistor being required.

This is particularly useful for interfacing to PCs, PDAs and terminals where a complete serial interface can be implemented using just one resistor. This allows for low-cost designs which do not necessitate the use of RS232 level conversion circuits, although such converters can be used to provide a more traditional interface if desired.

[edit] Bi-directional inputs and outputs

The 18, 18A, 18X, 28 and 28A have fixed direction Input and Output pins. That is to say that every pin is pre-defined as either an input or an output and this use cannot be explicitly changed under program control.

The 08 and 08M have three bi-directional pins which can be either input or output pins and can be explicitly set as such and changed under program control. The 28X and 40X also have eight 'PORTC' lines which can be made inputs or outputs under program control.

At the start of program execution (after power is applied or reset occurs) the bi-directional pins will automatically be set as inputs and only become outputs when instructed by the executing program.

Because it is not always clear what program has previously been downloaded into a PICAXE which supports bi-directional pins, care must be taken when inserting those into alternative hardware. It is recommended that any existing program be erased before it is inserted into hardware different to that which it was previously used with. Inadvertently, or deliberately, making an input line an output line may have damaging effects upon the chip itself and any hardware connected to those pins.

[edit] Interrupts

The 08M, 18A, and all X parts support hardware interrupting on a pattern match with the input pins.

[edit] Analogue inputs

Analogue input is supported across the entire range. The 08 and 18 support only coarse, low-resolution analogue inputs which is delivered as one of 16 8-bit levels. The others support full 256-level, 8-bit analogue input, and some offer 1024-level, 10-bit inputs.

The number of analogue inputs depends on the actual device, and in some cases the analogue inputs and digital inputs share the same pins, allowing them to be used as either analogue or digital inputs, and with some design, both.

[edit] Analogue outputs

Like most microcontrollers, there is no ability to generate an analogue output voltage directly, however analogue voltages can be produced by using PWM output capabilities plus the addition of suitable circuitry.

[edit] Dallas one-wire device

The firmware supports interfacing with some Dallas 1-Wire devices, including the iButton and DS18B20 Temperature sensor. The firmware also provides the ability, through the READOWSN command, to read the unique serial number of any 1-Wire device, whether it can access and use that device's capabilities or not.

[edit] I²C

All of the X Parts (18X, 28X, and 40X) have the capability to use the in-built I²C commands for reading and writing to most I²C devices.

Prior to firmware revision 8.6 on the 18X, and 7.7 on the 28X and 40X, the device being accessed must require at least two data bytes (3 bytes total including the address byte) to be able to be used with the built-in commands. Starting with the above mentioned firmware revisions, and the latest editor software, devices that only require two bytes (an address byte and a data byte) can be used.

Some limitations with the firmware controlled I²C functions are that there is no control beyond read and write commands. Protocol options like repeated start, early stop, attention are not supported. By bit-banging the signals, however, the entire gamut of I²C options can be used, but this requires an in depth familiarity with the I²C protocol itself.

The X1 and X2 parts can be operated as I²C slave devices.

[edit] Infrared

There are three commands to support infrared interfacing -

  • INFRAIN - Allows reception of infrared signals which use Sony's SIRC protocol. This command is tailored to supporting key press codes which would be sent from a TV remote control and supports only a subset of possible remote control commands.
  • INFRAIN2 - Allows reception of infrared signals which use Sony's SIRC protocol. This command allows the reception of any remote control command.
  • INFRAOUT - Allows the generation of an infrared signal using Sony's SIRC protocol.

The INRAIN and INFRAIN2 commands require that an external infrared sensor (TSOP18) is used to demodulate the incoming infrared signal for processing. INFRAOUT can be used to drive an infrared LED (and optional visible light LED) directly.

Even without the INFRAIN and INFRAIN2 commands, infrared signals which use Sony's SIRC protocol can be read by means of bit-banging; sampling the signal provided by the infrared sensor and determining the remote control command sent. This technique is also applicable for decoding infrared signals sent using other protocols, but is not suitable for all such protocols.

[edit] LCD

All variants can interface to serially controlled LCDs using the SEROUT command providing they use a baud rate which is supported.

All but the 08 and 08M can directly control Hitachi HD44780 and similar LCDs operating in parallel, 4-bit mode. Those having too few output lines to support direct interfacing, can connect to such displays, but only through additional interface circuitry, for example by using shift registers. Such interfacing schemes can always be used instead of direct parallel connections.

[edit] PC keyboard

The 18A, 28A and the X-range all provide the ability to interface to a PC keyboard using the KEYIN and KEYLED commands.

These two commands respectively determine which key was pressed on a PC keyboard through its scan code and control the setting of the keyboard LEDs. The KEYIN command can be used to flash the keyboard LEDs to indicate when a key press has been received.

[edit] PWM output

The 08 and 08M support the PWM command and the 08M and the X-range support the PWMOUT command.

The PWM command provides for a burst of a pulse-width modulated signal to be generated which may be used to charge a simple Resitor-Capacitor circuit to generate an analogue voltage output. PWM can be used with any Digital Output pin.

The PWMOUT command allows a pulse-width modulated signal to be continually generated while execution continues, which may be used to generate analogue voltages, as with PWM, but can also be used for speed control of motors and brightness control of visual indicators. PWMOUT can only be used with certain Digital Output pins. The 08M and 18X provide only a single PWMOUT output, while the 28X and 40X provide two.

[edit] Serial

User programmed serial input and output is implemented in the firmware, allowing the serial communications to be received on any Digital Input pin and transmitted on any Digital Output Pin. The serial interface supports a variety of baud rates between 300 and 4800 baud when operating at 4 MHz, up to 9600 baud at 8 MHz (M, A, and X parts), and up to 19,200 baud at 16 MHz (28X and 40X only).

On the 18X, 28X and 40X access to the on-chip AUSART through the use of PEEK and POKE allows bytes to be sent and received at higher baud rates. Those which allow access to the AUSART are capable of transmitting MIDI compatible data and potentially DMX-512 data streams.

[edit] Servo control

All except the 08 and 18 can simultaneously support the control of multiple servos connected to any of its Digital Output pins.

Up to eight servos can be controlled directly using the SERVO command. Once the servo positions have been specified, the required signal stream for each servo is sent while program execution continues.

[edit] Sound control

The SOUND command allows tone generation on any of the Digital Outputs. Piezo sounders can be directly connected to the Digital Output pins and loudspeakers can be driven through a simple amplifier circuit.

The 08M and 14M are the most comprehensive devices for sound generation and supports the PLAY and TUNE commands. PLAY allows one of four pre-programmed tunes to be generated with LEDs optionally flashed in step with the tune's beat, while TUNE allows user created tunes to be played.

Whereas the SOUND command provides for the frequency of a tone and its duration to be specified, the TUNE command allows the tone to be specified in terms of a musical note and the tempo of the tune to be specified. The Programming Editor includes a Wizard to convert suitable mobile phone ringtones to an equivalent TUNE command.

[edit] SPI

Native support for SPI and similar interfacing is provided on the X1 and X2 and can be implemented by direct control of the digital output lines, using a technique known as bit-banging.

[edit] See also

[edit] External links

Languages