KEMBAR78
Microcontroller | PDF | Microcontroller | Input/Output
0% found this document useful (0 votes)
83 views45 pages

Microcontroller

The document provides an overview of microcontrollers and their applications in robotics, emphasizing the importance of programming and interfacing with hardware components. It details the BASIC Stamp microprocessor, its programming language PBASIC, and various exercises to practice programming skills with LEDs and buttons. Additionally, it explains the structure of programs, including logic constructs and variable usage, to control outputs based on inputs.

Uploaded by

Daniel Mwembia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views45 pages

Microcontroller

The document provides an overview of microcontrollers and their applications in robotics, emphasizing the importance of programming and interfacing with hardware components. It details the BASIC Stamp microprocessor, its programming language PBASIC, and various exercises to practice programming skills with LEDs and buttons. Additionally, it explains the structure of programs, including logic constructs and variable usage, to control outputs based on inputs.

Uploaded by

Daniel Mwembia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Microcontrollers

Engr 1020
Intro to Engineering
Robot Project Overview
● A robot is a microprocessor controlled
machine
● It responds to inputs and produces
movement
● We must learn about microprocessors in
order to control the movement of our robot.
Parts of a computer

Input Processor Output


a. Microprocessor
b. MEMORY

storage
Hardware and Software
● Hardware refers to the physical
devices that make up the computer
system.
– Board of Education contains the
STAMP, memory, input/output pins
● Software refers to the instructions
that tell the computer what to do
How Humans are like a Computer
● What is the average of 3, 7, 4, 6?
● What is the standard deviation?
● How do you walk through the classroom
doorway when class is over?
Microcontrollers
● A microcontroller is a computer that
responds to inputs and produces output but
does not require human interaction.
● Microcontrollers are in microwaves, VCRs,
cars, traffic signals, etc.

Inputs Processor Outputs


(Sensors, a. Microprocessor (motors, LEDs,
switches, etc) speaker, etc.)
b. MEMORY
The BASIC Stamp Microprocessor
● About the size of a stamp
● On the “Board of
Education”
● Has 16 input/output pins
● Can control LEDs,
Speakers, Motors,
Solenoids, etc.
The “Board of Education”
● The BOE makes the STAMP convenient to
power, program, and interface.
STAMP Chip

Power Supply
connection 5 V (Vdd)
Ground (Vss)
9 V battery
I/O pins

Serial port
(for programming) Breadboard for
interfacing
How do we use the Stamp
● We tell the Stamp what to do
(PROGRAMMING)
● We connect (interface) the Stamp to input
devices and output devices
● We will first learn about programming (we
will learn about interfacing after we learn
about electronics).
A Stamp Application
How do we tell the Stamp what to do?
● Stamp must be programmed using a
programming language (similar to BASIC)
● We type the instructions into a personal
computer then download the program into
the stamp
● The stamp then runs the program
Programming
● PBASIC is similar to other programming
languages
● Your instructions must be PERFECT
– correct syntax
– correct logic
● Suggestion: Write your program on paper
first and desk check it.
Logic Constructs
∙ Sequence
conditi
∙ Selection (If/Then) on
∙ Looping
(For/Next)

cond
ition
PBASIC Language Statements

● INPUT ● FOR / NEXT


● OUTPUT ● DO / LOOP
● PAUSE ● GOTO
● LOW ● IF / THEN
● HIGH
Statement Examples
● Input - Makes the specified pin an Input
pin

INPUT 7

● Makes Pin7 an input pin


More Examples

● Output - Makes the specified pin an Output


pin.

OUTPUT 4

● Makes Pin4 an output pin


More Statements
● PAUSE - Pause program execution for the
specified number of milliseconds
● LOW - Makes the specified pin output low
(0 volts).
● HIGH - Makes the specified pin output
high (5 volts).
● GOTO - Used to create loops or branches
Example 1 - Simple Program
' {$STAMP BS2}
' {$PBASIC 2.5}
' This program blinks an LED
OUTPUT 2 ' Make pin 2 an Output pin
Tokyo:
LOW 2 ' pin 2 low (LED off)
PAUSE 1000 ' wait 1 second
HIGH 2 ' pin 2 high (LED on)
PAUSE 1000 ' wait 1 second
GOTO Tokyo ' loop forever
Downloading Program to the Stamp
● Use a 9V or 6V battery to power the stamp or
use a power supply. BATTERY MUST BE FRESH!
● Connect the stamp to your PC’s serial port
using the cable provided.
● Using the stamp editor, type in your program or
retrieve an existing program from disk.
● Type CTRL-R or press the blue “Run” arrow to
upload and run program.
Light Emitting Diodes (LEDs)
● A diode allows current to pass in one direction only.
● LEDs require a resistor to limit the current being passed.
● Some LEDs have the resistor built-in, but most LEDs
need a resistor in series . A 330ohm resistor should work.
● The LED must be wired with proper polarity. Current
will flow from higher voltage to lower voltage.
● Wire the LED from output pin to ground (Vss)
Interfacing Using I/O Pins and Breadboard
● Each set of 5 holes on
breadboard are connected
together.
● I/O pin holes are not
connected to breadboard
● Vss = Ground ( 0 Volts )
● VDD = 5 Volts
Wiring LED
● Vss is Ground.
● Flat side of LED points
toward Vss.
● Use 330 ohm resistor to
limit LED current.
● LED will light when I/O pin
is high (5 volts).
Practice Exercise 1 (Do in Teams)
● Write a program that will turn on an LED
(attached to Pin7) for 2 seconds, turn it off
for 1 second, turn on for 2, off for 1, etc.
Practice Exercise 2 (traffic light)
● Write a program that controls a traffic light
at an intersection. The cycle should repeat
forever. Times are below.
– Green 4 seconds
– Yellow 1 second
– Red 2 seconds
Variables
● Locations in memory that you can assign
values to.
● Types
– Bit – can store a “0” or a “1”
– Nibble (Nib) – 4 bits – can store 0 to 15
– Byte – 8 bits – can store 0 to 255
– Word – 16 bits – can store 0 to 65535
Declaring Variables
● Some names are predefined (or reserved),
so usually you must declare your own.
– Examples
Freaks VAR Word
M VAR Byte
Rashomon VAR Nib
THX_1138 VAR Bit
● Declare variables at the beginning of your
program.
Storing a value in memory
● First declare the variable (near top of program)
Pulse_Count VAR Word

