Make: Electronics

Home > Other > Make: Electronics > Page 41
Make: Electronics Page 41

by Charles Platt


  The 22K and 10K resistors are essential for using the chip; see the following warning note for an explanation. My schematic also includes an LED and a 330Ω resistor, but they are needed only for the test that we’ll be making momentarily.

  Pin 2 Pull-Down

  Always include the 22K resistor and the 10K resistor in the configuration shown in Figure 5-131. These resistors apply correct voltage to the serial connection, and when you’re using the PICAXE on its own, they pull down the voltage on pin 2.

  If pin 2 is left unconnected (floating), it may pick up random voltages, which the chip can misinterpret as a new program or other instructions, with unpredictable and undesirable results.

  The 22K and 10K resistors should be regarded as permanent items accompanying your PICAXE regardless of whether you have it attached to your computer.

  Figure 5-132. Correct wiring of the socket is essential. When soldering wires to the lower terminals, make sure that you attach the wires to both of the terminals in each pair.

  Figure 5-133. The breadboard version of the test schematic, with the plug of the USB download cable inserted in the socket on the board. The PICAXE chip can now receive a downloaded program, and will immediately start to execute it.

  Verifying the Connection

  Follow these steps carefully every time you want to program or reprogram your PICAXE chip:

  1. Insert the USB plug of your PICAXE cable into the same USB port that you used before.

  2. Start the Programming Editor (or AXEpad if you are using a Mac OS or Linux).

  3. In the Programming Editor, select View→Options to verify that the editor is using the right COM port and is expecting the 08M PICAXE chip.

  4. Plug the stereo plug on the free end of the USB cable into the stereo socket that is now wired into your breadboard. See Figures 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 they’ll do, before you download them. This is easily done: click the “simulate” button on the menu bar of the Programming Editor. A new window will open, displaying a diagrammatic view of the PICAXE chip and showing you the states of its pins. (Note that if you use very short pause commands, the simulation won’t run fast enough to display the time accurately.) A simulation screenshot is shown in Figure 5-140.

  The >> button at the bottom-right corner of the simulation window will open up a list of all the variables in your program. So far, it doesn’t have any variables, but it soon will. All the zeros on the righthand side are binary numbers, which you can ignore for now.

  Figure 5-140. This screenshot shows the simulation window that can be opened in the Program Editor to test program code before it is downloaded to the chip. The values of variables are shown in the section on the right. The pin states are shown on the left.

  Loops

  Here’s another thing I’d like you to try. Rewrite your program code as shown here and download it onto the PICAXE:

  main:

  for b0 = 1 to 5

  high 1

  pause 200

  low 1

  pause 200

  next

  wait 2

  goto main

  Note that b0 is letter b followed by a zero, not letter b followed by letter O. The extra indents once again are added to make the listing easier to understand. The four lines beginning “high 1” and ending “pause 200” will be executed repeatedly. It’s helpful to see them as a block.

  Watch the light and see what happens. It should flash five times quickly, then wait for two seconds, and then repeat. You just added a loop to your program. You can use a loop if you want something to happen more than once.

  b0 is known as a variable. Think of it as being like a little “memory box” with its name, b0, on a label on the outside. Figure 5-141 illustrates this concept. This particular memory box can contain any number from 0 through 255. The loop begins by telling the computer to put number 1 in the box, then process the remaining statements, until the word “next” sends the processor back to the first line, at which point it adds 1 to the contents of b0. If the value of b0 is 5 or less, the loop repeats. If the value is 6, the loop has run five times, so it’s over, and the PICAXE skips down to the “wait 2” statement after “next.” See Figure 5-142 for an annotated version of the program listing.

  Figure 5-141. To understand how a program works, visualize a variable as being like a “memory box” with its name on the outside and a number stored on the inside.

  “Wait” is a PICAXE command that is measured in whole seconds, so “wait 2” waits for 2 seconds. Then “goto main” begins the procedure all over again.

  If your flashing-light demo worked out as planned, it’s time to take the next step and make the chip do something more useful.

  Figure 5-142. The blue annotations explain what the program, on the right, is telling the PICAXE to do.

  Fundamentals

  Basic PICAXE parameters

  Here are some of the most useful parameters of the PICAXE:

  The PICAXE requires 5 volts DC, regulated.

  The inputs and outputs of the PICAXE are compatible with 5-volt logic chips. You can attach them directly.

  Each PICAXE pin can sink or source up to 20mA. The whole chip can deliver up to 90mA. This means that you can run LEDs directly from the pins, or a piezo noisemaker (which draws very little current), or a transistor.

  You can use a chip such as the ULN2001A Darlington array (mentioned in the previous experiment) to amplify the output from the PICAXE and drive something such as a relay or a motor.

  The chip executes each line of your program in about 0.1 milliseconds.

  The 08M chip has enough flash memory for about 80 lines of program code. Other PICAXE chips have more memory.

  The PICAXE provides 14 variables named b0 through b13. The “b” stands for “byte,” as each variable occupies a single byte. Each can hold a value ranging from 0 through 255.

  No negative or fractional values are allowed in variables.

  You also have 7 double-byte variables, named w0 through w6. The “w” stands for “word.” Each can hold a value ranging from 0 through 65535.

  The “b” variables share the same memory space as the “w” variables. Thus:

  b0 and b1 use the same bytes as w0.

  b2 and b3 use the same bytes as w1.

  b4 and b5 use the same bytes as w2.

  b6 and b7 use the same bytes as w3.

  b8 and b9 use the same bytes as w4.

  b10 and b11 use the same bytes as w5.

  b12 and b13 use the same bytes as w6.

  b14 and b15 use the same bytes as w7.

  Therefore, if you use w0 as a variable, do not use b0 or b1. If you use b6 as a variable, do not use w3, and so on.

  Variable values are stored in RAM, and disappear when the power is switched off.

  The program is stored in nonvolatile memory, and remains intact when the power is off.

  The manufacturer’s specification claims that the nonvolatile memory is rewritable up to about 100,000 times.

  If you want to attach a switch or pushbutton to a pin and use it as an input, you should add a 10K pull-down resistor between the pin and the negative side of the power supply to hold the pin in a low state when the switch is open. Figure 5-143 shows how pull-down resistors should be used in conjunction with a SPST switch or a pushbutton.

  On the 08M chip, if you apply a varying resistance between Logic Pins 1, 2, or 4, and the negative side of the power supply, the chip can measure it and “decide” what to do. This is the “Analog-Digital Conversion” feature—which leads to our next experiment.

  Figure 5-143. The PICAXE can respond to the state of a switch or button attached to any of its input-capable pins. A 10K resistor must be used to pull down the state of the pin when the contact of the switch or button is open. Otherwise, you may get unpredictable results.

  Experiment 35: Checking the Real World

  Often we want a microcontroller to measure something and respond in an appropriate way. For instance, it can measure a low temperature and sound an alarm, as I suggested in the example that I gave earlier.

  The PICAXE has three analog-to-digital converters (ADCs) built in, accessible via logic pins 1, 2, and 4, as shown in Figure 5-139. The best way to use them is by applying a potential somewhere between 0 and 5 volts. In this experiment, I’ll show you how to calibrate the response of the chip.

  You will need:

  Trimmer potentiometer, 2K. Quantity: 1.

  PICAXE 08M chip and associated USB cable and socket. Quantity: 1 of each.

  Procedure

  Take the same trimmer potentiometer that you used in Experiment 32 and wire its center terminal to Logic Pin 2 of the PICAXE (which is hardware pin 5). The other two terminals of the 2K trimmer go to positive and to negative, respectively. So depending how you set the trimmer, the pin of the PICAXE is directly connected to positive (at one end of the scale), or directly connected to negative (at the other end of the scale), or somewhere in between. See Figure 5-144 for the revised schematic, and Figure 5-145 for a photograph of the breadboarded circuit.

  Figure 5-144. This schematic, drawn in a layout suitable for breadboarding, shows how a 2K potentiometer can be used to apply a varying voltage to one of the pins of the PICAXE that is capable of converting an analog signal to a digital value.

  Figure 5-145. The trimmer potentiometer added to the previously breadboarded circuit.

  Now we need a program to tell the chip what to do. Using the Programming Editor, start a new docum
