0% found this document useful (0 votes)
2 views

EENG 371 Lecture Notes

Uploaded by

zghanem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EENG 371 Lecture Notes

Uploaded by

zghanem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 114

EENG 371

Microcontrollers
Prof. Jeff Ting
What is a Computer?
• An electronic device that manipulates information or data. It can
store, retrieve and process data.

• What’s the difference between Hardware and Software?


• Hardware is the physical machine (CPU, motherboard, screen, etc).
• Software is the programs - sets of instructions for the computer to follow.

• The CPU (hardware) “processes” these software’s instructions


• The power of a computer comes from the speed at which these
instructions are processed/executed.
Computer Architecture
• What is Architecture?

• Designing a building well-suited for its purpose.


• Office building? Apartment complex? Single family home?

• Computer Architecture: designing a computer well-suited


for its purpose:
• Desktop, Laptop, Mobile device.
Microprocessors / Microcontrollers
• In the past 50 years, microprocessors have revolutionized the world
• A computer used to occupy an entire room, now it fits in your hand
• Electronic Computing began in the 1940’s, now it comprises over 10%
of the nations GDP
• Where are these computers?
• PCs, cell phones, tablets
• Servers, Cloud Computing, SaaS
• Cars (regulate fuel efficiency, pollution, GPS, backup camera, windshield
wipers, seat adjusters, air bag deployment, blind spot detector, etc)
• Biomedical: DNA genome sequencing
• Embedded Systems: Coffee Maker, Washing Machine, TV
• World Wide Web, Search Engines
• Self Driving Cars
Inputs and Outputs, I/O
• Five Classic Components of a Computer
• Input
• Output
• Memory
• Datapath
• Control
• Inputs and Outputs, I/O?
• Example: Cell Phone
• Input: Microphone
• Output: Speaker
• What else? The screen? Input or Output?
What is Binary?
• Binary is a number system that only uses 1’s and 0’s:
• 0010 1101 = 0x2D = 45 decimal
• 1 = Logic high
• 0 = Logic low

• Why do Computers use Binary?


Why do Computers use Binary?
• Because the building blocks of digital systems are tiny
electrical devices called Transistors.

• What’s a Transistor?
• A semiconductor device with 3 terminals (Gate, Drain, Source)
used to amplify (analog) or switch (digital) electrical signals. In this
class, think “switch.”
• There are many types of transistors. CMOS technology is used in
most digital systems. CMOS uses 2 transistor types, each is doped
(chemically altered) differently:
• nMOS
• pMOS
nMOS & pMOS Transistors
Transistors
• The Drain and Source of Transistors must be connected to Power (e.g. Vdd and Gnd).
• G = Gate controlled by a Voltage, either HIGH (1) or LOW (0).
• Transistor is “ON” meaning the switch closed, current flows.
• Transistor is “OFF” meaning the switch open, no current flows.
• Consider nMOS and pMOS opposites.
So both the input of a transistor
(G=0 or G=1) and the output
Vdd (switch closed or switch open) is
Vdd Vdd BINARY.

Gnd Gnd Gnd


Vdd Vdd Vdd

Gnd Gnd Gnd


NAND Gate
Truth Table Scenarios?
Multiplexer
Micro-Architecture
Transistor Layout
Transistor Layout
Quad Core Layout
iPhone 13
Managing Complexity
• Modern Digital Systems are built from Billions of transistors.
• Layers of Abstraction: hiding unimportant details (like layers
of an onion)
• Physics: Electrons
• Devices: Transistors
• Analog / Digital Circuits: Amps, Filters / Logic Gates
• Logic: Adders, Multiplexers, Memory
• Micro-Architecture: Datapaths, Controllers
• Architecture: Instructions (Assembly Code)
• Operating System: Device Drivers (High-Level Language)
• Application Software: Programs, Apps
Decimal vs Binary
• Decimal: Base 10
• N-bit binary number represents a number up to 10N – 1. [start counting from Zero]

• Binary: Base 2
• Each digit called a “bit.” 8 bits = 1 byte
• N-bit binary number represents a number up to 2N – 1. [start counting
from Zero]

• Convert Decimal to Binary using the “places” (16, 8, 4, 2, 1)


Hexadecimal or Hex
• Base 16
• Long Binary Numbers are tedious, error-prone.
• Hex uses 0-9, A – F
• Sometimes written as “0xFFA3”
How to convert between Binary, Decimal & Hex?
General Purpose Microprocessor v Microcontroller

• General-Purpose • Microcontroller:
Microprocessor: • CPU (microprocessor)
• No RAM • RAM
• No Flash/ROM • Flash/ROM
• No I/O ports • I/O Ports (customizable)
• Timer
• ADC and other peripherals

• What’s the difference between RAM and


Flash/ROM?
General Purpose Microprocessor v Microcontroller
General Purpose Microprocessor v Microcontroller
Sometimes hard to tell the difference.
General Purpose Microprocessor v Microcontroller

