Introduction
Programming a PICAXE is not hard, but it is exacting. The code must not only be written according to well defined rules, and using very specific terminology, but it must also take into account the details of the circuit being programmed. For that reason, the circuit must be designed before the code can be written, and the hardware must be built before the code can be implemented. Some designers are able to visualize a circuit without benefit of a schematic, but for most people, a schematic diagram is necessary to avoid needless coding errors.This article will cover the basics of programming a PICAXE microcontroller, and will use a very simple test circuit and sample code to help explain the programming process. The test circuit is shown in the schematic diagram below, and in a labeled photograph of the circuit built on a solderless breadboard. A list of the parts required to duplicate the breadboard assembly is also included.
Important Notes: Building and double checking the test circuit as shown following is highly recommended before proceeding further in this article. A review of PICAXE to PC Connections is a good idea as well.
PICAXE Programming Test Circuit Parts List | ||||
---|---|---|---|---|
Designation | Description | Source | Part Number | Comments |
R1 | Resistor, 1/4W, 22kOhms, (red, red, orange) | Digi-Key | 22KQBK-ND | Required. Okay to substitute similar 1/8 watt or larger part. |
R2 | Resistor, 1/4W, 10kOhms, (brown, black, orange) | Digi-Key | 10KQBK-ND | Required. Okay to substitute similar 1/8 watt or larger part. |
R3. R4 | Resistor, 1/4W, 330Ohms, (orange, orange, brown) | Digi-Key | 330QBK-ND | Required. Okay to substitute similar 1/8 watt or larger part. |
J1 | Jack, 3.5mm, 3-conductor | Digi-Key | CP1-3533NG-ND | Optional. Depends on programming cable. See article: PICAXE to PC Connections. |
LED1 | LED, T1, Indicator, Green | Digi-Key | LTL-4231N | Required. Okay to substitute similar part. |
LED2 | LED, T1, Indicator, Red | Digi-Key | LTL-4221N | Required. Okay to substitute similar part. |
C1 | Capacitor, 50V, .1µF, (104) | Digi-Key | 399-9797-ND | Recommended for noise suppression. Okay to substitute similar part. |
U1 | Microcontroller, PICAXE 08M2+ | P.H.Anderson.com | PICAXE-08M2 | Required. Do not substitute. |
N/A | Cable, PICAXE Programming | TBD | TBD | Cable choice depends on PC ports available. See article: PICAXE to PC Connections. |
N/A | Breadboard, solderless, 400 contacts | Digi-Key | 377-2094-ND | Required. Okay to substitute similar part. |
N/A | Wire, jumper, AWG22, solid, tinned, assorted colors | TBD | TBD | Required. Okay to substitute similar part. |
N/A | Power Supply, 5VDC, 500mA, regulated, filtered | TBD | TBD | Required. Any regulated, filtered, 2.3-5.5VDC, 100mA (min.) power source is okay. |
Choosing an IDE
The operating system of the computer that will be used to program PICAXE µCs largely determines the choice of an Integrated Development Environment (IDE.) The table below defines the options. PICAXE Editor 6 includes the Logicator functions, and is the recommended choice for Windows XP, Vista, 7, 8, and 8.1 operating systems. However, AXEpad is a good choice for those just learning the PICAXE system, and will run on Windows, Linux, and Mac operating systems.Operating System | PICAXE Editor 6 | PICAXE Programming Editor | Logicator | WinAXEpad | LinAXEpad | MacAXEpad (Intel) | MacAXEpad (Power PC) |
---|---|---|---|---|---|---|---|
Windows | Yes | Yes | Yes | Yes | No | No | No |
Linux | No | No | No | No | Yes | No | No |
Intel Mac | No | No | No | No | No | Yes | No |
PowerPC Mac | No | No | No | No | No | No | Yes |
Additional information about PICAXE IDEs is available here; follow the instructions to choose, download, and install an IDE on your computer. Windows users could benefit by starting with WinAXEpad due to its ease of use, and then proceeding to PICAXE Editor 6 (covered in part 2 of this topic.)
LinAXEpad
The author of this article is primarily a Windows computer user, but has briefly used AXEpad for Linux. One of LinAXEpad's benefits is that it will run on a very modest and inexpensive computer platform, as proven by the author's use of a 15 year old Dell Latitude and Ubuntu 9.04 as the OS. The setup performed flawlessly, as evidenced by the screenshot below. (Note that Ubuntu distributions 11.10 and later do not include the 32-bit libraries; they must be installed separately in order to run LinAXEpad.)WinAXEpad
WinAXEpad is designed to run under Windows and is one of four variants of AXEpad, but is representative of them all and was used for writing and illustrating this article. The graphical user interface is dated, but its familiar look makes it quite easy to use. As is common in GUIs of this vintage, almost all actions can be accomplished via a pull-down menu or an icon. In either case, operation is straightforward. The start-up screen is shown below.From left to right, the first icon that needs any elucidation is the "Options" tool. Clicking on it opens a tabbed window that will allow you to set up AXEpad to accommodate your hardware and your preferences. The first option to set is to choose the Com port to which your programming cable is connected. If necessary, you can click the "List Serial Ports" button to help identify the port to use.
The next step is to click the "Mode" tab, and use the pull-down selector to choose the PICAXE to be used. Your test circuit should contain a PICAXE-08M2+, and should be powered up. Once you have that completed, click the "Firmware" button.
If you have built the test circuit correctly and have it properly connected to your computer through the programming cable to the port chosen in the previous step, you will be rewarded with the following screen.
Congratulations! You have successfully communicated from your computer to your PICAXE, and received a confirmation message in return. From here on, it gets easier and easier to make the PICAXE do whatever you want.
If you didn't receive the confirmation message and you did have power to the test circuit, you almost certainly have a wiring error of some sort. Check the breadboard carefully to be sure that all connections are correct and secure. Check the programming cable to be sure it is properly connected to the correct port on the PC and to the test circuit. If the programming cable is home-built, test every wire and every connection. Once you have found the problem and corrected it, proceed to next section in this article.
The Program Code
Only a simple piece of code is needed to prove your programming setup as well as the integrity of your breadboard assembly of the PICAXE programming test circuit. To avoid the possibility of typing errors, the code is available for you to download and save on your computer.Start AXEpad, and use it to open the code; it should look like the figure below. Everything that is in green text is a comment that is not actually necessary for the program to run, but is included to explain the operation of the program. Note that a single apostrophe is used to denote the beginning of a comment. Each line in the code is commented, and should be sufficient for you to fully understand its purpose and operation.
One thing that may be a little confusing at first is that, in the PICAXE world, the metal leads that extend from the sides of each PICAXE chip are not called "pins." They are called "legs," and are numbered with leg 1 being adjacent to a small dot or depression in the body of the chip, and increasing as they are counted in a counter-clockwise direction. The term "pin" is reserved to identify which of the legs are inputs and/or outputs to the µC. The diagram below shows a PICAXE-08M2 with the legs numbered 1 through 8, and the pins identified with a letter and a number. The pin designations are included in the code, but the leg numbers are not.
Okay, finally you are ready to program your first PICAXE; just click the Program icon to start the download. If all is well, you should see a screen pop up like the one below. As the program downloads, the progress will show as a series of dots appearing across the screen.
When the download is finished, the download in progress screen will disappear, and a "Download Successful" screen will appear. Click the OK button and pat yourself on the back.
As a final check, look at the two LEDs on your breadboard assembly; they should be happily alternating on and off every half second. If so, you have reached a real milestone in your PICAXE progress: you have a correctly-built download circuit, you have a programming cable that works, and your computer is correctly configured! You have arrived where many who want to be microcontroller hobbyists never do, and it is a big deal!
What's Next?
Watching two leds flash is fun for a little while, but it gets boring in a week or so. Fortunately, there is lots more you can do.• Explore AXEpad. You can't do any permanent harm, so click away and watch the results.
• Read the documentation that is included with AXEpad. It's under the Help icon.
• Modify the program code by changing the timing and sequencing of the LED flashes. (Remember that the original program code can be re-downloaded in the event you need to start over.)
• Add a yellow LED and 330 ohm resistor to the circuit on pin C.4, and make it flash along with the green and red LEDs.
• Then, write the code to create a traffic light pattern with the three LEDs.
Have fun! You're a programmer now.
No comments:
Post a Comment