Top Banner
Hardware Meets Hardware Meets Software Software CPSC 120 CPSC 120 Principles of Computer Principles of Computer Science Science February 15, 2012 February 15, 2012
19

Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Dec 17, 2015

Download

Documents

Phillip Greene
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Hardware Meets Hardware Meets SoftwareSoftware

CPSC 120CPSC 120

Principles of Computer SciencePrinciples of Computer Science

February 15, 2012February 15, 2012

Page 2: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Quick Reminder: Programming as Quick Reminder: Programming as EncodingEncoding

Recall we write computer programs in high level Recall we write computer programs in high level languages such as PBASIC, Java, C++, etc.languages such as PBASIC, Java, C++, etc.

These commands are checked for correct spelling These commands are checked for correct spelling and use by a and use by a syntaxsyntax checker checker

If acceptable, our program is translated into If acceptable, our program is translated into machine specific code by a machine specific code by a compilercompiler

This object code or This object code or machine codemachine code is downloaded is downloaded to our target device, such as BS2, usually by to our target device, such as BS2, usually by serialserial cable or wireless link cable or wireless link

This machine code is stored in RAM then fetched, This machine code is stored in RAM then fetched, decoded and executed one instruction after decoded and executed one instruction after another until the program exitsanother until the program exits

Page 3: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Ideal view of the programming

process.

What is missing is the design

phase and the edit-run-test-edit loop of revision

and testing.

Page 4: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Basic Stamp Editor: An Integrated Basic Stamp Editor: An Integrated Development Environment (IDE)Development Environment (IDE)

Page 5: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Communication ChannelsCommunication Channels Notice we communicate with our BS2 through a Notice we communicate with our BS2 through a

serialserial link. One character after another sent link. One character after another sent along a wire. Examples are along a wire. Examples are USBUSB (Universal (Universal Serial Bus) and old fashioned Serial Bus) and old fashioned RS-232RS-232..

Bits in serial mode are encoded as voltage up or Bits in serial mode are encoded as voltage up or down, a pulse, during a short time interval.down, a pulse, during a short time interval.

This compares to This compares to parallelparallel mode of having 8 or mode of having 8 or more wires in a cable. Ex. Older printers.more wires in a cable. Ex. Older printers.

ASCIIASCII code (American Standard Code for code (American Standard Code for Information Interchange)is used to encode Information Interchange)is used to encode letters, digits, and control characters.letters, digits, and control characters.

Basic unit is 8 bits called a Basic unit is 8 bits called a bytebyte to encode a to encode a character. There are 256 possible characters.character. There are 256 possible characters.

UnicodeUnicode is a more modern extension of ASCII. is a more modern extension of ASCII.

Page 6: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

ASCII Code Chart

Page 7: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Integrated CircuitsIntegrated Circuits

Our CPU and RAM are manufactured as integrated circuits. The metal tabs are called pins and allow communication between the IC and connected circuits. The high/low or

Vdd/Vss values of certain pins can be set/controlled by our programs! Pin voltage can be read by our programs and thus allow our program to react accordingly. This is programmer

controlled input/output or programmed I/O.

Page 8: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Controlling Input/Output PinsControlling Input/Output Pins

Input/output pins on the Basic Stamp 2 IC are brought Input/output pins on the Basic Stamp 2 IC are brought out to and are numbered on our BOE board for access out to and are numbered on our BOE board for access via the breadboard.via the breadboard.

BS2 has 16 I/O pins available via BOE, labeled P0, P1, BS2 has 16 I/O pins available via BOE, labeled P0, P1, P2, …, P14, P15, we can use to read/write values. P2, …, P14, P15, we can use to read/write values.

A TRUE/HIGH value on a pin means +5 volts (Vdd), a A TRUE/HIGH value on a pin means +5 volts (Vdd), a FALSE/LOW value means 0 volts (Vss).FALSE/LOW value means 0 volts (Vss).

Using program control, we can turn an LED on/off if we Using program control, we can turn an LED on/off if we set up such a circuit on our breadboard connected to set up such a circuit on our breadboard connected to BS2 correctly!BS2 correctly!

As we can read pin values, we can also build As we can read pin values, we can also build sensor sensor circuits on our breadboard so our robot will be smarter.circuits on our breadboard so our robot will be smarter.

Page 9: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Creating a PBASIC program Creating a PBASIC program to control PIN valuesto control PIN values

Here is our hardware arrangement with the resistors and LEDs controlled from pins P12 and P13 on the BOE

