88 lines
17 KiB
HTML
88 lines
17 KiB
HTML
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|||
|
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Getting Started" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
|
|||
|
<head><title>First Program</title>
|
|||
|
<link href="../SkinSupport/MadCap.css" rel="stylesheet" />
|
|||
|
<link href="../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
|
|||
|
<script src="../SkinSupport/MadCapAll.js" type="text/javascript">
|
|||
|
</script>
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="Start.htm">Getting Started</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">First Program</span>
|
|||
|
</div>
|
|||
|
<p><a href="TestComm.htm" title="Go back to "Testing for Communication"" alt="Go back to "Testing for Communication""><img src="../Resources/Images/Prev.png" /></a><a href="LookUpAnswers.htm" title="Go on to "Looking Up Answers"" alt="Go on to "Looking Up Answers""><img src="../Resources/Images/Next.png" /></a>
|
|||
|
</p>
|
|||
|
<h1>Activity: First Program</h1>
|
|||
|
<p class="PlainText">The first program you will write and test will tell the BASIC Stamp to send a message to your computer.  The figure below shows how it sends a stream of ones and zeros to communicate the text characters displayed by your computer.  These ones and zeros are called <i>binary numbers</i>.  The BASIC Stamp Editor software has the ability to detect and display these messages as you will soon see. </p>
|
|||
|
<p class="PlainText">
|
|||
|
<img src="../Resources/Images/BitStream.png" />
|
|||
|
</p>
|
|||
|
<h2>First Program</h2>
|
|||
|
<p class="PlainText">In this Help tutorial, the program listings that you will type into the BASIC Stamp Editor and download to the BASIC Stamp module will be shown with a blue background like this:  </p>
|
|||
|
<h3>Example Program: FirstProgram.bs2</h3><pre class="SICcode" xml:space="preserve">' Stamps in Class - FirstProgram.bs2
|
|||
|
' BASIC Stamp sends message to Debug Terminal.
|
|||
|
 
|
|||
|
' {$STAMP BS2}
|
|||
|
' {$PBASIC 2.5}
|
|||
|
 