• General-purpose • Microcontroller:
Microprocessor: • The fixed amount of on-chip
• Systems tend to be Bulkier Flash/ROM, RAM, and I/O
and more Expensive (Must ports makes them ideal for
add RAM, Flash/ROM, I/O applications in which cost and
ports, Timers). space are critical.
• It’s versatile: you can add • In MANY applications, space,
more RAM, Flash/ROM, and power consumption, and the
I/O ports if needed. price per unit are more
important than the
computing power.
• Cheaper!
Embedded Systems (aka Microcontroller)
• An embedded product/system uses a microcontroller to do
one task only.
• Example: a Coffee Maker ONLY makes coffee!
• In contrast, A PC (microprocessor) can be used for many
applications.
• The OS (Operating System) can load a variety of applications into
memory
• PC connected to various peripherals (video card, sound card,
wireless LAN, etc).
• Each peripheral is essentially an embedded system that performs only one
task.
• Would you use an Intel Core i7 to run a Coffee Maker?
General Purpose Microprocessor v Microcontroller
FEATURE Texas Instr MSP430 Intel Core i9X (18 cores)
Clock Speed Up to 25 MHz 4.4 GHz

Address Bus 16 bits 64 bits


Data Bus 16 bits 64 bits
ALU Width 16 bits 64 bits
Applications Appliances, Autos, etc Personal Computer
Power Consumption Fraction of a Watt 140 W
Cost of Chip ~$2 $1800

• What does the “Clock” do?

• Why is more bits better?


Field Programmable Gate Array (FPGA) vs Microcontroller
• FPGA: • Microcontroller:
• Harder to program but more • Easier to program (can use C)
flexible. • Fixed instruction set
• Must use a Hardware • Cheaper
Description Language to
program (VHDL, Verilog)
• Higher up-front cost
• Generally Faster
• Higher power consumption
• Better for parallel processes

• Both can be reprogrammed.


Choosing a Microcontroller
• Choose a micro that meets the needs of the task efficiently
and cost effectively.
• Speed
• Package: Does it fit?
• Power consumption
• The amount of RAM and Flash/ROM on chip
• The number of I/O pins: How many inputs/outputs do I need for
my Coffee Maker?
• The On-Chip Timer
• How easy to upgrade to higher performance or lower power-
consumption versions
• Cost per unit
Choosing a Microcontroller
• Package

Dual In-Line Package Plastic Leaded Chip Carrier Quad Flat Pack
MSP430 Microcontroller (Texas Instruments)
• MSP430 has hundreds of variations, in this class we will be using the
MSP430F5529LP.
• MSP430F5529LP highlights:
• USB 2.0-enabled 16-bit MCU
• Up to 25 MHz
• 128KB Flash and 8KB RAM
• 12 Bit SAR ADC
• Code Composer Studio IDE (Integrated Development Environment) for edit/debug
• Can write code in C, C++, or Assembly
• Various USB device class examples and embedded software libraries available (CDC,
HID, MSC)
• eZ-FET lite: Open source onboard debugger with application UART
• One USB connection for emulator and target via the use of an onboard USB hub
• USB as power source: 5V and 3.3V through a high efficiency DC/DC converter
• 40 pin LaunchPad standard leveraging the BoosterPack ecosystem
MSP430
• 16 bit RISC (Reduced Instruction Set Computer) CPU
Prof Ting’s Project at Texas Instruments
ARC FAULT CIRCUIT BREAKER
• How does a traditional circuit breaker work?
• Too Slow! Arcs can occur in milliseconds.
• Our goal was to use a microcontroller to “sense” arcs
on the line.
EENG 320 Control Systems
• Plot of Temperature?

• Imagine an automatic temperature control system


for your bedroom. Describe each box.
Micro-Architecture (again)
• The circuit below is made up of digital logic (transistors). So now
what? How do we get it to do what we want?
• We must feed it Instructions!
Assembly Language Instructions
• Assembly Language instructions include:
• a Mnemonic CPU Command
• Example: MOV.W
• One or two operands
• the data items
• Directives (pseudo-instructions)
• directions for the assembler
• In the 1940’s, programmers coded in machine language (0s and 1s)
• Tedious, slow, prone to error
• In the 1970’s, someone decided Assembly had also become too
tedious, so they created C Language. And then someone decided C
was too tedious so…
Address Machine Code Assembly Language Comment
01002C 40B1 002B 0004 MOV.W #0x002B,0x0004(SP) ;copy #0x002B into addr SP+0x0004
Hierarchy (Code Composer Studio)
• Compiler converts C code to
Assembly code (.asm)
• Assembler program converts
Assembly into Machine Code,
produces an object file (.obj)
• Linker program links object
code with Libraries (.lib) and
produces an output file (.out)
• Output file is burned into the
micro’s Flash/ROM (code mem)
MSP430 Memory Map
MSP430 Memory Map
• The micro has many different parts: Memory,
ALU, GPIO, Timers, ADC
• Memory is broken into segments DATA CODE
• Code Memory (Flash/ROM)
• Data Memory (RAM)
• Every piece of memory has 2 critical items:
Address and Data.
• Think of memory like a library where we are
concerned with both where the book is and
what the book is.
• Book = Data
MSP430 Memory Map The “h” suffix
means Hex
• This picture is not an accurate
representation of how memory is laid out
on the physical circuit.
• Different parts of memory have different
“access” times. Ex: you are searching for
the book in the library.
• (128 KB total address space)
Registers
• What is Register? A register is bank of Flip-Flops. What is a FF? A FF is
a Latch that can be used to store information. What’s a Latch? SR
Latch is 2 cross-coupled NOR gates.
• Registers are part of Data Memory (RAM), they store information
(bytes) temporarily. The information could be:
• Data
• an Address pointing to some data
• Define:
• Word = 16 bits
• Byte = 8 bits
Registers
• MSP430 has sixteen 16-bit Registers.
• Special Registers:
• R0 = Program Counter (PC)
• R1 = Stack Pointer (SP)
• R2 = Status Register (SR)
• R3-R15 = General Purpose Registers
Structure of Memory
Every memory address holds 1 byte (8 bits).
Address Data • Example: my phone has 64 GB of storage

