Presentation is loading. Please wait.

Presentation is loading. Please wait.

PICAXE BASIC The programming language used by the PICAXE microcontrollers is called BASIC. BASIC is a very early programming language and designed to make.

Similar presentations


Presentation on theme: "PICAXE BASIC The programming language used by the PICAXE microcontrollers is called BASIC. BASIC is a very early programming language and designed to make."— Presentation transcript:

1 PICAXE BASIC The programming language used by the PICAXE microcontrollers is called BASIC. BASIC is a very early programming language and designed to make it easy for the beginner to program. In the next set of slides, we will cover the commands you will need to program the Temperature Logger. Your program will be a sequence of command, with each command being an instruction for the PICAXE in the Temperature Logger to carry out. The execution of the program begins at the top of the program, just like you start reading a text at the top of a page. Execution of the program works its way down the program until a command to stop execution is encountered. There are several ways to make the PICAXE repeat some or all of the program. Execution of the program begins as soon as the battery is attached to the Temperature Logger. If the battery is disconnected and then reconnected, execution of the program begins all over again at the top of the program.

2 The PICAXE-08M2 The heart of the Temperature Logger is the PICAXE-08M2. The PICAXE has eight connections, hence the 08 in its name. Two of the connections, or pins, are connected to the battery. Two more are connected to the DB-9 connector that permits you to attach the Temperature Logger to the PC. Three of the remaining four are called I/O pins, where I/O stands for input-output. The I/O pins are names C.1 to C.4. I/O pin C.3 connects to the pushbutton switch, C.2 connects to the temperature sensor, and C.1 connects to the LED indicator. I/O pin C.4 is not used in the circuit. The PICAXE-08M2 is a programmable microcontroller that measures and the temperature as reported by a LM335 temperature sensor and records it into memory. The temperature sensor is an analog device that produces a voltage proportional to the temperature. Its voltage is converted to the digital value through a process called analog to digital conversion. This is necessary because an analog voltage cannot be stored into memory based on bytes. The PICAXE can digitize a voltage only when it is instructed to do so. If this command is not included in the PICAXE’s program, it will never digitize the temperature sensor’s voltage. Likewise for storing readings. Unless the program specifically instructs the PICAXE to store a temperature reading into memory, it will never do so. It is now your job to write a program, the set of instruction that the PICAXE must follow. This program is created on a PC and saved there. At a time of your choosing, you can download a copy of the program into the PICAXE and it will then carry out the instructions perfectly. A problem can occur when your program does not properly instruct the PICAXE to carry out the process of logging temperatures. But fear not, you can always fix or tweak your program and download it again into the PICAXE.

3 Using Labels Temperature: Read_temperature: ReadTemp:
First, use labels to indicate locations in the program. Labels are like marking addresses in your program. Your program can then move execution to a new location in the program by jumping to the label. Besides, smart uses of labels can indicate the function of blocks of code. A label can be any word with these exceptions. It cannot begin with a digit It must be a single word It cannot be a valid BASIC command It must end with a colon In the examples in this slide, the word temperature is not a command in BASIC, so it makes a valid label. Notice that it ends in a colon. Read_temperature is a good label because it is telling you that this portion of the program is reading the temperature. However, it must use the underscore as a spacer if I want to leave the label as a two word sentence. ReadTemp is another good example of the label. It uses a capital T to indicate that Temp is a new word.

4 Using RAM Variables B0 = 12 B1 = B3 B4 = B4 + 1
Remember that RAM bytes are named with a “B” (for byte) followed by a number. RAM bytes begin with B0 and end with B27 for a total of 28 different bytes. Another name for a RAM byte is variable. The program uses variables to collect data, manipulate data, and to store data. Often a program will use a specific variable for a single purpose. So for example, a program could always use variable B4 to store a temperature. Every place you see B4 in this program, you will know the program is storing a temperature at that point. A variable can be set to a specific value, like B0 = 12. After executing this command, the RAM byte B0 would contain the number 12 in base two form (which is ). In the second example, the value stored in B1 is copied to B3. The number stored in B1 is still there, a copy was just made and moved to B3. In the third example, the number stored in B4 is increased by one. So after this command, the number stored in B4 is one larger than before

