bookssland.com » Other » Make: Electronics by Charles Platt (classic books to read TXT) 📗

Book online «Make: Electronics by Charles Platt (classic books to read TXT) 📗». Author Charles Platt



1 ... 111 112 113 114 115 116 117 118 119 ... 122
Go to page:
5-133 and 5-134.

Figure 5-134. After the program has been downloaded, the plug can be removed, and the program will continue to run, causing the LED to continue flashing.

5. Check your wiring, and then connect your power supply to the breadboard.

6. Click the button labeled “program” in the Program Editor window to tell the software to look for the PICAXE.

What If It Doesn’t Work?

The first thing to do is pull out the plug of the USB cable from the PICAXE breadboard, leaving the other end of the cable attached to your computer. Set your multimeter to measure DC volts, and attach its probes to sections b and c of the plug. See Figure 5-135. Now click the “program” button again, and your meter should show 5 volts briefly coming out of your computer to the plug on the end of the cable.

If you detect the voltage, the software is installed and working properly. In that case, there’s a problem on your breadboard, either in the chip or in the wiring around it.

Figure 5-135. The stereo plug on the end of the USB download cable can be used for fault tracing. A multimeter set to measure DC volts can be attached to sections b and c of the plug to establish whether the Programming Editor is sending data through the serial connection.

If you cannot detect any voltage, the software probably wasn’t installed properly, or is looking for the wrong serial port. Try uninstalling it and reinstalling it.

Your First Program

Finally you’re ready to create your first program. Type the following code into the Programming Editor window:

main:

high 1

pause 1000

low 1

pause 1000

goto main

Be sure to include the colon after the word “main” on the first line. See Figure 5-136 for a screenshot. The indents are created by pressing the Tab key. Their only purpose is to make program listings more legible. The software ignores them.

Figure 5-136. This screenshot shows the first test program as it should be displayed by the Programming Editor (on a Windows computer).

Click the Program button in the Programming Editor to download this program into the chip. As soon as the download is complete, the chip should start flashing the LED, lighting it for 1 second and then switching it off for 1 second. Figure 5-137 shows the steps that you should have followed to program the chip.

Figure 5-137. Four steps to create and run a program on the PICAXE controller chip.

Now for the interesting part: disconnect the USB cable from the breadboard. The chip should continue flashing.

Disconnect the power supply from the breadboard and wait a minute or two for the capacitors to lose their charge. Reconnect the power, and the chip will start flashing again.

The program that you downloaded to the chip will remain in the memory inside the chip and will begin running every time power is applied to the chip.

Decoding the Code

Let’s take a look at the little program that you typed in. The first line identifies a section of the program. This program only has one section, and we’re calling it “main.” Any word with a colon after it is the name of a section of a program:

main:

The second line tells the chip to send a high output from Logic Pin 1:

high 1

The program doesn’t use the usual pin numbers on the chip. It uses what I will call “logic pin numbers.” Figure 5-138 shows how they are numbered. Figure 5-139 shows their multiple functions. I have put Logic Pin 0 in parentheses because its main purpose is to send data to the computer through the USB cable. It can do double duty as a digital output, but you have to disconnect it from the USB cable first. It’s easy to forget to do this. It’s a hassle that I prefer to avoid.

The third line of the program tells the chip to wait for 1,000 milliseconds. This of course is the same as one second:

pause 1000

The fourth line tells the chip to change Logic Pin 1 back to its low state:

low 1

The fifth line tells the chip to wait for another 1,000 milliseconds:

pause 1000

The last line tells the chip to go back to the beginning of the “main” section:

goto main

Figure 5-138. The conventional pin numbers of the PICAXE chip are incompatible with the numbering system that is used in the PICAXE programming language. To minimize confusion, this guide refers to “Logic Pins” when using the numbering system that is required for programming the chip.

Figure 5-139. Many of the pins on the PICAXE 08M have multiple functions, which can be selected by appropriate program instructions.

Editing

What if you want to change the program? No problem! Use the Programming Editor to change one of the lines in the program. Substitute 100 instead of 1000 milliseconds, for instance. (The pause command can be followed by any number up to 65535.) In your program, don’t use a thousands comma in any of the numbers that you specify.

Plug the USB cable into the breadboard again, hit the Program button on the screen, and the new version of the program will be automatically downloaded to the chip, overwriting the old version.

What if you want to save the program for future use? Just go to the File menu in the Programming Editor and save the program onto your computer’s hard drive. Because the PICAXE uses a variant of the BASIC computer language, it adds a .bas filename extension.

Simulation

If you make a simple typing error, the Programming Editor will find it and stop you from downloading your program. It will leave you to figure out how to fix the line that contains the error.

You’ll need to check the second part of the PICAXE documentation, which contains all the programming statements and their correct syntax. At the time of writing, this is stored at http://www.rev-ed.co.uk/docs/picaxe_manual2.pdf.

Even if all the statements in your program are correctly typed, it’s a good idea to run a simulation of what

1 ... 111 112 113 114 115 116 117 118 119 ... 122
Go to page:

Free e-book «Make: Electronics by Charles Platt (classic books to read TXT) 📗» - read online now

Comments (0)

There are no comments yet. You can be the first!
Add a comment