ent. The code should look like this:

  main:

  readadc 2,b0

  debug b0

  goto main

  The command “readadc 2,b0” means “read the analog input on Logic Pin 2, convert from analog to digital, and store the result in b0.”

  The command “debug b0” tells the chip to go into program debugging mode, in which it uses its USB cable to tell the Programming Editor the values of all the variables while the program is running. The variables are displayed in a debugging window.

  Download the program, and as the program starts to execute, the debugging window should open. Start adjusting the trimmer while looking at the value of b0, and you’ll see b0 change its value.

  You can make a table and draw a graph showing the relationship between the resistance between Logic Pin 2 and ground, and the value of b0. Just pull the trimmer off the breadboard, measure its resistance with a meter, then increase its resistance by, say, 200Ω, put it back into the breadboard, and look at the value of b0 again.

  This is laborious, but calibrating equipment is always laborious—and in any case, I decided to do it for you. The graph is shown in Figure 5-146. You can also see the raw data numbers in the following table. I was pleased to find that the PICAXE gives a very precise, linear response to the input voltage. In other words, the graph is a straight line.

  Figure 5-146. When an ADC input pin is hooked up to a 2K potentiometer, which is connected across the same voltage that powers the chip, you should find that the resistance between the input pin and the negative side of the power supply generates the series of digital values shown on the graph. Note that the potentiometer must have a 2K value, and the power supply is assumed to be precisely 5 volts.

 

‹ Prev