5 SYMBOL SYMBOL Temperature = B0 SYMBOL Correction = 3
Often the numbers and variables used in a program are not very meaningful to someone who is reading the program. Even if you write the program, chances are that if one year later you were to look at the program again, you wouldn’t remember what each variable was used for. Symbols are used in this case to remind the reader the function of a variable. Using symbols also makes it easy to update a program. Especially where the same number is used in multiple places in the program. In the first example, the word Temperature is used in place of the variable B0. Essentially, the word Temperature has become the variable B0 in this program. The reason one would do this is to remind someone reading the program (perhaps to fix an error) that the variable B0 is used for the temperature value. When the program says to store the temperature in memory, its more meaningful than seeing the program say to store B0 in memory. Some programs need to use the same number repeatedly. For example, a microcontroller circuit may have a sensor that has a small error in it. This can be corrected in the program by adding a correction to the sensor’s reading. But what happens if the sensor is replaced and the new sensor has a different amount of error? A very large program that requires lots of correction factors would be a major pain to update. Every place where the correction is included in the program would need to be found and updated. Chances are that in a long and complex program, one or more corrections would be missed. However, updating a program like this can be made much easier by writing the symbol “correction” in place of the actual correction factor. Then at the top of the program, the symbol “correction” could be set to the correction factor. Then any time it was necessary to make a change to the correction factor, one would only need to change it at the top of the program where the value of the symbol is set.

6 DEBUG DEBUG The DEBUG command gives you a snapshot of the RAM of the PICAXE. The Temperature Logger must remain connected to the PC and the PICAXE Editor must remain opened for the DEBUG command to be of any use. The PICAXE Editor and PICAXE work together. So anytime the PICAXE executes the Debug Window, it either opens up the Debug Window to show you the current numbers stored in each RAM byte or it updates an already opened Debug Window. The Debug Window is just a snapshot at the time if the DEBUG command. It won’t update with new values unless a DEBUG command is executed again. DEBUG is a tool for analyzing why a program or a circuit is not working correctly. You can set values in the RAM bytes and then execute DEBUG to see what values are appearing to the PICAXE. Now a helpful hint. The PICAXE-08M2 is pretty fast. If a EBUG command is executes early in the program, the PICAXE may respond before the PC is ready to receive the PICAXE’s data. Therefore, it is a good idea to include a PAUSE 500 at the beginning of your program. That way the PICAXE will wait ½ a second before trying to send a debug report to the PC.

7 This is what the Debug Window looks like
This is what the Debug Window looks like. Notice in this case, that RAM byte B0 (or variable B0) is currently set to a value of 152. In hexadecimal (base 16) a 152 is written as 98 and in binary (or base two), it is written as You will see in the program behind the Debug Window, that a command called READADC did something with I/O pin C.2 and moved the results into variable B0. Then the EBUG command showed the value that the READADC command moved into B0.

8 GOTO GOTO Temperature * Temperature:
The GOTO command directs the program’s execution to a new location in the program. The location is indicated by a label written into the program. In this example, the PICAXE stops executing commands as soon as it sees the GOTO Temperature command. The PICAXE then jumps over all the commands in the program until it comes across the label, Temperature. The PICAXE then begins executing commands from that point on. GOTO commands can point to locations in the program that are above or below the GOTO statement.

9 HIGH HIGH C.1 The HIGH command internally sets an I/O pin to five volts. Since the PICAXE is connected to the battery (though the five volt voltage regulator), it can connect an I/O pin to five volts. When this occurs, five volts appears on the I/O pin. If that I/O pin is connected to an LED, then you can light the LED by using the HIGH command. In the example here, I/O pin C.1 is connected to the anode of the LED. As a result, when I/O pin C.1 is set high, it provides the voltage needed to light up the LED.

10 LOW LOW C.1 The LOW command internally sets an I/O pin to zero volts, or ground. Since the PICAXE is connected to the battery (though the five volt voltage regulator), it can connect an I/O pin to the negative side of the battery. When this occurs, zero volts appears on the I/O pin. If that I/O pin is connected to an LED, then you can shut off a lit LED by using the LOW command. In the example here, I/O pin C.1 is connected to the anode of the LED. As a result, when I/O pin C.1 is set low, no voltage is connected to the LED and so it shuts off.