|
|||
|
DEBUG "Hello, it's me, your BASIC Stamp!"
|
|||
|
END</pre>
|
|||
|
<p class="PlainText">You will enter this program into the BASIC Stamp Editor. Some lines of the program are created automatically by clicking buttons on the toolbar.  Other lines are made by typing them in from the keyboard.</p>
|
|||
|
<ul>
|
|||
|
<li class="Checklist" value="1">Begin by clicking the BS2 icon (the green diagonal chip) on the toolbar.  If you hold your cursor over this button, its flyover help description “Stamp Mode: BS2” will appear. </li>
|
|||
|
<li class="Checklist" value="2">Next, click on the gear icon labeled “2.5.”  It’s flyover help description is “PBASIC Language: 2.5”.</li>
|
|||
|
</ul>
|
|||
|
<p>
|
|||
|
<img src="../Resources/Images/StampIconButton.png" />
|
|||
|
</p>
|
|||
|
<p class="PlainText">
|
|||
|
<img src="../Resources/Images/PBASICicon.png" />
|
|||
|
</p>
|
|||
|
<p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>ALWAYS use these toolbar buttons to add these two lines as the beginning of every program!   Compiler directives use braces {  }.  If you try to type in these parts of your program, you may accidentally use parentheses ( ) or square brackets [  ].  If you do this, your program will not work.</p>
|
|||
|
<p>If you are using some other 24-pin BASIC Stamp model instead of a BS2, you can still follow the activities here and in any Stamps in Class text, but you will need to adjust all time-sensitive commands or they will not work properly. Read <a href="../LanguageTopics/Reference/Adapt.htm" target="" title="" alt="" class="MCXref_0">Adapt BS2 Code to Other Models</a> for instructions. (It's under PBASIC Language Reference in the Table of Contents).</p>
|
|||
|
<ul>
|
|||
|
<li class="Checklist" value="1">Type in the remaining lines of the program exactly as shown: </li>
|
|||
|
</ul>
|
|||
|
<div class="background">
|
|||
|
<img src="../Resources/Images/FirstProgram.png" />
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li class="Checklist" value="1">Save your work by clicking File and selecting Save. </li>
|
|||
|
</ul>
|
|||
|
<img src="../Resources/Images/Save.png" />
|
|||
|
<ul>
|
|||
|
<li class="Checklist" value="1">Enter the name "FirstProgram" into the File name field near the bottom of the Save As window.</li>
|
|||
|
<li class="Checklist" value="2">Click the Save button.</li>
|
|||
|
</ul>   <img src="../Resources/Images/FileSave.png" /> 
|
|||
|
<p> </p><p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>The next time you save, the BASIC Stamp Editor will automatically save to the same filename (FirstProgram.bs2) unless you tell it to save to a different filename by clicking File and selecting Save As (instead of just Save).</p><ul><li class="Checklist" value="1">Click Run, and select Run from the menu that appears. </li></ul><p><img src="../Resources/Images/RunMenu.png" /></p><p class="PlainText">A Download Progress window will appear briefly as the program is transmitted from your computer to your BASIC Stamp module.  The figure below shows the Debug Terminal that should appear when the download is complete.  You can prove to yourself that this is a message from the BASIC Stamp by pressing and releasing the Reset button on your board.  Every time you press and release it, the program will re-run, and you will see another copy of the message displayed in the Debug Terminal.</p><ul><li class="Checklist" value="1">Press and release the Reset button.  Did you see a second “Hello…” message appear in the Debug Terminal?</li></ul><p><img src="../Resources/Images/DebugFirstProgram.png" /></p><p class="PlainText">The BASIC Stamp Editor has shortcuts for most common tasks.  For example, to run a program, you can press the ‘Ctrl’ and ‘R’ keys at the same time.  You can also click the <b>Run</b> button.  It’s the blue triangle that looks like a music player’s Play button.  The flyover help (the Run hint) will appear if you point at the Run button with your mouse.  You can get similar hints to find out what the other buttons do by pointing at them too.</p><p><img src="../Resources/Images/RunButton.png" /></p><h3>How FirstProgram.bs2 Works</h3><p class="PlainText">The first two lines in the example are called comments.  A comment is a line of text that gets ignored by the BASIC Stamp Editor, because it’s meant for a human reading the program, not for the BASIC Stamp module.  In PBASIC, everything to the right of an apostrophe is normally considered to be a comment by the BASIC Stamp Editor.  The first comment tells which book the example program is from, and the program’s filename.  The second comment contains a handy, one-line description that explains what the program does.</p><pre xml:space="preserve" class="snippet">
|
|||
|
' Stamps in Class - FirstProgram.bs2
|
|||
|
' BASIC Stamp sends message to Debug Terminal.</pre><p class="PlainText">Although comments are ignored most of the time, the BASIC Stamp Editor does search through comments for special directives.  Every program in this Getting Started section of help will use these two directives:  </p><pre class="snippet">' {$STAMP BS2}
|
|||
|
' {$PBASIC 2.5}</pre><p class="PlainText">The first directive is called the $STAMP Directive, and it tells the BASIC Stamp Editor that you will be downloading the program specifically to a BASIC Stamp 2 module.  The second directive is called the $PBASIC directive, and it tells the BASIC Stamp Editor that you are using version 2.5 of the PBASIC programming language.  These special comments are called compiler directives, and they are enclosed in braces { } not parentheses ( ). You should always use the toolbar icons to place these compiler directives in your program to avoid typing errors. Also, entering the compiler directives by hand may not activate the syntax highlighting in the BASIC Stamp Editor. That function is what causes various letters, characters and words in your program to appear in different colors and capitalization schemes.  Syntax highlighting makes your programs easier to read, understand, and correct if there are any bugs in them.</p><p class="PlainText">A command is a word you can use to tell the BASIC Stamp do a certain job.  The first of the two commands in this program is called the <span class="keyword_in_text">DEBUG</span> command:</p><pre class="snippet">
|
|||
|
DEBUG "Hello, it's me, your BASIC Stamp!"</pre><p class="PlainText">This is the command that tells the BASIC Stamp to send a message to the PC using the serial cable.  </p><p class="PlainText">The second command is the <span class="keyword_in_text">END</span> command:</p><pre class="snippet">END</pre><p class="PlainText">This command is handy because it puts the BASIC Stamp into low power mode when it’s done running the program.  In low power mode, the BASIC Stamp waits for either the Reset button to be pressed (and released), or for a new program to be loaded into it by the BASIC Stamp Editor.  If the Reset button on your board is pressed (or if you disconnect and reconnect your power supply), the BASIC Stamp will re-run the program you loaded into it.  If a new program is loaded into it, the old one is erased, and the new program begins to run.</p><h3>Your Turn – Delays with PAUSE, DEBUG Formatters, and Control Characters</h3><p class="PlainText">In <i>What’s a Microcontroller?</i> and <i>Robotics with the Boe-Bot</i>, the first command you will likely see in the example programs that display messages in the Debug Terminal is a 1-second delay, typically with the command <span class="code_in_text">PAUSE 1000</span>. The <span class="keyword_in_text">PAUSE </span>command delays the program for a certain number of milliseconds. Milliseconds are thousandths of a second are typically abbreviated ms. So, <span class="code_in_text">PAUSE 1000 </span>delays the program for 1000 thousandths of a second, which is one second.</p><ul><li class="Checklist" value="1">Modify the program by inserting <span class="code_in_text">PAUSE 1000</span> immediately above the <span class="keyword_in_text">DEBUG </span>command. </li></ul><pre xml:space="preserve" class="snippet">PAUSE 1000</pre><ul style="list-style-type: circle;"><li class="Checklist" value="1">Your code should then look like this:</li></ul><pre class="SICcode" xml:space="preserve">' Stamps in Class - FirstProgram.bs2
|
|||
|
' BASIC Stamp sends message to Debug Terminal.
|
|||
|
|
|||
|
' {$STAMP BS2}
|
|||
|
' {$PBASIC 2.5}
|
|||
|
|
|||
|
|
|||
|
PAUSE 1000
|
|||
|
DEBUG "Hello, it's me, your BASIC Stamp!"
|
|||
|
END</pre><ul style="list-style-type: circle;"><li class="Checklist" value="1">Run the modified program and verify that it delays for a second before displaying the Hello message.</li></ul><p class="PlainText">For comparison, you can disable the <span class="keyword_in_text">PAUSE </span>command by commenting it. In other words, add an apostrophe to its left so that it reads <span class="code_in_text">' PAUSE 1000</span>. By removing the apostrophe and re-running the program, you can then test how the program behaves without the <span class="keyword_in_text">PAUSE</span>.</p><ul style="list-style-type: circle;"><li class="Checklist" value="1">Try it.</li></ul><p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>Inserting a one second delay before the BASIC Stamp transmits messages to the Debug Terminal ensures that the Windows operating system cannot possibly mistake the BASIC Stamp for a plug-and play-serial device like a mouse or keyboard. This can happen if the BASIC Stamp is running a program that immediately transmits messages to the Debug Terminal when it gets connected to a USB port. It can also happen if the same program is running as the computer boots while it is connected to a serial or USB port. The <span class="code_in_text">PAUSE 1000</span> ensures that this case of "mistaken microcontroller identity" won't happen because it waits longer than the 0.7 second window that PCs give serial plug-and-play devices to identify themselves.</p><h3>DEBUG Formatters and Control Characters</h3><p class="PlainText">A <span class="keyword_in_text">DEBUG </span>formatter is a code-word you can use to make the message the BASIC Stamp sends look a certain way in the Debug Terminal. DEC is an example of a formatter that makes the Debug Terminal display a decimal value. An example of a control character is CR, which is used to send a carriage return to the Debug Terminal. The text or numbers that come after a CR will appear on the line below characters that came before it. You can modify your program so that it contains more<span class="keyword_in_text"> DEBUG</span> commands along with some formatters and control characters. Here’s an example of how to do it:</p><ul><li class="Checklist" value="1">Modify the comments at the beginning of the program so they read:</li></ul><pre xml:space="preserve" class="snippet">
|
|||
|
' Stamps in Class - FirstProgramYourTurn.bs2
|
|||
|
' BASIC Stamp sends messages to Debug Terminal</pre><ul><li class="Checklist" value="1">Add these three lines between the first <span class="keyword_in_text">DEBUG </span>command and the <span class="keyword_in_text">END</span> command:</li></ul><pre xml:space="preserve" class="snippet">
|
|||
|
DEBUG CR, "What's 7 X 11?"
|
|||
|
DEBUG CR, "The answer is: "
|
|||
|
DEBUG DEC 7 * 11</pre><ul><li class="Checklist" value="1">Save the changes you made by clicking File and selecting Save As. A good name would be FirstProgramYourTurn.bs2</li><li class="Checklist" value="2">Check your work against the example program shown here.</li><li class="Checklist" value="3">Run your modified program.  You will have to either select Run from the Run menu again, or click the Run button.</li></ul><p><img src="../Resources/Images/FirstProgramYT.png" /></p><ul><li class="Checklist" value="1">Check your Debug Terminal - does it now look like this?</li><li class="Checklist" value="2">If not, correct your program and re-run it until you get the results you expect.</li></ul><p><img src="../Resources/Images/DebugFirstProgYT.png" /></p><p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>Sometimes the Debug Terminal gets hidden behind the BASIC Stamp Editor window.  You can bring it back to the front by using the Run menu as shown, the Debug Terminal 1 shortcut button on the toolbar, or the F12 key on your keyboard.<br /><br /><img src="../Resources/Images/RefindDebugTerminal.png" />          <img src="../Resources/Images/Debug1Button.png" /><br /><br /></p><p><a href="TestComm.htm" title="Go back to "Testing for Communication"" alt="Go back to "Testing for Communication""><img src="../Resources/Images/Prev.png" /></a><a href="LookUpAnswers.htm" title="Go on to "Looking Up Answers"" alt="Go on to "Looking Up Answers""><img src="../Resources/Images/Next.png" /></a></p><p> </p><div class="Col2"><div class="MasterFoot"><p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../HomeTopics/HomePage.htm">Go to Welcome page</a></p></div></div><div class="Col2"><div class="MasterFoot"><p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p><p style="text-align: right;">Copyright © <span class="ContactInfoCompanyName">Parallax Inc.</span></p><p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span></p></div></div><script type="text/javascript">/* <![CDATA[ */
|
|||
|
|
|||
|
var _gaq = _gaq || [];
|
|||
|
_gaq.push(['_setAccount', 'UA-285614-1']);
|
|||
|
_gaq.push(['_trackPageview']);
|
|||
|
|
|||
|
(function() {
|
|||
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
|||
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|||
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
|||
|
})();
|
|||
|
|
|||
|
/* ]]> */</script><script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"></script></body>
|
|||
|
</html>
|