PBASIC点滴

PBASIC INTRODUCTION
PBASIC stands for Parallax BASIC(Beginners All-purpose Symbolic Instruction Code) which is a variant of BASIC. This special language has familiar BASIC instructions such as FOR..NEXT, IF..THEN and GOTO along with some useful extra instructions that are specially for input and output (I/O). Programs can be written using the STAMP programming software and downloaded on a serial port to
the BASIC Stamp.


0.Notice how the template structure helps me keep the code organized and contains contact information about the author. This is a good idea if you choose to launch your programs into cyber-space.
' ---- [ Title ]---------------------------
' File...... NEW.BAS
' Purpose... PBASIC Programming Template
' Author.... Jon Williams
' E-Mail.... jonwms@aol.com
' WWW....... http://members.aol.com/jonwms
' Started...
' Updated...
' ---- [ Program Description ] ------------
'
' ---- [ Revision History ] ---------------
'
' ---- [ Constants ] ----------------------
'
' ---- [ Variables ] ----------------------
'
' ---- [ EEPROM Data ] --------------------
'
' ---- [ Initialization ] -----------------
'
' ---- [ Main Code ] ----------------------
'
' ---- [ Subroutines ] --------------------


1.PBASIC 1 allows three variable types: bits,bytes,and words.
  PBASIC 2 allows three variable types: bits,bytes,nibbles and words.
   A map of the Stamp 1’s user-variable space looks like this:
   
         

   PBASIC 2 allows three variable types: bits,bytes,nibbles and words.
   eg:Dog VAR BIT ‘Value can be 0 or 1
         Cat VAR NIB ‘Value can be 0 to 15
         Dolphin VAR BYTE ‘Value can be 0 to 255
         Whale VAR WORD ‘Value can be 0 to 65535

2.The keyword "SYMBOL" is equal to the keyword "parameter" in the verilog.

3.Just make sure that your program does not need to save the value of B3 (from our example) or you may get some unexpected behavior.
  eg:SYMBOL looper = B3
       SYMBOL addr = B3

4.After starting with a template, I use the following formatting conventions in my PBASIC programs:
• Variables use mixed-case and begin with a low-ercase letter. Examples: lcdChar,hrs, iCount
• Constants (including subroutine address labels) use mixed-case and begin with an uppercase letter. Examples: LGear, SndAlrm, DlyTm
• PBASIC keywords use all uppercase. Examples: FOR, NEXT, GOSUB,RETURN
• White space (spaces, tabs, etc.) is used liberally to improve the readability of the program. White space has no impact of the size of the compiled (tokenized) program, so feel free to use it. I suggest starting the working code in the first column following a tab and indenting code within FOR/NEXT loops with two spaces.

5.PBASIC is not case sensitive and therefore both upper case and lower case letters can be used in an identifier.

6.The sizes of the variable can vary from WORD, BYTE, NIB (nibble) or BIT.
eg:Dog VAR BIT ‘Value can be 0 or 1
    Cat VAR NIB ‘Value can be 0 to 15
    Dolphin VAR BYTE ‘Value can be 0 to 255
    Whale VAR WORD ‘Value can be 0 to 65535
Aliases for variables can also be created using the VAR command.
 For example:counter VAR BYTE
                   countodd VAR counter
In the lines above, countodd is an alias to the variable counter. Anything stored in counter shows up in countodd and vice versa. Both names will refer to the same physical address.
You can also use the alias as a window into a portion of another variable. This is done using “modifiers”.
For example:Whale VAR WORD ‘A 16-bit variable
                   Dolphin VAR Whale.HIGHBYTE ‘Highest 8 bits of Whale
                   Shark VAR Whale.LOWBYTE ‘Lowest 8 bits of Whale
The following table lists the modifiers and the definition of their use with variables.
Modifiers Definition
LOWBYTE Low byte of a word
HIGHBYTE high byte of a word
BYTE0 byte 0 (low byte) of a word
LOWNIB low nibble of word or byte
HIGHNIB high nibble of word or byte
NIB0 nib 0 of a word or byte
NIB1 nib 1 of a word or byte
NIB2 nib 2 of a word
NIB3 nib 3 of a word
LOWBIT low bit of a word, byte, or nibble
HIGHBIT high bit of a word, byte, or nibble
BIT0 bit 0 of a word, byte, or nibble
BIT1 bit 1 of a word, byte, or nibble
BIT2 bit 2 of a word, byte, or nibble
BIT3 bit 3 of a word, byte, or nibble
BIT4 ... BIT7 bits 4 through 7 of a word or byte
BIT8 ... BIT15 bits 8 through 15 of a word

7.Array Variables
You can also declare arrays using the VAR command.
eg:myarry VAR byte(3)
myarray(0) = 1
myarray(1) = 10
myarray(2) = 100
Using an array without the specified index will cause the software to respond with/to the 1st element in the array.