11 PAUSE PAUSE 1000 Normally, the PICAXE quickly executes one command after another. The PICAXE can execute a command and jump to the next one faster than you can read the program. The PAUSE command stops program execution for a specific amount of time. During the pause, the PICAXE does nothing but wait. Since the PICAXE is so fast (compared to humans), the length of the pause is given in milliseconds, or thousandths of a second. So the command example here stops execution of the program for a total of one thousand milliseconds, or for one second. The longest PAUSE that can be coded into a program is 65.5 seconds long. If you need to pause for two minutes, then type two PAUSE commands, one after the other.

12 END * END The END command stops execution of the program. Don’t let the PICAXE execute the END command until you are sure you want the PICAXE to stop. In this example, the PICAXE executes the commands represented by the asterisks in sequence until the PICAXE encounters the END command. No commands written after the END will be executed once the PICAXE executes the END command.

13 Practice #1 HIGH C.1 PAUSE 1000 LOW C.1 END
Let’s put the HIGH, LOW, and PAUSE commands together to create your first simple program. Recall that the HIGH will turn on the LED since the LED is connected to I/O pin C.1. If we HIGH a different I/O pin, nothing will happen to the LED. Also recall that the LOW will turn off the LED since I/O pin C.1 is the one being commanded to shut off. Between the HIGH and LOW commands, the PICAXE will wait for one second. Type this simple program and run a syntax check. Correct any typos that may have occurred. Then make sure the correct mode (PICAXE-08M2) and serial port are selected in the Setup menu. Then connect a battery and the programming cable to the Temperature Logger and download this program. After the program is successfully downloaded into the PICAXE, you should observe the LED light up for one second and then shut off. If you disconnect and then reconnect the battery, the program will start over and the LED will light up for one second. If you disconnect the battery until tomorrow, the LED will light up for one second as soon as the battery is reconnected. Try changing the length of the PAUSE to longer and shorted values and observe the results.

14 Practice #2 Temperature: HIGH C.1 PAUSE 1000 LOW C.1 GOTO Temperature
END Let’s put the HIGH, LOW, PAUSE, and GOTO commands together to create your second simple program. Recall that the previous practice only lit the LED once. Now add a GOTO command to the end of the program (but before the END command) so the process can be repeated over and over again. After connecting the battery, the PICAXE will start execution at the Temperature label. This is only a label, so the PICAXE doesn’t actually do anything. Then the PICAXE will internally connect I/O pin C.1 to five volt. This connects the LED to five volts and so it lights up. Then the PICAXE waits for one second. During this wait, no new commands are executed, so the HIGH command remains in operation. In other words, the LED remains lit until the PICAXE is finished waiting and is instructed to do something else with I/O pin C.1 The next command shuts off the LED because it makes the I/O pin C.1 go to zero volts. After shutting off the LED, the PICAXE waits another 1,000 milliseconds, or one second. During the time, the LED remains off. The next command tells the PICAXE to jump to where the label Temperature is written and begin execution again. As a result, the PICAXE begins running the program over again. Notice that the END command is never reached. Even though it is written into the program, the PICAXE will never get to it. If it did, then the program would stop the PICAXE from executing any other commands. So as a result, this program will cause the PICAXE to continually light up and turn off the LED. Now try changing the length in the PAUSE commands and see what happens. Remember, the longest pause is 65.5 seconds, but it’s a good idea to limit them to one minute (60 seconds or 60,000 milliseconds) in your program. What is the fastest you can blink the LED and still see that it is blinking?

15 READADC READADC C.2,B0 This command measures the voltage on an I/O pin and converts it to a digital value. The voltage range is zero to five volts and the digital output range is 0 to 255. In this scheme, zero volts is converted to the digital value of 0 and five volts is converted to the digital value of 255. This is a linear conversion process, so 2.5 volts is converted to either 127 or 128 (there are no decimal values). In the example here, the voltage of the temperature sensor (which is connected to I/O pin C.2) is converted into a digital equivalent (digitized) and then the value is copied into variable B0.