Page 10: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

' Robotics with the Boe-Bot – HighLowLed.bs2 ' Turn the LED connected to P13 on/off once every second. ' Programmer: John Doe' Date Created: 9/20/2007' Last modified: 2/15/2012 ' {$STAMP BS2} ' {$PBASIC 2.5}

DEBUG "The LED connected to Pin 13 is blinking!" DO ' Beginning of loop

HIGH 13 ' Set pin 13 highPAUSE 500 ' Pause for 500 millisecondsLOW 13 ' Set pin 13 lowPAUSE 500 ' Pause again for 500 milliseconds

LOOP ' End of loop

A simple PBASIC program to blink an LED

Page 11: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Controlling Servo MotorsControlling Servo Motors Servo motorsServo motors are electric motors controlled by are electric motors controlled by

digital signals, perfect for use with digital signals, perfect for use with microcontrollersmicrocontrollers such as BS2 such as BS2

Servo motors have three attached wires: power Servo motors have three attached wires: power (+6V), ground, and control(+6V), ground, and control

Servo motors rotate left or right depending on Servo motors rotate left or right depending on the width of a pulse on the control wirethe width of a pulse on the control wire

The duration of a servo pulse is about 1.5 The duration of a servo pulse is about 1.5 thousands of a second. 1.5 milliseconds!thousands of a second. 1.5 milliseconds!

We have library commands in our PBASIC We have library commands in our PBASIC language to generate such very short pulses language to generate such very short pulses

Using two servos, we can drive a robot around!Using two servos, we can drive a robot around!

Page 12: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Servo motors attached to the Homework Board, our BOE-BOT connection is similar but easier!

Page 13: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

' Robotics with the Boe-Bot - CenterServoP13.bs2

' This program sends 1.5 ms pulses to the servo connected to

' P13 for servo centering. See BOE-BOT manual for details.

' Programmer: John Doe

' Date Created: 9/21/07

' Date Modified: 9/24/07

' {$STAMP BS2}

' {$PBASIC 2.5}

DEBUG "Program Running!"

DO

PULSOUT 13, 750 ' Send centering pulse to pin 13

PAUSE 20 ' Wait 20 milliseconds

LOOP

A PBASIC program to test centering of a servo attached to BOE

Page 14: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Review of Common Prefixes from SIReview of Common Prefixes from SIHere are some standard prefix modifiers for multiples of number values:Here are some standard prefix modifiers for multiples of number values:

Deca: means 10. Ex. A geometric decagon has 10 sides.Deca: means 10. Ex. A geometric decagon has 10 sides.Kilo: means 1000 = 10Kilo: means 1000 = 1033. Ex. A kilometer is 1000 meters.. Ex. A kilometer is 1000 meters.Mega: means 1,000,000 = 10Mega: means 1,000,000 = 1066. Ex. A megabye of RAM is (about) one . Ex. A megabye of RAM is (about) one million bytes. [Actually it is 2million bytes. [Actually it is 22020 = 1,048,576 in binary] = 1,048,576 in binary]Giga: means 1,000,000,000 = 10Giga: means 1,000,000,000 = 1099. Ex. A gigabyte of disk storage is . Ex. A gigabyte of disk storage is (about) one billion bytes of storage space. [2(about) one billion bytes of storage space. [23030 in binary] in binary]

Other prefixes are used for fractions:Other prefixes are used for fractions:Deci: means 1/10. Ex. To decimate is to destroy 1/10, also decimal.Deci: means 1/10. Ex. To decimate is to destroy 1/10, also decimal.Centi: means 1/100 = 1 x 10Centi: means 1/100 = 1 x 10-2-2. Ex. Centimeter = 1/100 of a meter.. Ex. Centimeter = 1/100 of a meter.Milli: means 1/1000 = 1 x 10Milli: means 1/1000 = 1 x 10-3-3. Ex. A millisecond is 1/1000 of a . Ex. A millisecond is 1/1000 of a second. Often denoted ms or msec. second. Often denoted ms or msec.

Micro: means 1/1,000,000 = 1 x 10Micro: means 1/1,000,000 = 1 x 10-6-6. Ex. A microsecond is 1 millionth . Ex. A microsecond is 1 millionth of a second. Often denoted of a second. Often denoted μμsec. [Greek letter mu]sec. [Greek letter mu]

Page 15: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Applications to PBASICApplications to PBASIC We have seen how the DEBUG command acts like a mathematical We have seen how the DEBUG command acts like a mathematical