8.Input/Output direction
The direction of input and output pins on the Stamp can be controlled by setting the appropriate bits in the DIRS register: a ‘1’ indicates an output pin and a ‘0’ indicates an input pin.
For example: dirs = $F000
this is equivalent to setting I/O pins 15:12 as output pins while pins 11:0 are designated as input pins.
Separate registers are provided for input (INS) and output (OUTS) and can be assigned separately (IN0 or OUT1).

9.
Math expressions
Math operations are performed from left to right. There are no operator precedence rules except when it comes to UNARY and BINARY operators. Unary operators are given precedence in math calculations. For example:
10 - SQR 16
The BASIC Stamp first takes the square root of 16 and then subtracts it from 10.
In the case of binary operators, the expression:
5 + 3 * 2
will yeild the result 16 and not 11. Therefore, for proper calculation, the parentheses character can be placed in the expression.
5 + (3 * 2) = 11
Note: Only 8 levels of parentheses are allowed in your math expressions.
Math expressions can also be used when dealing with input and output pins. For
example:
B1 = 10
INPUT B1+1 ‘Make pin 11 an input pin

10.
Constants
Constants can be specified using the
CON keyword. For example:
delay CON 1000
Constants can also be defined in terms of another constant but must be kept fairly simple.
For example:
Whale CON 20
Dolphin CON Whale*2-1

Declaring Pin Numbers as Constants
One very useful application of constants is to stand for BASIC Stamp pin numbers. This practice makes a program more understandable.
eg:Led CON 8                     ' LED is connected to Pin P8
     Servo CON 12                ' Servo motor connected to Pin P12

Aliases
To declare an alias, enter the alias, the keyword VAR, and the name of the existing variable.
eg:btn VAR  IN7               ' name (alias) the input
     LED VAR OUT8            ' name (alias) the output

11.NUMBERS
PBASIC allows you to use several numbering systems. By default, it assumes that numbers
are in decimal, but you can identify binary and hexadecimal numbers with a prefix.
99 decimal
%1010 binary
$FE hex
BASIC Stamp performs integer match (whole numbers only) and drops any fractional portion
from the results of the computation. The size of the variables can be a bit (0-1), nibble
(0-15), byte (0-255) or word (0-65535) respectively.
For negative numbers, two’s complement math can be used for representation.

12.
Negative Numbers
When the Stamp performs calculations, if the value is greater than the size of the variable,
then it just chops off the extra bits from left to right to form the appropriate size. For example:
When adding $64 to $FFDF (DEC 100 and -33), the result would be $10043 but the
Stamp chops the extra bit(s) off, leaving the result to be $0043 = 67.

13.
Adding 2 large numbers
Sometimes there may be a need to add or subtract large numbers depending on the application. In order to do this, we can concatenate (not physically) several registers together. For example, suppose we wish to add 2, 32 bit numbers together:

Program Example 1:
W1 = $2
W2 = $FFFF
W3 = 0
W4 = 1
W5 = W2                          ‘temporary storage for W2
W2 = W2 + W4
IF W2 >= W5 then nocy     ‘if W2 < (old) W2
W1 = W1 + 1
nocy:
W1 = W1 + W3

Subtracting 2 large numbers

Program Example 2:
W1 = 0
W2 = 1
W3 = $2
W4 = $FFFF
W5 = W2                         ‘temporary storage for W2
W2 = W2 - W4
IF W2 <= W5 then nobor  ‘if W2 > (old) W2
W1 = W1 - 1
nobor:
W1 = W1 - W3

14.
Multiplying 2 large numbers
Multiplying 2 16 bit numbers requires a 32 bit result. The ‘*’ and ‘**’ operators can be usedfor this purpose.
* --> returns value of bottom 16 bits
** --> returns value of top 16 bits
For example:
Multiplication of $FFFF and $FFFF produces the result $FFFE0001
W1 = $FFFF
W2 = $FFFF
W3 = W1 * W2
W4 = W1 ** W2
Non-integer numbers
The BASIC Stamp can only handle whole integer numbers. Therefore, methods of conversion to integer numbers is necessary depending on your exact needs. For the expression:
F = 1.8 * C + 32
can be re-written as
F = 18 * C + 320
This conversion however means that the actual result 1/10th of the result obtained from the calculation.
One way to deal with fractional numbers is to use the ‘ */ ’ operator. It has the effect of multiplying a value by a whole number and a fraction.
This operator places the whole number portion in the upper byte, multiplies the fractional part by 256 and places the result in the lower byte.(divided by two multiplying parts)
For example, if multiplying value with 1.8, using the ‘ */ ’ would mean 0.8 * 256 = 204.8 = 205 and therefore:
Upper byte : 01
Lower byte : CD
so 1.8 can be represented as $01CD. Therefore, some care will need to be taken when dealing with non-integer numbers.
Now, F = 1.8 * C + 32 can be re-written as F = C */ $01CD + 32