0x0007 0xAA Since the MSP430 is a 16 bit processor, every


chunk of data is 16 bits (2 bytes). So for
0x0006 0x4C simplicity, we need not keep track of the “odd”
addresses.
0x0005 0x0A

0x0004 0x00 Address Data

0x0003 0xBA 0x0006 0x4CAA

0x0002 0x00 0x0004 0x000A

0x0001 0x1D 0x0002 0x00BA

0x0000 0xD9 0x0000 0xD91D


Addressing Modes
• How do we access/manipulate data in memory?
• Seven Addressing Modes:
• Register
• Indexed
• Symbolic (we will skip)
• Absolute
• Indirect
• Indirect Auto Increment
• Immediate
Register Mode
• Example: MOV instruction

MOV.W R3,R4 ;copy the data in R3 into R4


• R3 and R4 are called “Operands.”
• R3 is the “Source”
• R4 is the “Destination”
• The left operand is always the Source and the right operand is always the Destination.
• The suffix .W indicates means word (16 bits). The suffix .B means byte (8 bits).

• Example: Let’s say R3=0x0005 and R4=0x60AA.


Before Data Memory After
Address Data Address Data

R4 0x60AA R4 0x0005
R3 0x0005 R3 0x0005
R2 R2
R1 R1
R0 R0
Indexed Mode
• Example: MOV instruction

MOV.W R3,2(R4) ;copy data in R3 to destination address


• R3 is the “Source”
• Destination Address = M(R4 + 2) = M(0x60A8 + 2) = M(0x60AA)
• “M” stands for Memory Address
Before Data Memory After
MOV.W R3,4(R4) Address Data Address Data

R4 0x60A8 R4 0x60A8
R3 0x0005 R3 0x0005
R2 R2
R1 R1
R0 R0
Address Data Address Data

0x60AC 0x60AC
0x60AA 0xFFD3 0x60AA 0x0005
0x60A8 0x60A8
0x60A6 0x60A6
0x60A4 0x60A4
Absolute Mode
• Example: MOV instruction

MOV.W R4,&0x60AC ;copy data in R4 to destination address


• Destination Address = M(0x60AC)

Before Data Memory After


Address Data Address Data

R4 0x00AA R4 0x00AA
R3 R3
R2 R2
R1 R1
R0 R0
Address Data Address Data

0x60AC 0xFFD3 0x60AC 0x00AA


0x60AA 0x60AA
0x60A8 0x60A8
0x60A6 0x60A6
0x60A4 0x60A4
Indirect Mode / Pointer Mode
• Example: MOV instruction

MOV.W @R4,R3 ;copy data in source address to R3


• Source Address = M(R4)
• R4 is a Pointer to Data
Before Data Memory After
Address Data Address Data

R4 0x60AA R4 0x60AA
R3 0x0005 R3 0xFFD3
R2 R2
R1 R1
R0 R0
Address Data Address Data

0x60AC 0x60AC
0x60AA 0xFFD3 0x60AA 0xFFD3
0x60A8 0x60A8
0x60A6 0x60A6
0x60A4 0x60A4
Indirect Mode Auto Increment
• Example: MOV instruction

MOV.W @R4+,R3 ;copy data in source address to R3

• Source Address = M(R4)


• Also Increment the pointer by 2 (after the copy is completed)
Before Data Memory After
Address Data Address Data

R4 0x60AA R4 0x60AC
R3 0x0005 R3 0xFFD3
R2 R2
R1 R1
R0 R0
Address Data Address Data

0x60AC 0x60AC
0x60AA 0xFFD3 0x60AA 0xFFD3
0x60A8 0x60A8
0x60A6 0x60A6
0x60A4 0x60A4
Immediate Mode
• Example: MOV instruction

MOV.W #0x04A8,R3 ;copy #data to R3

• Source = 0x04A8
• The “#” prefix indicates Immediate Mode

Before Data Memory After


Address Data Address Data

R4 R4
R3 0x0005 R3 0x04A8
R2 R2
R1 R1
R0 R0
Various Combinations
• Examples:
MOV.W #0x04A8,&0x8015 ;copy #data to M(0x8015)

MOV.W @R3,&0x8015 ;copy M(R3) to M(0x8015)

• Source (1st Operand) can use all 7 addressing modes.


• Destination (2nd Operand) can only use 4 addressing modes:
• Cannot use Immediate
• Cannot use Indirect or Indirect auto-increment

MOV.W R3,@R4 ;illegal!


Instruction Set
• Example: ADD instruction (Register Mode)
ADD.W R3,R4 ;R4 = R3 + R4

• R4 is the “Destination”

• Example: Let’s say R3=0xF0BC and R4=0x604A.


Before Data Memory After
Address Data Address Data

R4 0x604A R4 0x5106
R3 0xF0BC R3 0xF0BC
R2 R2
R1 R1
R0 R0
Instruction Set
• Example: ADD instruction

ADD.W @R4,R3 ;R3 = M(R4) + R3


• Source Address = M(R4)
• R4 is a Pointer to Data Before Data Memory After
Address Data Address Data

