Microcomputers - University of Detroit Mercy
Download
Report
Transcript Microcomputers - University of Detroit Mercy
Microcontrollers
Instructor:Shuvra Das
mechanical engineering department
University of Detroit Mercy
Flowchart of Mechatronic
Systems
Microprocessor Structure
• CPU (Central Processing Units): to recognize and
carry out program instructions
• Memory: storage of data
• I/O Devices: to handle communications between
the computer and outside world
• Buses:digital signals move from one part of the
computer to another along buses. These could
be track on a printed circuit board or wires in a
ribbon cable (e.g.data bus, control bus, address
bus).
CPU
• Consists of control unit, arithmetic/logic unit
(ALU), and various registers.
• The control unit manages the flow and
manipulation of data. Determines timing and
sequence of operations.
• A clock circuit provides synchronization;
• The ALU performs all arithmetic and logical
computations on the data that have been
transferred to appropriate registers.
Registers
• Accumulator: Temporary data storage. To read
data the CPU needs to address the specific
memory word.
• E.g. when operating with two numbers only one is
fetched at one time and stored in the accumulator.
When the ALU operation is done the result is sent
back to the accumulator.
• Accumulator: Is involved in all data transfers.
Registers
• Status register: contains the status information about the
latest operations. Usually a bit is associated with it and it
is called a flag.
• E.g. binary addition 101+110 = (1) 011 (leads to overflow
and carries a 1 to the overflow)… this will raise a flag.
• Program counter register, Memory address register,
Instruction Register, General-purpose register, Stack
pointer register.
• The total number and types of registers
depends on the microprocessor.
Memory
• Used for storing program, binary data,
intermediate results from computations.
• Memory units consist of cells that can store values
0 or 1. Storage cells are grouped together to store
one word.
• With a 4-bit address we can access 16 different
memory units (with each perhaps holding 8 bits)
• Size of a memory unit is specified in terms of the
number of storage locations available; 1K is
210=1024.
RAM
• Random access memory (RAM) is an example of
volatile memory; information stored in volatile
memory is lost when power is disconnected.
• In most cases, user programs can read from and
write to RAM.
• Temporary data is stored in RAM.
Memory
• ROM (read only memory) cannot be written to by a user
program. ROM is often used to store look-up tables, and
program code that will no longer be changed (such as
operating systems)
• PROM, EPROM, EAROM, EEROM are variations on
this, which allow ROM to be programmed, and
reprogrammed.
• Most ROM's are programmed in such a way that the data
they store are not lost when power is disconnected, so
ROM is an example of non-volatile memory.
Memory
• EPROM: Erasable, Programmable ROM. The
information is permanently stored by applying a
voltage to the Integrated circuit. Ultraviolet light
shined on the quartz window erases this memory.
• EEPROM: Electrically erasable PROM, erasing happens
through the application of an electric voltage.
I/O Devices
• Without input and output devices, the
computational power of a digital computer
has no meaningful contribution to
automation and control.
• Disk drives, monitors, and printers are
examples of output devices. Keyboards,
disk drives, and scanners are examples of
input devices.
Bus
• These are the paths that data follow throughout the
computer system.
• Data need to be retrieved from memory into
registers in the CPU, and the results of
computation need to be transferred back to
memory.
• These data transfers take place along the bidirectional data bus.
• The parallel and serial ports on a microcomputer
are other examples of buses.
Bus
• The address bus carries the address of the memory
location of data or a program instruction that has been
requested by the control unit.
• When a particular address is selected in the address bus
only that location is open to the CPU. The CPU can
only communicate with one location at one time.
Bus
• Many devices can use a single bus; hence it is
important that the control unit keep track of which
device is requesting use of the bus, and whether it
is to receive or send data.
Bus
• Data bus is used to transport a word from CPU and
memory or I/O. Word lengths used may be 4, 8, 16, 32, or
64.
• An 8-bit data bus may consist of 8 separate copper tracks
laid out on a printed circuit board, or it may connect to
other devices through ribbon cables
• Each wire carries a 0 or 1 signal
• 8 bit microprocessors are very commonly used as
microcontrollers.
• For 8 bit processor the maximum number of values that
can be transported is 28=256
Bus
• Control bus is the means by which signals are sent to
synchronize the separate elements. The system clock
signals are carried by the control bus. These signals
generate time intervals during which system operations can
take place. The CPU can send control signals to other
elements to indicate the type of operation being performed:
• e.g. whether it needs to READ (receive) and WRITE
(send) a signal.
Vdd
Data Bus
clock
CPU
Microcontrollers
memory
ROM
RAM
EEPROM
Control
lines
Vss
I/OControl
&
Status
Registers
I/O
data
registers
Input/Output
ports
Basic StampII
Basic Stamp II processor:
A. BS2 Hardware
• The brain of the BS2 is a custom PIC16C57
microcontroller, which has been permanently
programmed with the PBASIC2 instructions set.
• When you run a program on BS2 it retrieves the
information from a separate memory chip and
interprets them and carries out the instruction.
• PIC executes 5 million ins/sec. But PBASIC2 does
3k-4k ins./sec.
Basic Stamp II processor:
A. BS2 Hardware
• 20 I/O pins, 16 are for general use, 2 can be used
for serial communication, 2 are dedicated to
interfacing with the memory chip.
• P0-P15 interface with 5-volt logic (HIGH=5V,
LOW=0V).
• In input mode, the state (1 or 0) of the pin as
determined by external circuitry can be read. In
output mode, the pin is internally connected to
either ground or 5V, depending on the
programmer's preference.
A. BS2 Hardware
• BS2 has 32 bytes of RAM (random access
memory).
• Six bytes are reserved for input, output and
direction control of the I/O pins, which
leaves 26 bytes for storing variables.
Memory chip
• EEPROM- Used for program storage: non-volatile
memory. Not lost due to power loss.
• Can be written to and read from by program, but
need to keep in mind that there is a limit to the
number of times you can write to the EEPROM
(about 10 million).
• Also it takes a long time (as much as several
millisecond) to write data to memory.
Reset circuit
• When power is interrupted or corrupted, the reset
circuit shuts down the BS2 to prevent mistakes or
lockup, either of which may be dangerous if using
the BS2 to control heavy equipment.
• When the voltage supply stabilizes, the program
starts again at the beginning.
Power supply
• Voltage regulator accepts 5-15V and provides a
constant 5V. Also allows for low-power modes
(Sleep, End, Nap instructions).
• Power supply can provide up to 50mA, BS2 needs
8mA when active. This means that some external
circuitry can be driven without needing a separate
supply. (Vdd = +5V, Vss = ground)
B. Input/Output, Variables
• RAM has 6 bytes (1 byte = 8 bits), reserved for
managing the I/O pins.
• The remaining 26 bytes are available for assigning
variables. Fixed variables reside in a particular
memory location. You are advised to use variables
which are automatically allocated by PBASIC2.
• Variables can be declared as bit, nib (nibble = 4
bits), byte (8 bits), word (16 bits).
B. Input/Output, Variables
• Declaring as bit means the variable can take on
values of 0 or 1.
• If declared as nib, the value can be in the range
from 0 to 15. Byte variables can range from 0 to
255 and word variables from 0 to 65535.
• In general, use the smallest size that will
adequately represent the value you need (due to
26-byte limit on memory.) (Arrays can also be
declared, but are not discussed here.)
B. Input/Output, Variables
• Variable modifiers allow us to look at certain bits
of a variable. For example, RESULT.LOWBIT
refers to the least significant bit of the variable
RESULT. Other modifiers include, lowbyte,
highbyte, nib0, nib1, bit0, bit15, etc…
• You can declare constants in PBASIC2. For
example, if we wish the baud rate of the serial
output to always be 9600, we may wish to store
9600 as a constant named BAUD. We need only
change it in the declaration line.
Serial host interface
• This interface used for downloading program from
a host PC (BS2 has no keyboard - programming
happens on PC, then is downloaded to the
EEPROM, via serial cable from the PC's serial
port to the 9-pin connector on STAMP2 carrier
board.
Commonly used commands in
BS2 programming
Debug
• Allows you to display variable, constants, or
expression values while program is running in
order to follow program flow (debugging).
• Example:
X = 75
DEBUG X
Displays x = 75 on the screen.
End
• End the program, placing the BASIC Stamp
into low power mode indefinitely.
GOSUB
• Syntax : GOSUB <SubroutineName>
• Make the execution of the program jump to the point of the subroutine
specified by the subroutine name, after storing the address of the
instruction next to GOSUB. The execution comes back to the
instruction next to the GOSUB after finishing the subroutine by the
RETURN instruction.
• Example:
GOSUB MyRoutine
………
……….
Myroutine :
------------------RETURN
RETURN
• Syntax: RETURN
• Return the execution from a subroutine to
the statement following the call of the
subroutine of this RETURN.
PULSOUT
• Syntax: PULSOUT <Pin>, <Period>
• Generate a pulse on Pin with a width of Period.
• In BS2 PULSOUT works on units of 2 micro
seconds.
• Example:
PULSOUT 12, 750
Generates a 150 micro second pulse width on pin 12
PAUSE
• Syntax: PAUSE <Period>
• Pause the program, execution stops for the
specified period.
Number System
• Decimal System: 1,2,3,4,5,….
– 103,102,101,100
– thousands,hundreds,tens,units
• Binary system: 0,1
– 23,22,21,20
– bit3,bit2,bit1,bit0 (bits=binary digits)
Binary Math
•
•
•
•
•
0+0=0
0+1=1+0=1
1+1=10, I.e.,0+carry 1
1+1+1 = 11, I.e. 1+carry 1
14+19=33
– 01110+10111=100001
•
•
•
•
0-0=0
1-0=1
1-1=0
0-1=10-1+borrow =
1+borrow
• 27-14=13
– 11011-01110=01101
C. BS2 Runtime Math and
Logic - pay attention!!
Number Representations: BS2 recognizes decimal
(no prefix), hex ($ prefix), binary (% prefix),
ASCII (string enclosed in quote - example: "A"
returns the ASCII code for A: 65.)
Order of operations: BS2 performs operations in
the order written - big difference from letting
multiplication and division take priority over
addition and subtraction.
C. BS2 Runtime Math and
Logic - pay attention!!
Example: 12+3*2/4 doesn't result in 13.5, which
is what we get if we follow the rules we were
taught. Rather, BS2 does 12+3=5, then 15*2=30,
then 30/4=7. (Note: BS2 does integer math, so
30/4 is 7 not 7.5.) Parentheses are necessary to
force the multiplication and division to take place
first.
C. BS2 Runtime Math and
Logic - pay attention!!
Integer math: BS2 uses rules of positive integer
math. It handles only whole numbers and drops
fractional parts of any computations. Careful with
negative numbers - these are stored as 2's
complement. Better to avoid negative numbers
when possible. Division with negative numbers
will not work, but addition, subtraction and
multiplication are ok, if you are careful. (Also use
MIN and MAX only with unsigned integers.)
C. BS2 Runtime Math and
Logic - pay attention!!
Unary and binary operators: Unary operators take
precedence over binary operators: 10 - SQR 16
results in SQR 16 being evaluated first, then
subtracts it from 10.
16-bit workspace: Computation is done in 16 bits.
If variable is byte, it is padded with 8 leading
zeros to make the operand 16 bits. After the
operation, the 8 LSB bits are placed into the byte
variable in which we want to store the result.
Careful when working with negative numbers.
C. BS2 Runtime Math and
Logic - pay attention!!
• Example: x = -99, where x has been declared as a
byte. When we ask for the value stored in x to be
displayed in signed decimal format (PBASIC2
instruction is "debug sdec ? x"), it shows that x is
157. What happened? Note that 99 is 01100011
as a byte. When BS2 negates 99, it converts the
number to 16 bits (0000000001100011), then
takes two's complement (1111111110011101).
C. BS2 Runtime Math and
Logic - pay attention!!
• Since we asked for the value to be stored as x
(byte), the 8 leftmost bits are truncated, and we
have (10011101). The SDEC modifier of the
debug instruction expects the operand to be a 16bit, 2-'s complement number, but we are only
giving it a byte to work with. So it just pads the
number with leading zeros (0000000010011101),
which is +157.
FOR … NEXT
• Syntax: FOR counter = <startvalue> TO <EndValue>
STEP <StepValue> …… NEXT
• Create a repeating loop that executes the lines
between FOR and NEXT, incrementing or
decrementing the counter according to the step
value.
• If start value is larger than end value, PBASIC
understands that the step value is negative even if
there is no minus sign.
FOR…NEXT
• Note: For Loops can not overlapped, Example:
•
FOR I = 1 TO 10
FOR J = 1 TO I
…..
NEXT
………
NEXT
IF…THEN
• Syntax: IF <condition> THEN <true-address>
• If the condition is true the execution jumps to the
true-address, otherwise it continues down.
High
• Syntax: High Pin
• Makes the specific pin output high.
LOW
• Syntax: LOW <Pin>
• Make the specific pin output low
Frequently used PBASIC
Instructions
Debug
End
For/Next
Gosub
Goto
High
If…Then
Input
Low
•
•
Output
Pause
PWM
Rctime
Return
Serin
Serout
Stop
Toggle
BRANCH
•
•
•
•
Syntax: BRANCH offset, (Add0, Add1, …, AddN)
Go to the address specified by offset.
It works the same way like (switch-case) in C++.
We will use it on look up tables when we have more than one
table to see which table we are going to read.
• If offset = 0 => branch to address add0
• If offset = 1 => branch to address add1
• … etc
DATA
• Syntax: <symbol> DATA <DataItem, DataItem,…>
• Write data to the EEPROM during the program download.
• Symbol: is an optional, unique symbol that will be
automatically defined as a constant equal to the location
number(address) of the first Item.
• DataItem: is a constant indicates a value.
GOTO
Syntax: GOTO label
• Makes the execution jump to the point of the
program specified by the label.
• A common use : on endless loops.
• Example:
loop: …….
……
GOTO loop
INPUT
• Syntax: INPUT <Pin>
• Make the specific pin an input.
LOOKUP
• Syntax: index, (value0, value1, … valueN), variable
• Find the value at location index and store it in variable:
If
index = 0 => variable = value0
Else if index = 1 => variable = value1
… etc
OUTPUT
• Syntax: OUTPUT <Pin>
• Make the specified pin an output.
PWM
• Syntax: PWM <Pin>, <Duty>, <Cycles>
• Convert a digital value to analog output via pulse-width
modulation.
• Units in cycles on BS2 is 1 ms.
• Average voltage equation: V(avg) = (Duty/255) * 5 Volts.
• Required charge time(cycles) equation: t(chg) = 4 * R * C
1000 ohm
• Example:
P0
Vo
• T(chg) = 4 * 1000 * 10^-6 = 40 ms
• So: PWM 0, 100, 40
Will Put a 1.96 V charge on the capacitor
0.1 uF
RCTIME
• Syntax: RCTIME <Pin>, <State>, <Variable>
• Measure time while Pin remains in State and put the result
on Variable; usually to measure the charge/ discharge time
of resistor/ capacitor (RC) circuit.
• We will use that on our following light program, by
comparing the RCTIME of the left and the right photo
sensors (resistor on our case) and use the comparison result
to decide whether we we should turn right or left.
READ
• Syntax: READ <Location>, <Variable>
• Read value at Location and store the result
in Variable.
• Usually used on lookup tables.
SERIN
• Receive asynchronous serial data (e.g, RS232 serial protocol data).
• We are not going to use serial
communication interface on our project(s).
• For details: refer to the user manual.
SEROUT
• Transmit asynchronous serial data (e.g RS232 data).
• Again we are not dealing with serial
communication interface.
STOP
• Syntax: STOP
• Stop program execution without putting BASIC stamp into
low-power mode, also you can use it in the middle of the
program (differences from END).
• Use this command on error handling (ex: divide by zero),
or if your program branches based on the inputs into more
than one independent algorithm, so each algorithm
considered as separated program has to end when finished.
TOGGLE
• Syntax: Toggle <Pin>
• Invert the state of an output pin, if it is high
make it low and vice versa.
Sample Programs