15."
CR" is the key word which is used for Carriage Return(linefeed).
eg:DEBUG "Hello World!", CR
     DEBUG"and Hello again"
or
    DEBUG "Hello World!",CR,"and hello again"
Output:Hello World!
           and Hello again

Debug Control Characters
CR Carriage Return
CLS Clear screen, cursor positioned in upper left
HOME Moves cursor to upper left corner but doesn’t clear screen
BELL Makes a sound
BKSP Backspace
TAB Tab

Printing Variables using DEBUG
x VAR Byte
x = 65
DEBUG “Printing variables”, CR
DEBUG ? x ‘ Shorthand to print “x = “, value, CR
DEBUG x, CR ‘ ASCII value!
DEBUG DEC x, CR ‘ Decimal
DEBUG IBIN x, CR ‘ Indicated Binary, starts with % sign
DEBUG IHEX x, CR ‘ Indicated Hex, starts with $ sign
‘ Printing variables along with text
DEBUG “The temperature is “, DEC x, “ degrees”, CR

Debug Formatters
? Shorthand notation. Prints “var = <value>”, CR
DEC Decimal
IHEX Indicated hexadecimal
IBIN Indicated binary
STR String from BYTEARRAY
ASC ASCII
Note: The default is ASCII! To print a number, you must include a formatter.

16.The simplest conditional branching is done with IF-THEN construct. The PBASIC IF-THEN construct is different from other flavors of BASIC. In PBASIC, THEN is always followed by a valid program address (other BASICs allow a variety of programming statements to follow THEN). If the condition statement evaluates as TRUE, the program will branch to the address specified. Otherwise, it will continue with the next line of code.

17.
FOR-NEXT LOOP
FOR controlVar = startVal TO endVal STEP stepSize
statement 1
statement 2
statement 3
NEXT

18.A subroutine is a section of code that can be called (run) from anywhere in the program.
GOSUB is used to redirect the program to
the subroutine code. The subroutine is terminated with the
RETURN command. RETURN causes the program to jump back to the line of code that follows the calling GOSUB command.

19.
BASIC Stamp Memory
The BASIC Stamp has two kinds of memory; RAM (for variables used by your program) and EEPROM (for storing the program itself). EEPROM may also be used to store long-term data in much the same way that desktop computers use a hard drive to hold both programs and files.
An important distinction between RAM and EEPROM is this:
• RAM loses its contents when the BASIC Stamp loses power; when power returns, all RAM locations are cleared to 0s.
• EEPROM retains the contents of memory, with or without power, until it is overwritten(such as during the program-downloading process or with a WRITE instruction.)
The BS2, BS2e, BS2sx and BS2p have 32 bytes of Variable RAM space. Of these, the first six bytes are reserved for input, output, and direction control of the I/O pins. The remaining 26 bytes are available for general-purpose use as variables.

20.
I/O Registers
• Occupy first 3 words RAM (6 bytes)
• 16-bit registers (Stamp has 16 I/O pins)
• Are all initialized to zero
• All pins set to inputs by default

21.
Names of I/O Registers
• INS Shows state of I/O pins regardless whether input or output
• OUTS Write values into here to make pin high (1) or low (0)
• DIRS 0=Input, 1 = Output

22.
Reserved Names for Referring to I/O Registers
INS REGISTER
Name                                    Size
IN0 - IN15                             Bit
INA, INB, INC, IND                  Nibble
INL, INH                                Byte
INS                                      Word

OUTS REGISTER
Name                                   Size
OUT0 – OUT15                     Bit
OUTA, OUTB, OUTC, OUTD     Nibble
OUTL, OUTH                         Byte
OUTS                                  Word

DIRS REGISTER
0 = INPUT, 1 = OUTPUT
Name                                 Size
DIR0 .. DIR15                     Bit
DIRA, DIRB, DIRC, DIRD      Nibble
DIRL, DIRH                         Byte
DIRS                                 Word

23.
To Specify a Pin as Output
Since all pins default to inputs, you must specify which pins you wish to use as outputs. There are a number of ways to do this:
1. Use DIRS register
Write a "1" for "Output"
DIRS = %00110000000000000000            ' Outputs: 13, 12
DIR4 = 1                                                 ' Outputs: 4
2. Use OUTPUT keyword
OUTPUT 7                                                ' Outputs: 7
3. Use HIGH or LOW keywords
These set the direction, and write a value
HIGH 5                                                    ' Specifies that pin P5 is an output, and sets it high
LOW 3                                                    ' Specifies that pin P3 is an output, and sets it low
4. Use keywords that do it for you
No need to use OUTPUT or DIRS first
FREQOUT, PULSOUT, SEROUT, ...
原文地址:https://www.cnblogs.com/god_like_donkey/p/1567582.html