R4 0x60AA R4 0x60AA
R3 0x0D09 R3 0x0CDC
R2 R2
R1 R1
R0 R0
Address Data Address Data

0x60AC 0x60AC
0x60AA 0xFFD3 0x60AA 0xFFD3
0x60A8 0x60A8
0x60A6 0x60A6
0x60A4 0x60A4
Instruction Set
Data Memory
• Given: Address Data
R4 0x60AA
R3 0x00BC

• Example: AND instruction


AND.W R3,R4 ;R4 = R3 && R4, bitwise logical AND
;R4 = 0x00A8
Instruction Set
Data Memory
• Given: Address Data
R4 0x60AA
R3 0x00BC

• Example: Bit Set (Logical OR) instruction


BIS.W R3,R4 ;R4 = R3 || R4, bitwise logical OR
;R4 = 0x60BE
Instruction Set
Data Memory
• Given: Address Data
R4 0x60AA
R3 0x00FF

• Example: Bit Clear instruction


BIC.W R3,R4 ;R4 = ~R3 && R4
;R4 = 0x6000
Instruction Set
• Given: Data Memory
Address Data

R4 0xE0AA
R3 0x00BC

• Example: Rotate Right Arithmetic instruction:

RRA R4 ;Rotate R4 right 1 bit


;MSB -> MSB, LSB -> C
;divide by 2

R4 = 0xE0AA = 1110 0000 1010 1010


after RRA = 1111 0000 0101 0101
Example
• Write the code to calculate the sum of 3 memory locations starting from 0x6040, save
the sum in 0x6046. Use Absolute Addressing. Note: we do not know the contents of the
memory locations, we are just developing the code to add them.
Data Memory
Address Data

0x6048 ??
0x6046 ??
0x6044 ??
0x6042 ??
0x6040 ??
• Now use Indirect Auto-increment Addressing.
Example
• Write the code to calculate the sum of 3 memory locations starting from 0x6040, save the sum in
0x6046. Use Absolute Addressing. Note: we do not know the contents of the memory locations,
we are just developing the code to add them.
Data Memory
MOV.W &0x6040,R4 ;R4 = M(0x6040) Address Data

ADD.W &0x6042,R4 ;R4 = M(0x6042) + R4 0x6048 ??

ADD.W &0x6044,R4 ;R4 = M(0x6044) + R4


0x6046 ??
0x6044 ??
MOV.W R4,&0x6046 ;M(0x6046) = R4 0x6042 ??
0x6040 ??

• Now use Indirect Auto-increment Addressing.

MOV.W #0x6040,R3 ;R3 = 0x6040 (make R3 a pointer)


MOV.W #0x0000,R4 ;R4 = 0x0000 (clear R4)
ADD.W @R3+,R4 ;R4 = M(0x6040) + R4, inc R3
ADD.W @R3+,R4 ;R4 = M(0x6042) + R4, inc R3
ADD.W @R3+,R4 ;R4 = M(0x6044) + R4, inc R3
MOV.W R4,&0x6046 ;M(0x6046) = R4
Why use Indirect Addressing?
• Based on the last example, it seems like Indirect Addressing requires
more lines of code, so why use it?

• Because of arrarys!

• What’s an array and why are they useful?

• How would we utilize Indirect Addressing when dealing with an array?


Instruction Set: Machine Code
• 27 core instructions + 24 emulated instructions.
• All instructions are 16 bits long (1 word = 2 bytes).
• Anatomy of an Instruction (different parts):
• Opcode: What does the instruction do? Is it a MOV or an ADD? (all
instructions have opcode)
• Source: 1st data item to be manipulated (optional)
• Destination: 2nd data item to be manipulated, also where the results of
operation are stored (optional)
• Addressing Modes
• Recall, Assembly Instructions are the code and occupy memory in the
Flash/ROM.

Address Machine Code Assembly Language Comment


01002C 40B1 002B 0004 MOV.W #0x002B,0x0004(SP) ;copy #0x002B into addr SP+0x0004
Instruction Set: 3 Formats
• Format I: Instructions with 2 Operands

• Format II: Instructions with 1 Operand

• Format III: Jump Instructions


Format I Instruction Summary
MOV opcode 0100
ADD opcode 0101
AND opcode 1111
BIS opcode 1101

1 word = 2 bytes = 16 bits


b/w = 1 for Byte instruction, b/w = 0 for Word instruction

Register Mode: Ad = 0, As = 00 Ex. MOV.W R3, R4


Indexed Mode: Ad = 0, As = 01 Ex. MOV.W 4(R3), R8
Indirect Mode: Ad = 0, As = 10 Ex. ADD.W @R8, R11
Indirect Auto-Increment Mode: Ad = 0, As = 11 Ex. ADD.W @R8+, R11
Immediate Mode: Ad = 0, As = 11, S-reg = 0000 Ex. ADD.W #0x022A, R10

Additional Instructions:
SUB.B R3, R4 ;R4 = R4 – R3
BIS.B R3, R4 ;R4 = R4 (logical OR) R3
BIC.B R3, R4 ;R4 = R4 (logical AND) !R3
Instruction Set: Format I

• Format I has 2 Operands.

• Opcode: 4 bits, defines what function (MOV, ADD)


• S-reg: 4 bits, source operand register (R0-R15)
• Ad: 1 bit, addressing mode for destination operand
• b/w: 1 bit, word or byte
• As: 2 bits, addressing mode for source operand
• D-reg: 4 bits, destination operand register (R0-R15)
Micro-Architecture
Format I: Register Mode
• Example: MOV.W R3, R6

