TECH 45 Applications of Microcontrollers Module 1, Part B - PowerPoint PPT Presentation

Loading...

PPT – TECH 45 Applications of Microcontrollers Module 1, Part B PowerPoint presentation | free to view - id: c8160-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

TECH 45 Applications of Microcontrollers Module 1, Part B

Description:

trunk. PBASIC pause. Use: Pause x to pause for x milliseconds ... Automobiles use numerous microcontrollers for measuring performance, controlling ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 38
Provided by: J3141
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: TECH 45 Applications of Microcontrollers Module 1, Part B


1
TECH 45Applications of MicrocontrollersModule
1, Part B
  • Parallax Basic Programming
  • Basic Stamp 2, PBasic Editor
  • Useful Commands
  • Useful Techniques
  • Instructor James Millar

2
Basic Stamp Programming
  • Basic Stamp Editor
  • PBasic Programming

3
Basic Stamp 2, PBasic Editor
Run Menu Run downloads code into Stamp Check
Syntax verifies compiler knows each
command written Memory Map shows how the
Stamps Memory is utilized Debug creates a
window so you may view outputs from your
Stamp Identify determines what kind of Stamp is
connected and if it is connected properly
Inserts BS2 directive. This tells the editor to
create the firmware for your version of the Basic
Stamp. It is necessary that you hit this
button before selecting the Run command.
4
Basic Stamp Programming
  • Basic Stamp Editor
  • PBasic Programming
  • Programming in General
  • Debug and Number Systems
  • Data Types
  • Input and Output Methods
  • Memory Map revisted
  • Program Pause
  • Labels Goto
  • Calculations
  • Decisions and Comparison Operators
  • Looping
  • Example Application

5
Programming the ParallaxBasic Stamp 2
  • How many students have programmed before?
  • What languages have you programmed in?
  • Structured or Object Oriented?
  • Your mothers Basic or Microsoft Visual Basic?
  • Assembly, C, C, Java?

6
Programming in General
  • There are many types of programming paradigms
  • The two most popular are
  • structured and object-oriented
  • Structured programming involves breaking down a
    problem into manageable pieces, then building the
    larger program from those pieces
  • Object-Oriented Programming (OOP) involves
    starting with the big picture, and adding
    increasing levels of complexity to it
  • Object-oriented programming is especially good
    for large projects that may require considerable
    modification.

7
PBASIC on the BS2
  • PBasic - this is your mothers Basic!
  • like gwbasic, QuickBasic, Turbo Basicetc
  • NOT like Visual Basic (an object oriented
    language)
  • Why?
  • We dont require a complex programming language
    for monitoring and controlling devices.
  • But we often need specialized built-in support
    for other tasks like monitoring pushbuttons,
    performing Analog to Digital conversion, and
    simple low level access to memory
  • Microcontrollers are almost never programmed in
    an object oriented language since the software
    needs to be compact, and has only one or a few
    simple tasks to carry out.

8
PBasic Comments
  • Excellent code has lots of comments.
  • Poorly written code no matter how well it
    functions has no comments
  • Something like 50-80 of the software design life
    cycle involves maintaining and upgrading old
    code. It is therefore extremely important that
    you place comments throughout your code so that
    you and others can understand why you did what
    you did for years to come. Heck, even
    understanding your code the next day can be hell
    without comments!
  • Comments begin with a single apostrophe in PBasic
  • The PBasic Tokenizer will ignore all of your
    comments. Comments are only for us mortals.

9
PBasic Comments
10
PBasic on the BS2 Debug
  • The Basic Stamp 2 makes it easy for you to debug
    your code.
  • Example
  • Debug Hello World
  • You may concatenate your output with commas
  • Debug Hello ,my ,World
  • It is often best to add a carriage return for
    clarity
  • Debug Hello World, CR
  • The Debug command will automatically bring up a
    window showing you the output
  • When we write characters within quotes, we refer
    to this as a string.

11
PBasic The Debug Window
12
PBasic Debug Number Systems
  • Debug by default displays everything as its ASCII
    character.
  • ASCII stands for American Standard Code for
    Information Interchange and the set of ASCII
    codes and their function can be found at
    http//www.asciitable.com/
  • For example, if I try the following
  • Debug Alphabets first letter is ,65,CR
  • I would see Alphabets first letter is A,
    because 65 is the ASCII code for the letter A.
  • If I wanted a number, I would have to say
  • Debug Senior citizens are age ,DEC 65, and
    older,CR

