I’d like to talk about my favorite microcontroller, the PICAXE. I like it for two major reasons. First, its cheap, only a few dollars for the smallest type And second, its programmed in BASIC. So the learning curve to using them is very shallow. The PICAXE was developed after the BASIC stamp and has many similarities.
PICAXE is a family of programmable microcontrollers from the UK company, Revolution Education. Each PICAXE is a PIC microcontroller programmed with a communications program and BASIC interpreter. The user programs the PICAXE in BASIC using the PICAXE programming Editor. Like a word processor, the Editor lets you type code, save the program in a text file on the PC, and checks the program for syntax errors. When a program is syntactically correct, the Editor allows the user to download the program into the PICAXE connected to the PC. The program is downloaded over a three wire serial port. The three connections in the serial cable are ground, signal in and signal out. The user created program is stored in the PICAXE’s internal memory. PICAXEs do not store their program in a separate memory chip like the BASIC Stamp. After having received the program or when the PICAXE is powered up, the PICAXE’s interpreter begins executing the instructions stored in its EEPROM memory, one command at a time.
There are six types of single-chip PICAXE microcontrollers that I want to discuss. The name given to the variations of PICAXE microcontrollers is based on the number of I/O pins on the IC and it generation. The smallest is the PICAXE-08M2 and it looks like an eight-pin 555 IC. Some have called the PICAXE-08 the 21 st century 555 timer. I want to mention the voltages, ability to execute multiple programs, and clock speeds of these six microcontrollers. Voltages
First, both the M2 and X2 series operate at 5 volts but can operate down to 1.8 volts. This permits you to create applications that don’t require a voltage regulator. Clock Speed M2 has an internal 4 MHz oscillator, but it can be clocked up to 8 times faster, or to a speed of 32 MHz. X2 has an internal 8 MHz oscillator, but can be clocked twice as fast, or 16 MHz.
Both variations can operate multiple programs, but in different ways. The M2 series can run either two or four programs in semi-parallel. Semi-parallel execution means each program gets a turn executing a single command. Commands waiting for input and without time outs can stall the switching between programs. The programs are stored in a single program slot.
The X2 series stores programs in separate slots. It uses the RUN command to switch execution between two or four program slots.
Finally there clock speeds. The M2 series has an internal 4 MHz oscillator, but it can be clocked up to 8 times faster, or to a speed of 32 MHz.
The X2 has an internal 8 MHz oscillator, but can be clocked twice as fast, or 16 MHz.
I/O pins are the microcontroller’s access to the real world. The I/O pins detect binary states where a logic low is less than 1.4 volts and a logic high is above 1.4 volts. Several I/O pins can handle analog voltage using the READADC command The PICAXE-08M2 has only a bank C of six I/O pins while The PICAXE40X2 has four banks of eight I/O pins. Its banks are A, B, C, and D. Most pins can be used as both input and output while a few can only be configured to be just input or output The X2 series has pins that can be configured as hardware interrupts. The interrupts are not instantaneous however. Pins configured as hardware interrupts are scanned constantly in background. When the interrupt condition occurs, the interrupt flag of the PICAXE is set. The PICAXE does not check the interrupt flag until it executes the SETINTFLAGS command. Both the M2 and X2 series have a software interrupt that is established by the SETINT command. The command causes the PICAXE to scan selected pins for specified high and low conditions. When conditions are met, the PICAXE jumps execution to the specified subroutine.
PICAXE microcontrollers have four types of internal memory, program, RAM, data, and scratch pad.
First kind of memory is program memory. This is non-volatile memory and used to store user-created programs. The useful life of this kind of memory is predicted to be 10 years. The amount of memory depends on the specific microcontroller. The M2 series has 2k bytes of program memory. This is enough memory to store a program over 1,000 lines long. The X2 series has 4k bytes of program memory. However, the memory is arranged in slots. The X2 series chips have either one or four slots of program memory and there are commands to switch between slots. In addition, some X2 PICAXE microcontrollers allow you to add up to an additional 32 external program slots using I2C memory chips.
The second type of memory is RAM memory and its used to store variables used in math and I/O. The lower level of RAM is accessed in bytes by their names. Some of the bytes can also be accessed at the bit and word level. The M2 series has up to 512 bytes of RAM. The first 28 bytes are named B0 to B27 while the rest are accessed indirectly using the PEEK and POKE commands The X2 series has up to 256 bytes of RAM. The first 56 bytes are named B0 to B55 while the rest are accessed indirectly.
The M2 series has a third type of memory called data memory. There is 256 bytes of data memory which is useful for storing sensor readings in datalogger applications and storing configuration settings. The memory is non-volatile and accessed at the byte level with READ and WRITE commands.
The X2 series has a fourth type of memory called scratch pad memory. They have 1 kilobyte of scratch pad memory and it can be used to store serial data over the chip’s hardware serial port in background. The stored data string can be accessed later using the GET and PUT commands
The PICAXE can read its input pins several ways. At the simplest level, at any time during a program, the state of a pin can be detected as being either high or low. In addition, the PICAXE can count the number of high to low transitions in an I/O pin over a specified length of time. It can also measure how long a pulse is held high. There are several ways to read serial-type data over the I/O pins. These include the following Serial data from speeds 600 baud to 64k baud Serial data from I2C devices at speeds of either 100 kHz or 400 kHz Analog voltages can be converted into their digital representation with either 8-bit or 10-bit resolution Seven bit value from infrared devices like television remotes And eight bytes of data from inexpensive radio devices
The PICAXE can set its output pins in several ways. At the simplest level, the state of a pin can be set to high or low at any time in the program. In addition, a pin may be set high for a specified length of time. There are several ways to pulse an I/O pin and these include the following Send serial data from 600 baud to 64k baud Write to I2C devices at a speed of either 100 kHz or 400 kHz Play an embedded tune or musical note Send pulses to position a servo Send a seven bit value to infrared devices like television sets Send eight bytes of data through inexpensive radio devices
Execution of a program can be altered or controlled unconditionally or conditionally with the following commands. The GOTO and GOSUB commands are unconditional branching commands. The FOR-NEXT command is a looping control command. DO-LOOPs repeat commands while or until a condition is met. The branching condition can be tested either as part of the DO or the LOOP statement. The UNTIL and WHILE sub-commands state the condition at which the do loop is repeated or broken. The IF-THEN command is a conditional branching command. Incorporating the ELSEIF statement allows the command to create additional branches for additional conditions. It also allows a single condition to result in the execution of several commands, or essentially subroutines. ON-GOTO branches to an address in the program based on the current value of a variable. SELCT-CASE conditionally executes code statements depending on the value stored in a variable. The command is useful as a replacement for nested IF-THEN statements. The BRANCH command jumps execution to a new location in the program based on the value of a variable. The location is indicated by the address name.
This code snippet shows the three commands required to setup a PICAXE for communication with a I2C memory chip, to write values to memory, which in this example is the word hello, and to copy the stored data from I2C memory into RAM memory.
This code snippet shows seven examples of commands used to communicate over a serial link. The first is the DEBUG command. When ever this command is executed, the PICAXE sends the current values in RAM variables. The next two examples illustrate sending messages and values over any I/O pin. The command indicates which I/O pin to use, the baud rate, and the text or values which must be stored in RAM variables. The following two illustrate the same thing, but this time the data is sent and received over the PICAXE’s programming port. The speed is only 4800 baud. The last three illustrate how a hardware serial port is setup for a PICAXE. The hardware serial port allows the PICAXE to receive serial data in background and dump it into scratch pad memory. The PICAXE can parse through the data at a later time.
The POKE commands copies the value of one or more named RAM variables into unnamed RAM memory, or EEPROM. The command can copy data in bytes or words. The PEEK command does the reverse and copies one or more values from unnamed RAM memory into named RAM memory. The READ-WRITE commands do the same thing as PEEK and POKE, except data is copied into data memory, or EEPROM. The GET and PUT commands copy byte values to scratchpad memory.
The Editor is only PC based, there is no Mac version. The Editor allows you to do the following Select the variant of PICAXE to be programmed Select the com port for programming Create source code in either text or flowcharts Generate desired command parameters with the use of wizards Check syntax Download source code into a PICAXE Simulate a program running in a PICAXE Debug code by placing DEBUG commands that return variable values Communicate the PICAXE through a serial terminal program
The Editor can program any one of the PICAXEs. Therefore, you need to select which PICAXE will be programmed. One reason this is important s that not all commands and parameters are valid for every PICAXE. You select your PICAXE by clicking the Options button located at the top of the Editor. The Mode tab presents a drop down menu where you can select your PICAXE. The mode is sticky, so the next time the Editor is started, the previously selected PICAXE mode is in effect.
Since most PCs and laptops do not have serial ports, most PICAXEs are programmed through a USB to Serial adapter. This can create a forest of serial ports on the PC. The Editor does not try searching all possible serial ports in order to find which one a PICAXE is hanging off. You must set the correct serial port for the Editor. The serial port is selected by clicking the Options button at the top of the Editor screen. Serial ports is found in the Serial Port tab. There you will find a drop down menu where you can select the proper serial port.
The Editor is smart the way it displays programs. A comment followed by text is displayed in green. Comments are not downloaded into the PICAXE. Commands are displayed in blue text. Program labels are displayed in black text. Labels in the program must end in a colon, but labels used in commands do not end in colons. I/O pins and variables are displayed in purple. The Editor edits text like a word processor. So you can find characters, copy, paste, and make global replacements.
A second way to create a program is to use a flowchart. This graphical version of a program requires more space to display and can be converted into a text version of the program.
Wizards help you set up parameters for specific commands. Wizards are found under the PICAXE option. There are two wizards related directly toe PICAXE programming, ring tones and PWM output. In addition, there are wizards for converting a program into one that runs on an X2 version of a PICAXE and for converting serial data into TCP/IP data for remote programming a PICAXE.
Clicking the Syntax button checks over the current program in the Editor. This verifies that the program is correctly written for the PICAXE version specified in Mode. This does not verify that the program will work as desired. It only verifies that the code can operate in the PICAXE.
The Program button opens communication with the PICAXE connected to the serial port selected in Mode. Once communications have been established, the program is downloaded into the PICAXE. The PICAXE verifies with the Editor that it properly received the program. A graphic is displayed that indicates how much progress has been made in downloading the program. If there is an error, the Editor warns you that communications with the PICAXE has failed. After the program is correctly downloaded, the Editor notifies you and indicates how much memory the program requires.
Prior to downloading a program, you can simulate the operation of the program by clicking on the Simulate button. In the background is the text of the program which shows the currently executing line highlighted in a blue block. Three additional windows open up. The first windows displays the values of the variables in the PICAXE memory and the status of each I/O pin. You can set pins high or low by clicking its icon. In addition, you can enter analog voltage values for each of the pins capable of using the READADC command. The second window is a memory map. It displays the byte values of addresses in three types of memory. These are EEPROM memory which is accessed with READ and WRITE commands, RAM which is addressed directly or indirectly with PEEK and POKE commands, and Scratchpad memory which is accessed with GET and PUT commands. The last wind displays serial data that the PICAXE may generate.
Programs can be debugged live. By leaving the PICAXE connected to the PC and using the DEBUG command, your program will display the current values stored in RAM. The Debug window opens as soon as the program is downloaded to the PICAXE. The window displays the values in each byte in binary, decimal, hexadecimal, and ASCII. In addition, RAM memory values is also displayed in words. The last thing displayed in the Debug window is the values in several PICAXE registers.
Another feature of the Editor is its terminal program. The terminal displays serial data sent by any microcontroller. The PICAXE uses the SEROUT command to send serial data through any of its I/O pins that can be used as an output. One parameter of the SEROUT command is the baud rate. The terminal program can read serial data at ten different baud rates. You just need to make sure the terminal’s baud rate matches the baud rate of the PICAXE’s SEROUT command.
Introduction to PICAXE Microcontrollers
PICAXE Microcontrollers An Introduction by NearSys
What is a PICAXE?• A family of microcontrollers• Based on the PIC microcontroller• Internally programmed with an interpreter and communications program• Program stored in the PIC’s internal EEPROM• Interpreter operates on one instruction at a time