functionfunction and takes and takes argumentsarguments, the list of items to display. Ex. , the list of items to display. Ex. DEBUG CR, “Hello”, CR, “there!”DEBUG CR, “Hello”, CR, “there!”

The PAUSE command takes one argument which is the number of The PAUSE command takes one argument which is the number of millisecondsmilliseconds to simply wait, doing nothing. to simply wait, doing nothing.

PAUSE 500 ‘ Wait 1/2 second before continuingPAUSE 500 ‘ Wait 1/2 second before continuing The math: 1 ms x 500 = 500/1000 second = 1/2 second.The math: 1 ms x 500 = 500/1000 second = 1/2 second.

The PULSOUT command takes two arguments: A BS2 pin number The PULSOUT command takes two arguments: A BS2 pin number and interval to create an up/down signal on that pin. However, the and interval to create an up/down signal on that pin. However, the basic unit used is NOT milliseconds but 2 microseconds!basic unit used is NOT milliseconds but 2 microseconds!

PULSOUT 12, 500 ‘ Create a pulse on pin 12 of 1 millisecondPULSOUT 12, 500 ‘ Create a pulse on pin 12 of 1 millisecond

The math: 2 The math: 2 μμs x 500 = 1000 s x 500 = 1000 μμs = 1 millisecond.s = 1 millisecond. So, this turns out to be exactly the kind of extremely short on/off So, this turns out to be exactly the kind of extremely short on/off

interval we need to send to a servo motor to rotate in a particular interval we need to send to a servo motor to rotate in a particular direction.direction.

Page 16: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

' Robotics with the Boe-Bot - CenterServoP13.bs2

' This program sends 1.5 ms pulses to the servo connected to

' P13 for manual centering. See BOE-BOT manual for details.

' Programmer: John Doe

' Date Created: 9/21/07

' Date Modified: 2/15/12

' {$STAMP BS2}

' {$PBASIC 2.5}

DEBUG "Program Running!"

DO

PULSOUT 13, 750 ' Send centering pulse to pin 13

PAUSE 20 ' Wait 20 milliseconds

LOOP

A PBASIC program to test centering of a servo attached to BOE

Page 17: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Variables in PBASICVariables in PBASIC Variables are simply named memory locations we can Variables are simply named memory locations we can

use for arithmetic, control of loops, and storing sensor use for arithmetic, control of loops, and storing sensor values. We choose the variable names!values. We choose the variable names!

You can declare four different types of variables in You can declare four different types of variables in PBASIC: PBASIC:

Type Type Stores This Range of Number Stores This Range of Number ValuesValues

Bit Bit 0 to 1 0 to 1

Nib Nib 0 to 15 0 to 15

Byte Byte 0 to 255 0 to 255

Word Word 0 to 65535 or -32768 to + 327670 to 65535 or -32768 to + 32767

We need to choose the appropriate variable type when we We need to choose the appropriate variable type when we plan on storing values in the variableplan on storing values in the variable

Page 18: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Variable Use and AssignmentsVariable Use and AssignmentsHere are some variable declarations and assignments (with some errors). Notice that PBASIC is not case-sensitive. You can use either upper or lower case letters for your code. Some other languages do not allow this!

‘ Variable declarations begin here

Counter VAR WORD

ON_OFF VAR BIT

Letter VAR BYTE

‘ Some simple assignments begin here

LETTER = 32

Letter = -1

ON_OFF = 4

Counter = 25

Counter = Counter * 100 + LETTER

DO

Counter = Counter + 1

LOOP

Page 19: Hardware Meets Software CPSC 120 Principles of Computer Science February 15, 2012.

Loops in PBASICLoops in PBASICWe can use several different types of looping constructions in PBASIC depending on what we need to do.

‘ Simple FOR…NEXT loop to drive a servo for about 1 second

counter VAR Word

FOR counter = 1 TO 46 ‘ Iterate this loop 46 times

PULSOUT 13, 850 ‘ Drive servo attached to pin 13 forward, 1.7 ms

PAUSE 20 ‘ Do nothing for 20 ms

NEXT ‘ Increment our counter variable, then top of loop

PULSOUT 13, 750 ‘ Stop the servo by using the centering value

This code “loops” or iterates through the two commands in the body of of the loop as the counter variable (started at 1) is automatically incremented by one each time through. When counter reaches 46, we do one last pass through the loop, then exit to go to the next PBASIC instruction.