13
PBasic Debug Number Systems
  • With Debug, we use DEC, BIN and HEX to specify
    the output of a, decimal, binary or hexadecimal
    number
  • When assigning a value to a variable, use the
    following identifiers , and to specify the
    number as binary or hexadecimal.
  • ADecimalNumber23
  • ABinaryNumber 1011
  • AHexNumber 41 number 65 in decimal or base
    10
  • Debug This is Hex for 65,HEX AHexNumber,CR
  • see the next example

14
PBasic Debug Number Systems
  • What would the output be for

15
PBasic Programming, data types
  • Basic data types
  • Bit represents the smallest unit of data, a
    logic 1 or 0 only
  • Nib a nibble of data, 4 bits
  • Byte 8 bits or 2 nibbles worth of data
  • Word 16 bits or 2 bytes worth of data
  • CON a constant of any type
  • Half a byte is a nibble
  • Examples
  • OutputState VAR Bit
  • Counter VAR Nib
  • OutputState 1
  • Counter 0

16
PBasic Programming, Array Data type
  • Sometimes we wish to group data using arrays
  • Why
  • same formula to be applied to a common set of
    data
  • Common object data grouped together for program
    readability and easier data analysis
  • Example
  • MaxMotors CON 2 Two motors to steer electric
    vehicle
  • LeftMotor CON 0
  • RightMotor CON 1
  • MotorSpeed VAR Byte(MaxMotors) Our Array for 2
    motors
  • MotorSpeed(LeftMotor)100 rev/min assigning
    values to array
  • MotorSpeed(RightMotor)90 rev/min

17
PBasic ProgrammingGetting data to the user
  • DIRS - set direction on all pins
  • DIRS0 all pins are input
  • DIRx - set pin xs direction
  • DIR00 pin 0 is an input
  • DIR121 pin 12 is an output
  • INPUT x - set pin x to input
  • INPUT 5 pin 5 is an input
  • OUTPUT x - set pin x to output
  • OUTPUT 7 pin 7 is an output
  • OUTx - set pin x high (5v)
  • OUT121 pin 3 outputs 5v
  • INx reads the value of pin x
  • WhatIsIN7 stores Pin7 value in
  • variable WhatIs.

Set the direction of the pin, apply a value if it
is an output.
note OUTx requires the pin direction to
be set before use!
18
IN/OUT/DIRS and Memory
Remember this view of the memory map? Lets take
a closer look at how we set the values.
19
IN/OUT/DIRS and Memory
20
A Quick Example
How would you do this with the DIRx command?
21
Getting data to the user
  • HIGH x - set pins direction to output and make
    that output value a logic 1 (5v)
  • HIGH 0 sets pin 0 to output
  • logic 1
  • LOW x - set pin xs direction
  • LOW 1 sets pin 1 to output
  • logic 0

or set the direction and value in just one step.
22
Careful!!
  • Review What precautions must you take when using
    the Board of Education?
  • Can previously run software kill a perfectly
    good, new design?
  • If so, how can you prevent this from happening?

23
PBASIC pause
  • Many processes in real life involve waiting for a
    period of time
  • Examples (from the microcontrollers/microprocesso
    rs perspective)
  • Hold the red, green or yellow lights of a stop
    sign for X seconds
  • Ring a phone for 3 seconds, then wait for 2
    seconds
  • Sample analog data (voice) for digitization at a
    rate of 64kbits/s
  • 1/64000 15.625 microseconds (period between
    samples)

Digital trunk
24
PBASIC pause
  • Use
  • Pause x to pause for x milliseconds
  • Pause 500 to pause program execution for a half
    second.

25
PBASIC Labels and GOTO
  • Labels in PBASIC define the start of program
    blocks and give the Stamp tokenizer a reference
    for the GOTO command
  • GOTO command allows the programmer to jump
    program execution from one line to another (using
    labels)
  • The GOTO command has been eliminated in perhaps
    all modern programming languages. Why?
  • Example

Goto Middle Start Goto Last Middle Goto
Start Last stop
26
PBasic Example
27
PBASIC Mini Project
  • Modify the previous program to incorporate the
    following
  • use variables and constants where appropriate to
    illustrate the following
  • The pin to be used for input and output which
    wont change
  • make the LED off period shorter by 100
    milliseconds after each stage of the loop.
  • What is the result?

28
Simple Calculations in PBasic
29
PBasic - Decisions
  • IF / THEN structure
  • IF Condition THEN Address
  • Interpretation
  • If the Condition is TRUE then, go to the label
  • Example