Opcode S-reg Ad b/w As D-reg

• Machine Code:

• Example: ADD.W R3, R6


Opcode S-reg Ad b/w As D-reg

• Machine Code:

• Example: ADD.B R3, R6 ??


Format I: Register Mode
• Example: MOV.W R3, R6
Opcode S-reg Ad b/w As D-reg

0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0

• Machine Code: 0x4306 (1 word = 2 bytes)

• Example: ADD.W R3, R6


Opcode S-reg Ad b/w As D-reg

0 1 0 1 0 0 1 1 0 0 0 0 0 1 1 0

• Machine Code: 0x5306


Format I: Indexed Mode
• Example: MOV.W 3(R11), R9

Opcode S-reg Ad b/w As D-reg

• Machine Code:

• Example: ADD.W 5(R10), R7


Opcode S-reg Ad b/w As D-reg

• Machine Code:

Format I: Indexed Mode
• Example: MOV.W 3(R11), R9

Opcode S-reg Ad b/w As D-reg

0 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1

• Machine Code: 0x4B19 0003

• Example: ADD.W 5(R10), R7


Opcode S-reg Ad b/w As D-reg

0 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1

• Machine Code: 0x5A17 0005


• Indexed Mode takes 4 cycles to execute because we have to deal with more
data!
Format I: Indirect Mode
• Example: MOV.W @R15, R8

Opcode S-reg Ad b/w As D-reg


0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 0

• Machine Code: 0x4F28


Format I: Indirect Mode Auto-Increment
• Example: ADD.W @R15+, R8

Opcode S-reg Ad b/w As D-reg

0 1 0 1 1 1 1 1 0 0 1 1 1 0 0 0

• Machine Code: 0x5F38


Format I: Immediate Mode
• Example: MOV.W #0x15CA, R8

Opcode S-reg Ad b/w As D-reg

0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0

• Same as Indirect Auto-Increment with S-reg = 0000 (R0)


• Add the “immediate” value at the end
• Machine Code: 0x4038 15CA

• All examples done in this class will use Register Mode with the
destination. Ad = 0.
CPU Clock
• Most digital circuits have a Clock (CLK) to synchronize operations.
Recall Flip-Flops need a CLK, State Machines needs Flip-Flops, etc.
• Period = T = Clock Cycle = time it takes signal to repeat
• Frequency = f = 1 / T = measured in Hz
• The “Clock” usually described in terms of freq (e.g. 3.1 Ghz)
• A “faster” Clock (frequency) means faster computations but requires
more power as you must “switch” it on and off more often.
Instruction Cycles and Instruction Length
• Instruction Cycles (time):
• Some instructions take longer to execute than others.
• We measure how long an instruction takes to complete in Clock Cycles.
• The number of Clock Cycles is dependent on the Addressing Modes used in
the instruction.
• Think, how long does it take to find the books in the library.
• Instruction Length (space):
• The amount of memory a given instruction occupies.
• Measured in bytes or words.
• Think, how many books in the library.
Format I Instructions Cycles and Length
Src Addr Mode Dest Addr Mode Clock Cycles Length (bytes) Examples

Register Register 1 2 MOV R3, R4


Register Indexed, 4 4 ADD R5, 3(R6)
Absolute MOV R5, &0x001F
Indexed, Register 3 4 MOV 2(R5), R7
Absolute AND &0x15DC, R6
Indexed, Indexed, 6 6 MOV 3(R4), &0x5D88
Absolute Absolute
Indirect Register 2 2 MOV @R5, R7
Indirect Indexed, 5 4 ADD @R4, 0(R6)
Absolute MOV @R3, &0x114C
Indirect auto-inc, Register 2 2 MOV @R5+, R6

Indirect auto-inc, Indexed, 5 4 MOV @R4+, 2(R6)


Absolute MOV @R4+, &0x024A

Immediate Register 2 4 MOV #0x0033, R8


Immediate Indexed, 5 6 MOV #0x0022, &0x2CC7
Absolute

• All Format III instructions are 1 cycle, 1 word.


Format I Instruction Summary

• Opcode: 4 bits, defines what function (MOV, ADD)


• S-reg: 4 bits, source operand register (R0-R15)
• Ad: 1 bit, addressing mode for destination operand
• b/w: 1 bit, word or byte
• As: 2 bits, addressing mode for source operand
• D-reg: 4 bits, destination operand register (R0-R15)
Format I Instruction Summary
MOV opcode 0100
ADD opcode 0101
AND opcode 1111
BIS opcode 1101

1 word = 2 bytes = 16 bits


b/w = 1 for Byte instruction, b/w = 0 for Word instruction

Register Mode: Ad = 0, As = 00 Ex. MOV.W R3, R4


Indexed Mode: Ad = 0, As = 01 Ex. MOV.W 4(R3), R8
Indirect Mode: Ad = 0, As = 10 Ex. ADD.W @R8, R11
Indirect Auto-Increment Mode: Ad = 0, As = 11 Ex. ADD.W @R8+, R11
Immediate Mode: Ad = 0, As = 11, S-reg = 0000 Ex. ADD.W #0x022A, R10

