Make: Electronics by Charles Platt (classic books to read TXT) 📗
- Author: Charles Platt
Book online «Make: Electronics by Charles Platt (classic books to read TXT) 📗». Author Charles Platt
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
Comments (0)