16 Practice #3 Temperature: PAUSE 500 READADC C.2,B2 DEBUG END
Let’s put the READADC and DEBUG commands together to create another simple program. The Temperature label does nothing in this program. I just left it to remind myself what this program does, since I may not remember what is connected to I/O pin C.2 six months form now. The PAUSE 500 ensures the PICAXE will wait long enough for the PICAXE Editor to get ready to receive a snapshot of the PICAXE’s RAM. The READADC command measures the voltage that the temperature sensor places on I/O pin C.2 and then digitizes it. The results is then stored in variable (or RAM byte) B2. The DEBUG command then creates a snapshot of the PICAXE’s variables. We are interested in reading the value of variable B2 in this case. After trying out this program, place the Temperature into the freezer for a few minutes and then download this program into the PICAXE Again. You should see that the value stored in B2 is significantly lower. If it is a hot day outside, repeat this test by leaving the Temperature Logger in the sun for a few minutes. Then bring it back into the house and download this program again. You should see that the value in B2 is higher this time.

17 SERTXD SERTXD (“Temperature: ”, #B0)
The SERTXD command is like the EBUG command. It allows the PICAXE to end data to the PICAXE Editor. But the SERTXD command allows you to end more than just the value of all the variables in RAM. SERTXD allows you to send specific data and to format it into a useful form. The PICAXE Editor’s Terminal program must be opened to use the SERTXD command. The text to send to the Terminal program must be enclosed in parenthesis. If text is to be written exactly, then it must have double quote marks around it. Every bit of unique text must be separated by a comma. Finally, if a value in variable is to be written as a number and not interpreted as a computer character, then it must be proceeded by a pound sign (#). In this example, the word Temperature: appears in the Terminal program followed by the value stored in variable B0. The value in B0 will be displayed as a number and not as a computer character.

18 FOR - NEXT FOR B0 = 1 to 50 * NEXT +
The GOTO command created an endless loop. Once the PICAXE executed it, it would loop forever around the program. If you want a program loop to execute only a fixed number of times, then the FOR – NEXT command is your man. The FOR – NEXT loop makes use of a counter, which must be a variable since it is updated continuously in the program. The counter keeps track of how many times the program loop has executed. After the correct number of executions, the counter bumps the PICAXE out of the program loop and on to the next set of instructions. In this example, the counter is the variable B0. The loop begins at the FOR and ends at the NEXT. The counter will only let the PICAXE execute the program between the FOR and the NEXT (which is represented by the asterisks) fifty times. After fifty times around the block, the PICAXE jumps to the part of the program after the NEXT command (represented by the plus sign).

19 IF - THEN IF B4 < 25 THEN Temperature * Temperature: +
Some times we want the PICAXE to execute different code when a condition occurs. So for example, if the temperature is too low, then we want to record the temperature. And if the temperature is above a minimum, then we don’t want to record the temperature. We can create a conditional execution using the IF – THEN statement. In this command, if a condition is met, then the PICAXE jumps to a new location in the program. It does not execute the command or commands following the IF statement. So in this example, if variable B4 holds a value less than 25, the PICAXE jumps to the label Temperature and skips the commands represented by the asterisks. Only if B4 has a value greater than or equal to 25 will it execute the commands represented by the asterisks. By the way, saying when the condition is met is the same thing as saying if the condition is true.

20 IF – THEN part two PushButton: IF pinC.3 = 1 THEN PushButton *
It’s not just variables that can be checked with IF – THEN statements. The PICAXE can also check the condition of the Temperature Logger’s push button. The push button has two conditions. Either it is pushed or it’s not pushed. When it is not pressed down, current from the voltage regulator enters into I/O pin C.3. When five volts is present at C.3, then the I/O pin measures a one. When the push button is pressed down, then I/O pin no longer sees the voltage regulator’s five volts. Instead it sees the negative side of the battery, or zero volts. When the I/O pin is connected to ground like this, it measures a zero. The IF statement can check the status of the push button by asking if I/O pin measures either a one or a zero. This is done by the following command, IF pinC.3. The word pinC.3 takes the place of a variable like in the last slide. The example code on this slide determines of the push button is not pressed down. If it is not pressed, then the PICAXE goes back to the label PushButton and checks again. Once the push button is pressed, I/O pin C.3 measures zero and no longer measures a one. Since the I/O pin does not measure a one, the PICAXE does not jump back to the label PushButton. So instead it executes the commands represented by the asterisks. This snippet of program makes the Temperature Logger wait until the push button is pressed down before executing any of the other commands.

21 Practice #4 PushButton: IF pinC.3 = 1 THEN PushButton Blink: HIGH C.1
PAUSE 250 LOW C.1 GOTO Blink In this practice, the Temperature Logger will wait until after its push button is pressed before rapidly blinking the LED. Can you explain why it works?

22 IF - THEN - ENDIF IF B10 = 45 THEN * ENDIF +
There is another way to force the PICAXE to only execute a block of program if a particular condition is met. We can use the IF – THEN – ENDIF statement. The command begins with a condition to check. If that condition is met, then all the commands until the ENDIF are executed. After completing the commands inside the IF – ENDIF, the PICAXE continues executing commands after the ENDIF statement (represented by the plus signs). If the condition is not met, then the PICAXE jumps to the first command after the ENIF statement and continues execution (represented by the plus signs).

23 Practice #5 Temperature: READADC C.2,B14 IF B14 < 150 THEN HIGH C.1
PAUSE 1000 LOW C.1 GOTO Temperature ENDIF PAUSE 500 Let’s put the IF – THEN – ENDIF statement to use. In this practice, the Temperature Logger digitizes the temperature and stores the result in B14. Next it checks if the reading is less than or greater than 150 (this is roughly room temperature). If it is warmer, then the LED blinks faster than it blinks when the temperature is cooler.

24 WRITE WRITE 25,200 or WRITE 100,B0 WRITE B1,B2
The next command stores a number in a variable into permanent storage (permanent at least until it is overwritten). The WRITE command consists of the word “write” followed by the address in Data Memory to save a number followed by the number to save there. In the first example, the WRITE command will write the number 200 into Data Memory address 25. After executing this command, if you searched address 25 in Data Memory, you would find that the number 200 was being stored there. In the second example, The number stored in variable B0 will be stored into Data Memory address 100. So what ever number B0 is storing right now, a copy of that number will be written into Memory Data address 100. The third example is the most flexible. In this case, the number currently stored in variable B2 will be copied into a Data Memory address that is equal to the number in variable B1. This is a powerful statement because the number in variable B1 can be repeatedly increased. Each time it is increased and the command is executed, the number in B2 is written to a new location in Data Memory. You should realize that this means the address in Data Memory doesn’t have to be hard coded in the program. This single statement can write numbers into every Data Memory address without having to create a WRITE command for every address in Data Memory. In other words, one command can do the work of 256 commands. By the way, in this situation, we say that B1 is pointing to an address in Data Memory.

25 READ READ 100,B3 or READ B6,B12 Where the WRITE statement copies numbers into Data Memory, the READ command copies them into RAM so you can see them again. The READ command begins with the word “read” followed by the address in Data Memory you want to copy followed by the variable to copy the number to. After the READ command, the original number still exists in the Data Memory address. It’s just that a copy was made into a variable in RAM. In the first example, the number stored in Data Memory address 100 is copied into variable B3. If this is followed by a DEBUG command, then you will see in B3 the number stored in Data Memory address 100. Remember that the DEBUG command cannot see what is stored in Data Memory. So you need to use the READ command to copy the numbers into RAM where you can see it again. Remember that DEBUG is not the only command that can show numbers stored in RAM. Do you recall the other command? After a READ command, you could add a SERTXD command. If you write the SERTXD command properly, it will also show you the number currently stored in B3 (the same number that is also stored in Data Memory). The problem with the first example is that you would have to write the READ command 256 times to see all the numbers stored in Data Memory. So better would be to let a variable point to an address in Data Memory and execute it 256 times to copy all the numbers stored in Data Memory. This is what the second example will do if it is enclosed within a FOR – NEXT loop.

26 Using Comments HIGH C.1 ‘ turn on the LED PAUSE 500 ‘ wait ½ second
LOW C.1 ‘ turn off the LED PAUSE ‘ wait ½ second Comments are notes to yourself, the PICAXE ignores them. A comment is any text that follows the apostrophe (‘). Comments can occur anywhere in your program, but are more useful if they follow each command. This way they explain what the command is attempting to accomplish. In the above example, the HIGH C.1 is not very meaningful to someone reading your program. It may not be meaningful to you if you come back next year to look over your program. By using comments, you will be able to figure out what your program is doing. This is helpful if your program is not making the PICAXE do what you think it should be doing.

27 Next Step? Try writing your own Temperature Logger program. If you have trouble, you can look at the sample Temperature Logger program and figure out how it works.


Download ppt "PICAXE BASIC The programming language used by the PICAXE microcontrollers is called BASIC. BASIC is a very early programming language and designed to make."

Similar presentations


Ads by Google