Additional Instructions:
SUB.B R3, R4 ;R4 = R4 – R3
BIS.B R3, R4 ;R4 = R4 (logical OR) R3
BIC.B R3, R4 ;R4 = R4 (logical AND) !R3
Micro-Architecture
Special Registers
• R0 (PC) – Program Counter
• R1 (SP) – Stack Pointer
• R2 (SR/CG1) – Status Register

• R0 (PC)
• PC always points to the address of the next instruction to be
fetched
• Instructions always occupy an even number of bytes.
• After fetch of an instruction, the PC register is automatically
incremented by 2, 4, or 6 to point to the next instruction (PC
counts in bytes, not words)
Revisit Previous Example (Sum 3 locations)
• Fill in the missing columns. What is the significance of the Cycles and
Bytes columns?

Addr Machine Assembly Cycles Bytes

010006 ___________ MOV.W #0x6040,R3

______ ___________ MOV.W #0x0000,R4

______ ___________ ADD.W @R3+,R4

______ ___________ BIS.B R7,R10

______ ___________ AND.B 4(R3),R4

______
Revisit Previous Example (Sum 3 locations)
• Fill in the columns. What is the significance of the Cycles and Bytes
columns?

Addr Machine Assembly Cycles Bytes

010006 4033 6040 MOV.W #0x6040,R3 2 4


01000A 4034 0000 MOV.W #0x0000,R4 2 4
01000E 5334 ADD.W @R3+,R4 2 2
010010 D74A BIS.B R7,R10 1 2
010012 F354 0004 AND.B 4(R3),R4 3 4
010016 ----

Total: 10 Cycles, 16 bytes

The PC always points to the address of the next instruction.


Instruction/Code Memory vs Data Memory
Instruction/Code Memory Data Memory (for a photo)
Addr Data MIPS Instruction Addr Data Representation
Machine Code

010006 40 33 60 40 MOV.W #0x6040,R3 1A0C00 00 00 00 00 Pixel 0, x position


01000A 40 34 00 00 MOV.W #0x0000,R4 1A0C04 00 00 00 00 Pixel 0, y position
01000E 53 34 ADD.W @R3+,R4 1A0C08 00 C1 25 00 Pixel 0, color
1A0C0C 00 00 00 01 Pixel 1, x position
1A0C10 00 00 00 01 Pixel 1, y position
1A0C14 00 11 73 2A Pixel 1, color

So the bits in the Data column for both examples represent different
things. It’s up to the processor to know what they represent!
Special Registers
• R1 (SP) – Stack Pointer
• The Stack is a section of RAM used to store info temporarily in a specific
way. You can define almost any part of RAM to be “the stack”
• Used to store local variables or the return address of routines or interrupts
• LIFO (Last In First Out) data structure.
• Stack grows down toward lower addresses.
• The register used to access the stack is called the SP (Stack Pointer, R1)
• SP is 16 bits wide.
• SP always holds address of the current Bottom of the stack.
• Stack Operations:
• PUSH src ;performs 2 tasks
• Copies data from source to bottom of Stack.
• SP decremented by two.
• POP dst ;performs 2 tasks, emulated
• Copies data from bottom of Stack to destination.
• SP incremented by two.
Stacks
• Example: Given the code below, draw the initial stack and after each
PUSH/POP. What data values are in R3, R4 and R5 at the end?

MOV.W 0x0030,SP ;set top of stack


MOV.W 0x0055,R3 ;R3 = 0x55
MOV.W 0x1099,R4
MOV.W 0x0ABC,R5

PUSH.W R3 ;place contents of R3 on stack, dec SP by 2


PUSH.W R4 ;place contents of R4 on stack, dec SP by 2
PUSH.W R5 ;place contents of R5 on stack, dec SP by 2
POP.W R3 ;copy bottom word to R3, inc SP by 2
POP.W R5 ;copy bottom word to R5, inc SP by 2
POP.W R4 ;copy bottom word to R4, inc SP by 2

(R3) =
(R4) =
(R5) =
Stacks
• Example: Given the code below, draw the initial stack and after each
PUSH/POP. What data values are in R3, R4 and R5 at the end?
Pic 1 Pic 2 Pic 3 Pic 4
MOV.W 0x0030,SP ;set top of stack addr data
SP = 0x30 0x30 0x30
MOV.W 0x0055,R3 ;R3 = 0x55
0x30
MOV.W 0x1099,R4
MOV.W 0x0ABC,R5 SP = 0x0055 0x2E 0x0055 0x2E 0x0055
0x2E

PUSH.W R3 ;place contents of R3 on stack, dec SP by 2 SP = 0x1099 0x2C 0x1099


0x2C
PUSH.W R4 ;place contents of R4 on stack, dec SP by 2
PUSH.W R5 ;place contents of R5 on stack, dec SP by 2
SP = 0x0ABC
POP.W R3 ;copy bottom word to R3, inc SP by 2 0x2A

POP.W R5 ;copy bottom word to R5, inc SP by 2


POP.W R4 ;copy bottom word to R4, inc SP by 2 Pic 5 Pic 6 Pic 7
0x30 0x30 SP =
0x30
(R3) = 0x0ABC
(R4) = 0x0055 0x2E 0x0055 SP = 0x0055
0x2E
(R5) = 0x1099
SP = 0x1099
0x2C
Special Registers
• R2 (SR/CG1) – Status Register
• The status of the MSP430 CPU is
defined by a set of bits contained
in register R2
• This register can only be accessed
through register addressing mode
- all other addressing modes are
reserved to support the constants
generator
• The status register is used for clock
selection, interrupt
enable/disable, and instruction
result status
V bit (Overflow)
• 8-bit Signed Numbers (2’s complement):
• D7 (MSB) is the sign
• If D7 = 0, the number is positive, D6-D0 is magnitude.
• If D7 = 1, the number is negative, perform 2’s complement to find magnitude.
• Positive numbers are 0 (0x00) to +127 (0x7F)
• Negative numbers are -128 (0x80) to -1 (0xFF)