KeepDriving timetime1 DistanceSpeedtime IF
Distance100 THEN SlowDownCar GOTO
KeepDriving SlowDownCar SpeedSpeed-1 IF
Speedlt1 THEN TestFinished GOTO SlowDownCar TestFi
nished Stop
30
PBasic - Decisions
More IF / THEN Examples IF Quantity lt
((104)-(23)) THEN LoopLabel IF (Quantity2) ltgt
10 THEN NotEqualLabel IF NOT (Quantity2) 10
THEN NotEqualLabel IF Apples5 AND Orangeslt10
THEN HaveFruitLabel
31
PBasic, Looping
  • Weve seen how to loop continuously with GOTO,
    and exit those loops with IF / THEN, but there is
    a better way
  • FOR NEXT
  • FOR Counter StartValue TO EndValue Step
    StepValue
  • do something here between the FOR and NEXT
  • NEXT
  • Once the NEXT statement is reached, the program
    adds 1 to the variable Counter and continues.
  • Looping is terminated when Counter is outside the
    range set by StartValue to EndValue
  • Stepping for negative stepping, you can make
    StartValue larger than EndValue and keep any
    StepValue positive (if present).

32
An Example Application
  • Projects that solve a need in society are
    potentially good for starting up your own
    business.
  • Automobiles use numerous microcontrollers for
    measuring performance, controlling brakes for
    increased stability (intelligent cars), and
    deciding when to automatically phone for help
    (car accident, air bag goes off notify the
    police or call centre).
  • What about auto repair? Wouldnt it be nice if
    we knew when our brakes required changing instead
    of assuming so? Wouldnt it be nice to know if
    only the cheap brake pads require changing, not
    the entire assembly!
  • Lets implement a simple program to notify
    drivers when their brake pads require service.
  • To do this, Ill introduce the concept of arrays
    first

33
PBasic Programming, Array Data type
  • Sometimes we wish to group data using arrays
  • Why
  • same formula to be applied to a common set of
    data
  • Common object data grouped together for program
    readability and easier data analysis
  • How
  • Lets measure our cars brake pad thickness - for
    EACH wheel, and check to see if the brakes
    require servicing by using an array
  • BrakePadThickness VAR Byte(4)
  • To index each brake pads thickness value, do the
    following
  • BrakePadThickness(0) minimum index starts at
    0
  • BrakePadThickness(3) maximum index ends at n-1

34
PBasic Programming, Arrays
BrakePadThickness VAR Byte(4) array from 0
to 3 for each brake BrakePercentThickness VAR
Byte(4) of each brake left over ScalingFactor
VAR Byte Converts measured value to
ScalingFactor2 Acquire RAW Data on Brake Pad
Thickness BrakePadThickness(0)1
BrakePadThickness(1)4 BrakePadThickness(2)3
BrakePadThickness(3)2 Determine or set the
Scaling Factor to get thickness to a percent
BrakePercentThickness(0)BrakePadThickness(0)Scal
ingFactor BrakePercentThickness(1)BrakePadThick
ness(1)ScalingFactor BrakePercentThickness(2)B
rakePadThickness(2)ScalingFactor
BrakePercentThickness(3)BrakePadThickness(3)Scal
ingFactor
35
  • Array example to check notify driver if brake
    pad
  • thickness is unacceptable
  • BrakePadThickness VAR Byte(4) array from 0 to 3
    for each brake
  • BrakePercentThickness VAR Byte(4) of each
    brake left over
  • ScalingFactor VAR Byte Converts measured value
    to
  • Acquire Data on Brake Pad Thickness
  • BrakePadThickness(X)Y
  • Determine or set the Scaling Factor to get
    thickness to a percent
  • ScalingFactorAB/CD
  • For C0 to 3 same formula to scale thickness to
    a percent
  • BrakePercentThickness(C)BrakePadThickness(C)Sc
    alingFactor
  • Next
  • C0 initialize array index to the start
  • CheckThickness quick data analysis on identical
    objects
  • if (BrakePercentThickness(C)lt4) then
    NotifyInspectBrakes

36
PBasic Programming
  • What did you learn from the previous example?
  • Was it easy to see how arrays might be useful?
  • Other programming tips
  • Use indentation to block off common sections
  • Use white space to reduce clutter
  • Comment, Comment, Comment your work!
  • Any poor programming styles to avoid?
  • GOTO statements are frowned upon - why?
  • Array limits were hard-coded - why bad?

Your programs can get complicated, so plan before
you start, write neat code, and comment, comment,
comment!!!
37
The Complete Brake Inspection Source Code
About PowerShow.com