300 likes | 462 Views
Introduction to Programming with the BASIC Stamp. Programming Unit, Lecture 1. What is a micro-controller? (microcomputer, microprocessor, embedded controller). Microcomputer with self contained ROM & RAM Read Only Memory for program storage (Flash or OTP)
E N D
Introduction to Programmingwith the BASIC Stamp Programming Unit, Lecture 1 Introduction to Programming
What is a micro-controller?(microcomputer, microprocessor, embedded controller) Microcomputer with self contained ROM & RAM Read Only Memory for program storage (Flash or OTP) Random Access Memory for data storage CPU for logical and arithmetic operations Interfaces for external connections • Micro-controllers are everywhere • About 3 for every person on earth • A BMW 7-series automobile has 65 microprocessors Introduction to Programming
Why do I need a micro-controller? A small, inexpensive alternative to a PC. Ideal for battery operated applications. Easier to program than a PC. Easy to interface to other electronics. • More flexible than “hardwired” logic. • Changes made by editing software v. soldering iron. • Can emulate many logic IC’s and other components. • Additional functionality can be easily added. Introduction to Programming
Micro-controllers Micro-controllers are manufactured by numerous companies. Many target specific applications automotive industrial consumer appliance Some micro-controllers are particularly useful in prototype or educational environments. BASIC Stamp PIC (Microchip, Inc.) Rabbit (Rabbit Semiconductor) Atmel Introduction to Programming
The BASIC Stamp BS2P24 Size 1.2” x 0.6” Speed 20MHz ~12,000 instructions/sec 8x2K bytes EEPROM prog storage ~4,000 instructions 16 user I/O pins 2 I/O pins for serial comm 0 to 70 degrees C environment Introduction to Programming
BS2P24 Module 5 - 12 volts power supply Low power “sleep” mode RAM Size 128 Bytes scratchpad 26 variables Introduction to Programming
BASIC Stamp Development System Hardware platform for BASIC Stamp Power supply Prototyping area Connection for downloading compiled code Host PC STAMP Editor Documentation CD on-line from www.parallax.com Introduction to Programming
BASIC Stamp Development Systems BS2P Demo Board form Parallax, Inc. Connection to PC Introduction to Programming
BASIC Stamp Development Systems • Parallax, Inc. Board of Education Introduction to Programming
BASIC Stamp Development SystemsCANSAT v. 7 • DC-DC converter • Memory • MODEM • Expansion pins Introduction to Programming
BASIC Stamp Instruction Set Overview DIGITAL I/O ASYNC SERIAL I/O SYNC SERIAL I/O PARALLEL I/O ANALOG I/O SOUND FLOW CONTROL EEPROM ACCESS RAM ACCESS NUMERICS TIME DEBUG POWER CONTROL Introduction to Programming
FLOW CONTROL Instructions IF…THEN Compare and conditionally branch. BRANCH Branch to address specified by offset. GOTO Branch to address. GOSUB Branch to subroutine at address. RETURN Return from subroutine. RUN Switch execution to another program page. POLLRUN Switch execution upon polled interrupt. FOR..NEXT Establish a FOR…NEXT loop. Introduction to Programming
MEMORY ACCESS Instructions DATA Store data in EEPROM READ Read EEPROM data into variable WRITE Write byte into EEPROM STORE Switch READ/WRITE access program slot. GET Read Scratch Pad RAM byte into variable. PUT Write byte into Scratch Pad RAM. Introduction to Programming
NUMERICS Instructions LOOKUP Lookup data specified by offset and store in variable. Used to create lookup table. LOOKDOWN Find target’s match number(0-N) and store in a variable. RANDOM Generate a pseudo-random number. Introduction to Programming
TIME Instructions PAUSE Pause execution for 0 - 65535 milliseconds. POLLWAIT Pause until a polled interrupt occurs. Introduction to Programming
DEBUG Instruction DEBUG Send information to the host PC for viewing. Introduction to Programming
POWER CONTROL Instructions NAP Nap for a short period. Power consumption is reduced. SLEEP Sleep for 1 - 65535 seconds. Power consumption is reduced. END Sleep until the power cycles or the host PC connects. Power consumption is reduced. Introduction to Programming
DIGITAL I/O Instructions INPUT Make pin an input. OUTPUT Make pin an output. REVERSE Reverse direction of a pin. LOW Output a low logic level on pin. HIGH Output a high logic level on pin. TOGGLE Make pin an output and toggle state. PULSIN Measure an input pulse. PULSOUT Output a timed pulse by inverting a pin for some defined time. Introduction to Programming
DIGITAL I/O Instructions (cont’d) BUTTON Debounce button, perform auto-repeat, and branch to address if button is in target state. COUNT Count cycles on a pin for a given amount of time. XOUT Generate X-10 power line control codes. POLLIN Specify pin and state for polled-interrupt. POLLOUT Specify pin and state for output upon a polled- interrupt. POLLMODE Specifies the polled-interrupt mode. Introduction to Programming
ASYNCHRONOUS SERIAL I/O Instructions SERIN Input data in an asynchronous serial stream. SEROUT Output data in an asynchronous serial stream. OWIN Input data from a “1-wire” device. OWOUT Output data to a “1-wire” device. Introduction to Programming
SYNCHRONOUS SERIAL I/O Instructions SHIFTIN Shift data in from synchronous serial device. SHIFTOUT Shift data out to synchronous serial device. I2CIN Input data from I2C serial device. I2COUT Output data to I2C serial device. Introduction to Programming
PARALLEL I/O Instructions LCDCMD Write a command to a parallel LCD. LCDIN Read data from an LCD. LCDOUT Write data to an LCD. Introduction to Programming
ANALOG I/O Instructions PWM Output pulse-width-modulated (PWM) signal then return pin to input state. Can be used to generate an analog voltage (0 - 5V). RCTIME Measure an RC charge-discharge time. Can be used to measure a resistance. Introduction to Programming
SOUND Instructions FREQOUT Generate one or two sine waves of specified frequencies. DTMFOUT Generate DTMF (Touch-tone) telephone tones. Introduction to Programming
PBASIC Programming Style Do it right the first time The “I’ll fix it later” part usually never gets fixed. Sloppy code will be difficult to understand and debug later. Use meaningful names Meaningful names will reduce the need for comments Name variables, constants and program labels (up to 32 chars) Name I/O pins example: MotorContrl PIN 7 Introduction to Programming
PBASIC Programming Style (cont’d) Naming Constants Begin constant names with uppercase and use mixed case, using uppercase letters at the beginning of new words within the name. Example: AlarmCode CON 13 Naming Variables Begin variable names with lowercase and use mixed case, using uppercase letters at the beginning of new words within the name. Example: waterLevel VAR Word Introduction to Programming
PBASIC Programming Style (cont’d) Naming Constants Begin constant names with uppercase and use mixed case, using uppercase letters at the beginning of new words within the name. Example: AlarmCode CON 13 Naming Variables Begin variable names with lowercase and use mixed case, using uppercase letters at the beginning of new words within the name. Example: waterLevel VAR Word Introduction to Programming
PBASIC Programming Style (cont’d) PBASIC Keywords use UPPERCASE Indent nested code two spaces per indent is recommended Enclose condition statements in parenthesis Example: ...IF (waterTemp >= setPoint) THEN… Use white space to improve legibility two blank lines before subroutines for example Introduction to Programming
BASIC Stamp Applications Learning fundamentals of programming Data acquisition Control Robotics Embedded micro-controllers Introduction to Programming
Activity Students will inventory the BalloonSat kit of components and review the BalloonSat documentation. Students will begin construction of BalloonSat and complete assembly to the Stage 1 level. Students will inspect BalloonSat and correct any defects in workmanship. Introduction to Programming