• 4-bit Signed Numbers Example: 2 - 5 = -3


V bit (Overflow)
• V bit only has meaning for Signed Integer operations
• V is set if the result of the operation is outside of range (-128 to
+127)
• V = 1 if adding 2 positive numbers yields a negative result.
• V = 1 if adding 2 negative numbers yields a positive result.
• Example: V = 1 (verify in decimal)
MOV.B #0x56,R3
ADD.B #0x44,R3
C bit (Carry)
• The Carry bit only has meaning for unsigned integer operations
• C is set if the result of the operation is outside of range (0 to 255)

• Example 1: R3 =?, C = ?, V = ?
MOV.B #0x76,R3
ADD.B #0x55,R3

• Example 2: R3 =?, C = ?, V = ?
MOV.B #0xB7,R3
ADD.B #0xC9,R3
Subtraction
• Example: 0x4C – 0x6E = ? (Assume Unsigned Numbers)
MOV.B #0x4C,R3
SUB.B #0x6E,R3 ;R3 = 0x4C – 0x6E

• What value is the C bit?


• If bottom number bigger than top, result will be negative, can’t have negative
numbers! So C is set!
• If you don’t like Hex Subtraction, can take 2’s complement of subtrahend
(bottom number) and add it to the minuend (top number).
Subtraction
• Same Example: 0x4C – 0x6E = ? (Now assume Signed Numbers)
MOV.B #0x4C,R3
SUB.B #0x6E,R3 ;R3 = 0x4C – 0x6E

• What value is the V bit?


• 0x4C (positive number) – 0x6E (larger positive number) = 0xDE (negative
number), so V = 0
Micro-Architecture
Jumps
• When writing C code, often the code jumps (think “if”
statements, Loops). How is this done in Assembly Language?
• 2 Ways:
• Conditional Jump: jump to target if some condition met.
• Unconditional Jump: jump to target.

• Format III: Jump Instructions


Jumps
Format III: Jump Instructions

• 8 different Jump Instructions


▫ Opcode (3 bits): 001 for all of them
▫ Condition (3 bits):
(JEQ if zero bit set)
Jumps
• JMP (Unconditional Jump) Instruction
• JMP offset
• 10 bit signed offset
• Limited to jump forward 1022 bytes or jump backwards 1024 bytes in code (-
1024 to +1022) relative to the PC (the bookmark).
• Dest addr = (PC + 2) + (offset x 2)
• All instructions are guaranteed to be 2, 4 or 6 bytes wide, so Dest
addr is always an even number (that’s why it’s “times 2”)
• offset is calculated by the Assembler.
Jumps
• Example: Jump forward

addr Opcode Assembly


0x1050 0x3C60 JMP (0x????)
---

0x????
Jumps
• Example: Jump forward

addr Opcode Assembly


0x1050 0x3C60 JMP (0x????)
---

0x????

Opcode Condition Signed Offset

0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0

Offset = 0001100000 = 0x60


Offset x 2 (left shift to 11 bits) = 00011000000 = 0xC0
PC is 0x1052 (ALWAYS THE INSTRUCTION AFTER)
dest addr = (0x1050 + 2) + (0x60 x 2) = 0x1112
Jumps
• Example: Jump backward

addr Opcode Assembly


0x???? ---

0x1050 0x3E60 JMP (0x????)


Jumps
• Example: Jump backward

addr Opcode Assembly


0x???? ---

0x1050 0x3E60 JMP (0x????)

Opcode Condition Signed Offset

0 0 1 1 1 1 1 0 0 1 1 0 0 0 0 0

Offset = 10 0110 0000


Offset x 2 (11 bits) = 100 1100 0000
since it’s a negative number we need to sign extend it to match
PC’s 16 bits.
Offset x 2 = 1111 1100 1100 0000 = 0xFCC0
dstaddr = (0x1050 + 2) + 0xFCC0 = 0x0D12
Conditional Jumps
• Used to make decisions, IF statements, WHILE/FOR loops.

• A few more Instructions:


• Compare
CMP.B R3,R4 ;R4 - R3, discard result,
;save status bits: V,N,Z,C

• Test Destination Equal to Zero (this is an emulated instruction)


