EENG 371 Lecture Notes
EENG 371 Lecture Notes
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 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.
• 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]
• 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
• 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
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?
R4 0x60AA R4 0x0005
R3 0x0005 R3 0x0005
R2 R2
R1 R1
R0 R0
Indexed Mode
• Example: MOV instruction
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
R4 0x00AA R4 0x00AA
R3 R3
R2 R2
R1 R1
R0 R0
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
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
• Source = 0x04A8
• The “#” prefix indicates Immediate Mode
R4 R4
R3 0x0005 R3 0x04A8
R2 R2
R1 R1
R0 R0
Various Combinations
• Examples:
MOV.W #0x04A8,&0x8015 ;copy #data to M(0x8015)
• R4 is the “Destination”
R4 0x604A R4 0x5106
R3 0xF0BC R3 0xF0BC
R2 R2
R1 R1
R0 R0
Instruction Set
• Example: ADD instruction
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
R4 0xE0AA
R3 0x00BC
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
• Because of arrarys!
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
• Machine Code:
• Machine Code:
0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0
0 1 0 1 0 0 1 1 0 0 0 0 0 1 1 0
• Machine Code:
• Machine Code:
•
Format I: Indexed Mode
• Example: MOV.W 3(R11), R9
0 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1
0 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1
0 1 0 1 1 1 1 1 0 0 1 1 1 0 0 0
0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0
• 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
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?
______
Revisit Previous Example (Sum 3 locations)
• Fill in the columns. What is the significance of the Cycles and Bytes
columns?
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?
(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
• 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
0x????
Jumps
• Example: Jump forward
0x????
0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0
0 0 1 1 1 1 1 0 0 1 1 0 0 0 0 0
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
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
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 … ??
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 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
}
• Why is R2 necessary?