Title: Microcontrollers
1Microcontrollers
- InstructorShuvra Das
- mechanical engineering department
- University of Detroit Mercy
2Flowchart of Mechatronic Systems
3Microprocessor 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 - Busesdigital 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).
4CPU
- 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.
5Registers
- 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.
6Registers
- 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 101110 (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.
7Memory
- 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
2101024.
8RAM
- 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.
9Memory
- 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.
10Memory
- 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.
11I/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.
12Bus
- 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
bi-directional data bus. - The parallel and serial ports on a microcomputer
are other examples of buses.
13Bus
- 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.
14Bus
- 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.
15Bus
- 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 28256
16Bus
- 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.
17Microcontrollers
18Basic StampII
19Basic Stamp II processorA. 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.
20Basic Stamp II processorA. 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 (HIGH5V,
LOW0V). - 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.
21A. 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.
22Memory 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.
23Reset 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.
24Power 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)
25B. 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).
26B. 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.)
27B. 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.
28Serial 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.
29Commonly used commands in BS2 programming
30Debug
- 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.
31End
- End the program, placing the BASIC Stamp into low
power mode indefinitely.
32GOSUB
- Syntax GOSUB ltSubroutineNamegt
- 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
-
33RETURN
- Syntax RETURN
- Return the execution from a subroutine to the
statement following the call of the subroutine of
this RETURN.
34PULSOUT
- Syntax PULSOUT ltPingt, ltPeriodgt
- 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
35PAUSE
- Syntax PAUSE ltPeriodgt
- Pause the program, execution stops for the
specified period.
36Number 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 (bitsbinary digits)
37Binary Math
- 000
- 01101
- 1110, I.e.,0carry 1
- 111 11, I.e. 1carry 1
- 141933
- 0111010111100001
- 0-00
- 1-01
- 1-10
- 0-110-1borrow 1borrow
- 27-1413
- 11011-0111001101
38C. 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.
39C. BS2 Runtime Math and Logic - pay attention!!
- Example 1232/4 doesn't result in 13.5, which
is what we get if we follow the rules we were
taught. Rather, BS2 does 1235, then 15230,
then 30/47. (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.
40C. 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.)
41C. 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.
42C. 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).
43C. 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 16-bit,
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.
44FOR NEXT
- Syntax FOR counter ltstartvaluegt TO ltEndValuegt
STEP ltStepValuegt 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.
45FORNEXT
- Note For Loops can not overlapped, Example
- FOR I 1 TO 10
- FOR J 1 TO I
- ..
- NEXT
-
- NEXT
-
46IFTHEN
- Syntax IF ltconditiongt THEN lttrue-addressgt
- If the condition is true the execution jumps to
the true-address, otherwise it continues down.
47High
- Syntax High Pin
- Makes the specific pin output high.
48LOW
- Syntax LOW ltPingt
- Make the specific pin output low
49Frequently used PBASIC Instructions
- Debug
- End
- For/Next
- Gosub
- Goto
- High
- IfThen
- Input
- Low
- Output
- Pause
- PWM
- Rctime
- Return
- Serin
- Serout
- Stop
- Toggle
50BRANCH
- 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 gt branch to address add0
- If offset 1 gt branch to address add1
- etc
51DATA
- Syntax ltsymbolgt DATA ltDataItem, DataItem,gt
- 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.
52GOTO
- 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
53INPUT
- Syntax INPUT ltPingt
- Make the specific pin an input.
54LOOKUP
- Syntax index, (value0, value1, valueN),
variable - Find the value at location index and store it in
variable - If index 0 gt variable value0
- Else if index 1 gt variable value1
- etc
55OUTPUT
- Syntax OUTPUT ltPingt
- Make the specified pin an output.
56PWM
- Syntax PWM ltPingt, ltDutygt, ltCyclesgt
- 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 - Example
- T(chg) 4 1000 10-6 40 ms
- So PWM 0, 100, 40
- Will Put a 1.96 V charge on the capacitor
1000 ohm
P0
Vo
0.1 uF
57RCTIME
- Syntax RCTIME ltPingt, ltStategt, ltVariablegt
- 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.
58READ
- Syntax READ ltLocationgt, ltVariablegt
- Read value at Location and store the result in
Variable. - Usually used on lookup tables.
59SERIN
- Receive asynchronous serial data (e.g, RS-232
serial protocol data). - We are not going to use serial communication
interface on our project(s). - For details refer to the user manual.
60SEROUT
- Transmit asynchronous serial data (e.g RS-232
data). - Again we are not dealing with serial
communication interface.
61STOP
- 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. -
62TOGGLE
- Syntax Toggle ltPingt
- Invert the state of an output pin, if it is high
make it low and vice versa.
63Sample Programs