BasicStamp II Language Quick Tutorial - PowerPoint PPT Presentation

About This Presentation
Title:

BasicStamp II Language Quick Tutorial

Description:

REV reverse some low order bits (101000 REV 4) == 100001. Comparison and Logical Operators ... 12..15 are defined, but not on the phone. Serial I/O. Shiftin ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 41
Provided by: ScottH104
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: BasicStamp II Language Quick Tutorial


1
BasicStamp II Language(Quick) Tutorial
2
Lexical Aspects
  • Line oriented language (one stmt per line)
  • Multiple statements separated by
  • Lines limited to 256 chars, extended with
    trailing _
  • Each line may have a label at start (label)
  • Comments start with single quote ' (to EOL)
  • Identifiers like other languages
  • Including underscore
  • Not case sensitive

3
Lexical Aspects
  • Literals
  • Decimal 123
  • Hex FF
  • Binary 10101110
  • Chars A
  • Strings string
  • Only useful in parameters to certain built in
    statements
  • Means s, t, r, i, n, g

4
Variables
  • Flat global name space
  • Variable declaration
  • var
  • bit nib byte word (nibnibble4 bits)
  • 1, 4, 8, 16 bits values
  • E.g., counter var byte
  • Arrays
  • var ()
  • e.g., table var byte(5)

5
Variables (cont.)
  • Aliases (two names for the same variable)
  • var
  • var .
  • bit0 bit1 bit2 bit15
  • highbit lowbit
  • nib0 nib3 highnib lownib
  • byte0 byte1 highbyte lowbyte
  • Example LED_pin var outs.bit5

6
Constants
  • Declaration
  • con
  • Limits on
  • Evaluated left to right, no parens
  • Example sw_time con 41 1

7
Expressions
  • 8 and 16 bit unsigned integers
  • Operators
  • -/ (all as in C or Java)
  • top 16 bits of multiplication
  • / mult top 8 as in, low 8 as fraction
  • // remainder (mod)
  • Evaluated left-to-right (wrong precedence!)
  • Parens allowed to change order of eval

8
More Operators
  • ABS absolute value
  • MIN, MAX min and max (5 MIN 3)
  • SQR integer square root
  • SIN, COS sine cosine
  • Input 0..255 for 0..360deg
  • Output 127127

9
More Operators
  • DCD decode creates bit mask w/ 1 bit set
  • (DCD 2) 00000100 bits numbered 0..7
  • NCD encode find highest bit set
  • (NCD 01001000) 7 bits numbered 1..8 (!)
  • (NCD 00000000) 0
  • REV reverse some low order bits
  • (10101000 REV 4) 10100001

10
Comparison and Logical Operators
  • The usual comparison operators
  • Logical operators
  • not and or xor
  • Zero is false, non-zero is true

11
Assignment Conditionals
  • ()
  • .
  • If Then

12
Control Statements
  • For To Step
  • Next
  • Note always executes at least once through loop!
  • Goto
  • Branch , ,
  • Labels numbered from 0
  • If index is larger than label set no jump is made

13
Control Statements (cont.)
  • Gosub
  • call basic subroutine at label (ending at return)
  • no parameters
  • Return
  • return from most recently called subroutine

14
Control Statements (cont.)
  • End
  • stop execution and enter low power mode
  • I/O pins retain state
  • Stop
  • stop execution but dont enter low power mode
  • I/O pins retain state
  • Control starts at the first statement in your
    code
  • It is possible to run off the bottom of your
    program (apparently starts back at the top??)

15
Placing Data in EEPROM Memory
  • Non-volatile memory
  • keeps values w/o power
  • Data , ,
  • Data , ,
  • Fill EEPROM with values
  • Optionally defining constant to starting address
  • Values are placed at program download time not
    run-time

16
Placing Data in EEPROM Memory
  • Read ,
  • Write ,
  • Read / write a value from / to given EEPROM
    address

17
Sleeping and Low Power Mode
  • PIC has low power mode (draws 1?A)
  • Typical low power strategy sleep most of the
    time
  • Sleep
  • Nap
  • Timing is not extremely accurate
  • NAP Codes
  • 0 18msec
  • 36msec
  • 72msec
  • 144msec
  • 288msec
  • 576msec
  • 1.152sec
  • 2.304sec

18
Accurate Delays (Full Power)
  • Pause
  • Do nothing for given number of milliseconds
  • Example pause 500 ½ second delay

19
Misc. Statements
  • Lookup ,, ,
  • Table lookup for 8 or 16 bit values
  • Constant from Nth position (from 0) goes in
  • Strings count as multiple single character
    entries
  • Out of range index causes no action

20
Misc. Statements
  • Lookdown , ,
  • Search for a value in an 8-bit constant table
  • Strings equivalent to list of single characters
  • Var gets index of matching value (0 based
    indexing)
  • If not found then no action taken
  • Lookdown , ,
  • First that given comparison succeeds
  • Random
  • Generate 16-bit random number using var as seed
    (and result)

21
I/O Related Statements
  • Both low level , simple functions
  • Input, Output, High, Low
  • And high level, complex functions
  • I2Cin, SerIn, etc.