TST.B R4 ;emulated CMP.B #0x00,R4
IF THEN ELSE statements
Example 1:
if (R4 == 0) {
THEN R6 = #0x00
ELSE R6 = #0xFF
IF THEN ELSE statements
Example 1:
if (R4 == 0) {
THEN R6 = #0x00
ELSE R6 = #0xFF

TST.B R4 ;if R4 == 0, Z bit set


JNE ELSE ;Jump if Z Bit not set
MOV.B #0x00,R6
JMP EXIT
ELSE: MOV.B #0xFF,R6
EXIT:

Example 1 again, opposite logic:

TST.B R4
JEQ THEN ;Jump if Z Bit set
MOV.B #0xFF,R6
JMP EXIT
THEN: MOV.B #0x00,R6
EXIT:
IF THEN ELSE statements
Example 2:
IF (R4 < 0)
THEN R6 = #0x00
ELSE R6 = #0xFF

TST.B R4 ;if R4 == 0, Z bit set


JL THEN ;Jump Less, Jump if N = 1 and V = 0
MOV.B #0xFF,R6
JMP EXIT
THEN: MOV.B #0x00,R6
EXIT:
IF THEN ELSE statements
IF (R4 < R3)
THEN R6 = #0x00
ELSE R6 = #0xFF

CMP.B R3,R4 ;R4 – R3


JL THEN ;Jump Less, Jump if N = 1 and V = 0
MOV.B #0xFF,R6
JMP EXIT
THEN: MOV.B #0x00,R6
EXIT:
Forming Loops
FOR Loop is used when the number of repetitions is known. Data Memory
for (i = 0, i <100, i++) Address Data

R8
R5
WHILE Loop continues to execute as long as a condition is met.
R4
while (x < y)

Example: Use a loop to add 20 Words (consecutive memory locations) starting with 0x442A and
store the result in R8.
Data Memory
Address Data

… ??
… ??
… ??
0x4430 ??
0x442E ??
0x442C ??
0x442A ??
Forming Loops
FOR Loop is used when the number of repetitions is known. Data Memory

for (i = 0, i <100, i++) Address Data

R8 0x0000
R5 0x0014
WHILE Loop continues to execute as long as a condition is met. R4 0x442A

while (x < y)

Example: Use a loop to add 20 Words (consecutive memory locations) starting with
0x442A and store the result in R8.
Data Memory
MOV.W #0x442A,R4 ;setup pointer Address Data
MOV.W #0x0014,R5 ;setup loop counter “i” … ??
MOV.W #0x0000,R8 ;setup running sum … ??
START: ADD.W @R4,R8 ;add … ??

INCD.W R4 ;increment pointer 0x4430 ??

DEC.W R5 ;decrement loop counter 0x442E ??


0x442C ??
TST.W R5 ;i == 0?
0x442A ??
JNE START ;if (i != 0) jump to start
Quick Primer on C Code
Basic structure of Code:
• One “main.c” file. This is the main file.
• It’s good coding practice to break up your code into other .c
files, but this step is strictly for organization and not
necessary for your project to run correctly. But once again,
it’s good coding practice!
• Inside “main.c,” you must have: int main(void)
• This thread defines how your program will execute.
• All functions must be “called” from main.
Example: Function with “int” return value
#include <stdio.h>

int addition(int num1, int num2) //function declaration

int main(void)
{
int a = 2
int b = 5; //global variables
int res = addition(a, b); //function call, result stored in global variable “res”
return 0; //end of main program
}

int addition(int num1, int num2) //function definition


{
int sum; //local variable
sum = num1 + num2;
return sum;
}
Example: Function with “int” return value
#include <stdio.h> arguments
Return type

int addition(int num1, int num2) //function declaration

argument is void because main doesn’t take in arguments


int main(void)
{
int a = 2 //global variables
int b = 5;
int res = addition(a, b); //function call, result stored in global variable “res”
return 0; //end of main program
}

int addition(int num1, int num2) //function definition


{
int sum; //local variable
sum = num1 + num2;
return sum;
}
Example: Function with “void” return value
#include <stdio.h>
Return type

void addition(int num1, int num2) //function declaration

int main(void)
{
int a = 2; //global variables
int b = 5; Note, “res” must now be a global variable
int res = 0; Function modifies a Global variable so no need to return anything
addition(a, b); //function call
return 0; //end of main program
}

void addition(int num1, int num2) //function definition


{
res = num1 + num2; //result stored in global variable
}
Example: Function declared but not called
void addition(int num1, int num2) //function declaration
void subtraction(int num1, int num2) //function declaration

int main(void) Note: we never “call” subtraction from main, so the


function will never execute
{
int a = 2; //global variables
int b = 5;
int res = 0;
addition(a, b); //function call
return 0; //end of main program
}

void addition(int num1, int num2) //function definition


{
res = num1 + num2; //result stored in global variable
}

void subtraction(int num1, int num2) //function definition


{
res = num1 - num2; //result stored in global variable
}
EENG 320 Control Systems
Micro controlling DC Fan

• Why do we need transistor


Q1? Why not connect P6.0
directly to the DC fan?

• What is D1? Why is it


necessary?

• Why is R2 necessary?

• What is the difference


between Vcc and Vdd?
Micro controlling DC Fan
• The micro cannot supply enough current through P6.0 to
effectively “drive” the fan. Recall: P = VI

• D1 is a diode that provides a safe path for the inductive


kickback of the motor. If you switch off the current the
motor (an inductor) suddenly, it will try to maintain
whatever voltage is necessary to keep the current flowing
in the short term (recall: the current through an inductor
can’t change instantaneously). Without the diode, the
voltage across the motor will spike to keep the current
flowing, which might damage the transistor.

• R2 limits the current the digital output must


source/provide and the transistor base must handle. The
voltage across the transistor B-E junction is about 750 mV.
Holding a digital output at 750 mV when it is trying to
drive 3.3 V (Vdd) could damage the digital output.

• Vcc = supply voltage for circuit


• Vdd = working voltage for microcontroller
Chain Reaction Example
• https://www.youtube.com/watch?v=Ss-P4qLLUyk

You might also like