● Then assign the value to the variable


Pulse_Count = 12

● Assigns 12 to word variable Pulse_Count


Using Variables
● In Traffic Program we could use Variables

GreenTime VAR Word


GreenTime = 6000


PAUSE GreenTime

Example 2 (For/Next Loops)
' -------------------------------------
' Program flashes LED on Pin2 10 times.
' -------------------------------------
X VAR Word ‘could use byte var here
FOR X = 1 TO 10
LOW 2
PAUSE 1000
HIGH 2
PAUSE 1000
NEXT
Example 2 Variations
' -------------------------------------
' Program flashes LED on Pin2 10 times.
' -------------------------------------
X VAR Word ‘could use byte var instead
FOR X = 1 TO 10
LOW 2
PAUSE 1000 * X
HIGH 2
PAUSE 1000 * X
NEXT
Practice Exercise 3 (Do in Teams)
Write a program that turns on an LED
attached to Pin3 for 5 seconds then turns it
off. The light should stay off for 3 seconds
and then the LED should blink 10 times at a
rate of 1 Hz (on and off each second).
Practice Exercise 3.5 (Do in
Teams)
Write a program that operates a train crossing
light. The lights should flash at a rate of 2
Hz and operate for 20 seconds.
Using Buttons and Switches
● Buttons and switches provide input to the
Stamp
● Wiring a Button (see later slide)
– Active High
– Active Low
● Programming for a Button (next 2 slides).
– BUTTON Statement
– IF / THEN (recommended approach)
Button Statement
● Used to read a button and debounce it. Also has
autorepeat feature.
Button pin, downstate, delay, rate, bytevariable, targetstate, address

● Example: BUTTON 4,0,255,0,b2,0,Ringo


downstate Which logical state is read when button is pressed. Use 0 for active low, 1 for active
high
delay and rate Used for autorepeat feature
bytevariable Workspace for Button. Clear to 0 before using button statement.
targetstate Which logical state button should be in for branch to occur. 1 to branch when button
is pressed. 0 to branch if button not pressed.
address Label that specifies where to branch if button is in target state. If button is not in the
target state, the statement following the button statement is executed next.
If / Then Statement
● IF condition THEN label

● Example:
IF IN4 = 1 THEN George

If input pin 4 is high then goto George.


Wiring a button
Active Low Active High
Wiring Pushbutton Switch
ACTIVE LOW
Example 3 (Using a Pushbutton)
' ---------------------------------------------------
' Stamp button program. It turns an LED on pin0 on and
' off on alternate button presses.
' Interface pushbutton switch to pin4 and wire so it is
' "active low"
' ---------------------------------------------------

● Two ways
– Using Button Statement
– Using If / Then Statement
Using Button Statement
x VAR Byte ' variable for button command
OUTPUT 0 ' make pin0 an output pin
INPUT 4 ' make pin4 an input pin

Blue:
BUTTON 4,0,255,0,x,0,Blue ' wait for button press
HIGH 0 ' turn LED on
PAUSE 250 ' delay 1/4 second
Red:
BUTTON 4,0,255,0,x,0,Red ' wait for button press
LOW 0 ' turn LED off
PAUSE 250
GOTO Blue ' and repeat
Using If/Then Statement
INPUT 4 ' make pin4 an input pin
OUTPUT 7 ' make pin7 an output pin
Ferrari:
IF IN4 = 1 THEN Ferrari ' wait for button press
HIGH 7 ' turn LED on
PAUSE 250 ' delay 1/4 second

Geo_Metro:
IF IN4 = 1 THEN Geo_Metro ' wait for button press
LOW 7 ' turn LED off
PAUSE 250

GOTO Ferrari ' and repeat


Practice Exercise 4
Write a program that turns on an LED for 3
seconds every time a button is pressed.
Assume the button is wired active low.
Practice Exercise 5
Write a program that flashes an LED 10 times
at a rate of 2 Hz every time a button is
pressed. Assume the button is wired active
low.
Practice Exercise 6
Write a program that starts by having a red
LED attached to pin8 go on. The LED will
stay on until a button is pressed. A half
second after the button is pressed, the red
LED will go off and a green LED will go on
for 5 seconds. After this the whole cycle
repeats.
Practice Exercise 7
Write a program that starts out by flashing an
LED connected to pin7 at 2Hz. On a button
press (button is attached to pin4- assume
active low), the LED turns on for 2 seconds.
After this the whole cycle repeats.
Practice Exercise 8
Write a program that lights an LED attached
to pin 3. The LED should turn off after a
button attached to pin4 has been pushed 3
times. Assume the button is wired active
low. Assume there is least 1/4 second
between button presses.

You might also like