22
Statements for Low Level I/O
  • Input , Output
  • Establish direction of single pin
  • Reverse
  • Reverse direction of a single pin
  • High , Low
  • Implies output (most I/O commands imply
    input/output)
  • Same as Output
  • Toggle
  • Invert value of pin
  • To read input value

23
Additional I/O
  • Button , , , , ,
    ,
  • Wait for debounced repeated button
    press/release on pin
  • Have a look at the manual

24
Additional I/O
  • Pulsein , ,
  • Measure width of high/low pulse on pin
  • 0 low pulse, 1 high pulse
  • Returns in units of 2?sec (0 for too long /
    never)
  • Pulseout ,
  • Emit measured pulse in units of 2?sec
  • High/low depends on prior state (toggles pin
    twice)
  • Count , ,
  • Count pulses occurring within given time (in
    msec)

25
Analog I/O
  • RCTime , ,
  • Measure time pin stays in current state
  • 2?sec units
  • Typical use charge pin then measure time it takes
    RC circuit to drain
  • See example from last slides

26
Analog and Audio I/O
  • PWM , ,
  • Pulse width modulation output on pin
  • PWM signal is used to efficiently drive e.g., DC
    motor at fractional speed
  • High only some percentage (duty cycle)
  • 0 0 on, 255 100 on
  • Delivers of full power
  • note not clean square wave
  • in msec units

27
Analog and Audio I/O
  • FreqOut , ,
  • FreqOut , , ,
  • Output sine wave(s) at given frequency
  • Use filter capacitors for reasonable sound

28
Analog and Audio I/O
  • DTMFOut , , ,
  • DTMFOut , , , , ,
  • Output telephone touch tones (DTMF)
  • Generated with FreqOut
  • Needs low pass filter (freqout)
  • Values 0..15
  • 10 is
  • 11 is
  • 12..15 are defined, but not on the phone

29
Serial I/O
  • Shiftin
  • , , , ,
    \,
  • Shiftout , , , ,
    \,
  • Serin , , ,
  • Serout , , ,
  • items can be qualified with formatting
    information
  • e.g. dec for ascii decimal encoding
  • Also can do flow control and timeouts (see
    manual)
  • Debug , ,

30
About Serial Output
  • RS232C is standard for serial communications
  • EIA recommended standard from the early 60s
  • Designed for modems
  • Uses odd voltages (from modern perspective)
  • Logical 1 (mark) 15..-3v
  • Logical 0 (space) 3..15v
  • Stamps can put out 0 and 5v. so we have a
    problem

31
Level conversion for RS232
  • Maxim makes a single chip (powered by only 5v)
  • Built into BS II (used for Debug only)
  • Can buy HW that fits inside connector case (15)
  • http//www.sxlist.com/techref/io/serial/RCL1.htm

32
Or you can cheat for about .04
  • It turns out that most PC serial ports have a
    wide margin of things they will accept
  • If you invert the signal (1 0v 0 5v) it
    turns out that most PC serial ports will accept
    it as RS232!
  • Special modes for Serin (e.g., N9600) to do this
  • PICs have over/under voltage protection on pins
  • Negative voltage clamped and read as logic 0
  • Voltage 5V also clamped and read as 1
  • Because of details, need current limiting
    resistors in series

33
Serial cheater cable
  • Has worked on all (both) PCs Ive tried
  • Failed on 1 Mac I tried

34
Debugging Strategies
  • When programming you dont really spend your
    time/effort writing code, you spend it debugging
    the code when you get it wrong (which is pretty
    much always)
  • Embedded systems are particularly hard
  • Is it hardware or software?
  • Impoverished debug environment
  • Few tools
  • Low visibility
  • Timing may be an issue

35
Software Debugging in PBP
  • Have the equivalent of printf (debug)
  • If you have the code space
  • And you are not timing dependent
  • Can get small serial driven LCD displays
  • Can also do things like flash LED on Pin
  • E.g., unique patterns indicating that certain
    pieces of code are being executed

36
Hardware debugging
  • Preemptive debugging (AKA testing)
  • Seriously test your circuits before you use them
  • Start with ensuring power doesnt conduct to
    ground
  • Check that connections actually conduct
  • Check that adjacent soldered holes arent shorted
  • Multimeter for basic continuity checks
  • Double check that you have connected everything
  • Double check that you have connected it right
  • Polarity, etc.
  • Check that you have power (battery ok)

37
Hardware debugging
  • Logic probe
  • Very useful to checking that basic signals you
    expect are showing up on the pin you expect
  • Clipped to power and ground, needle touches
    point to probe
  • Indicates 0/1
  • Also shows fast pulses long enough to see

38
Hardware Debugging
  • Oscilloscope
  • Shows graph of actual voltages over time

39
Hardware Debugging
  • Can scale time (horiz) or voltage (vert)
  • Can typically trigger
  • Start graph at point of some event
  • E.g., first rise
  • Graph allows time measurements
  • E.g., see at right that pulse lasts just under
    1msec
  • Some allow dual trace
  • Allows comparisons

40
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com