Professional Documents
Culture Documents
EE 331: Microprocessor
Chapter Objectives
Fetch and Execute units: PC, ID, ALU, Stack, Program Store, Data Store, Status Register, etc. Peripherals: Oscillator, Timer, I/O Ports, Data EEPROM, etc. Problem solving
EE 331: Microprocessor
Microprocessors (MPU) and microcontrollers (MCU) are different MPU is general purpose in nature, and generally contains only CPU on one chip MCU is basically a microprocessor, memory, timers, I/O ports, etc. all on one chip
MCU is less flexible than MPU as it has fixed amount of memory and internal peripherals MCU is a complete system on one chip, hence, normally used in embedded systems
We can at any time build our own flexible MPU-based system by adding external chips to MPU
EE 331: Microprocessor
Arithmetic logic unit (ALU) Accumulator (or Working Reg) Instruction decoder Control unit Memory/Registers Internal system bus
EE 331: Microprocessor
Performs arithmetic (e.g. add, sub, etc.) and logic operations (e.g. AND, OR, etc.)
The accumulator is usually the register that is used in all ALU operations When data is sent from the MPU to external world or received from the external world to the MPU, it normally goes out or comes into from the accumulator via data bus
EE 331: Microprocessor
Instruction decoder
Decodes the instructions (it receives over the internal data bus) at a binary level, and sends the appropriate signals to the control unit Performs the operation as instructed Sends data to the appropriate internal section Also sends out any needed control signals externally to the MPU over the control bus to external devices (e.g. memories, I/O, etc.)
Control unit
EE 331: Microprocessor
Used by the MPU for data storage The size and number vary depending upon specific MPU type Accumulator Program counter (PC)
Instructions are normally stored sequentially in program memory, and PC is the counter register that keeps track of the current instruction A simple binary up counter PC always contains only the memory address of the next instruction to be executed Sometimes, referred as Instruction pointer
EE 331: Microprocessor
EE 331: Microprocessor
EE 331: Microprocessor
10
Q. Can you add two 16-bits numbers using this hardware architecture?
EE 331: Microprocessor
11
For a real application, a microcontroller alone is not enough; besides it needs a program that would be executed
EE 331: Microprocessor
12
EE 331: Microprocessor
13
Other Microcontroller
EE 331: Microprocessor
14
PIC12F508/509 Microcontroller
EE 331: Microprocessor
15
EE 331: Microprocessor
16
MCU Programming
Programming can be done in several languages, such as Assembly, C and BASIC, which are the most commonly used languages Assembly belongs to lower level languages that is programmed slowly, but takes up the least amount of space in memory and gives the best results Programs in C language are easier to be written and understood, but are slower in executing from assembly programs BASIC is the easiest one to learn, and its instructions are nearest a human's way of reasoning, but like C language, it is also slower than assembly
EE 331: Microprocessor
17
Assemble: translation from the assembly code to the target machine code Both compiler and assembler are machine (and vendor) specific (e.g., PIC assembler won't work with ARM)
Dr. KHAN WAHID 2010-11 (Term 1)
EE 331: Microprocessor
18
Consider a car odometer monitoring system that displays two things: (a) total distance since manufactured and (b) a trip odometer
Resource Budget (List of subsystem): 1. An edge-triggered input for the tachometer pulse train to add up engine revolutions 2. Gear ratio, mi/km option and trip reset 3. Four output digital lines to clock the two shift registers and provide segment data 4. An MPU to do the calculations and to read/write to the input/output ports 5. Program memory, usually ROM of some kind 6. Data memory for temporary storage of program variables, usually static RAM 7. Non-volatile memory to store physical variables
EE 331: Microprocessor
19
A greenhouse controller is to monitor an analog signal from a soil moisture probe and if below a certain value turn on a water valve for 5 seconds and off for 5 seconds.
Resource Budget (List of subsystem): 1. An input for an external oscillator, connected to a counter/timer to allow the MCU to calculate time. In practice the system clock can often be used by this internal timer to measure duration. 2. A 1-input analog input line to measure the analog signal from the moisture detector 3. A 1-input digital line to check the level of the reservoir water tank 4. A 1-output digital line to open and close the water valve 5. A 1-output digital line to activate the buzzer alarm 6. A microprocessor to do the calculations and to read/write to the input/output ports, respectively 7. Program memory, usually ROM of some kind 8. Data memory for temporary storage of program variables, usually static RAM
EE 331: Microprocessor
20
PIC was originally a design of General Instruments (now Motorola Connected Home Solutions) Microchip (formed in 1989) developed their first 8-bit Harvard architecture MCU PIC16C5xx All MCUs are low-cost, self-contained, Harvard arch, pipelined, RISC, single accumulator, with fixed reset and interrupt vectors
Instruction Word size 12 or 14-bit 12-bit 14-bit 16-bit 16-bit Number of Instructions 33 33 35 58, including hardware multiply 77, including hardware multiply Interrupt Vectors None None 1 4 2 (prioritised)
PIC Family
EE 331: Microprocessor
21
EE 331: Microprocessor
22
PIC16F84A
EE 331: Microprocessor
23
Parameter Name Program Memory Type Program Memory (KB) CPU Speed (MIPS) RAM Bytes Data EEPROM (bytes) Timers Temperature Range (C)
EE 331: Microprocessor
24
A good understanding of the block diagram leads to an ability to design with the microcontroller, and to program it.
Dr. KHAN WAHID 2010-11 (Term 1)
EE 331: Microprocessor
25
Block diagram from the textbook for better understanding Lets explore inside and look at the components one by one
EE 331: Microprocessor
26
System-look of ALU-Accumulator
IR / ID
EE 331: Microprocessor
27
An MPU needs a set of instructions to follow that tells it what operations to perform on what data
It fetches the first instruction from a designated area in its memory Loads it into instruction register Decodes it and executes that operation This sequence of fetch, decode and execute is continued until instructed to stop
EE 331: Microprocessor
28
MPU: Fetch-Decode-Execute
Example: Num_2 = Num_1 + 4 => D[26] = D[25] + 4
IR
EE 331: Microprocessor
29
Program Store
Mnemonics (Assembly)
Hexadecimal
Dr. KHAN WAHID 2010-11 (Term 1)
EE 331: Microprocessor
30
MPU: Fetch-Decode-Execute
(14-bits)
(8-bits)
??
Basic All-purpose Stored Instruction Computer (BASIC)
?? ??
??
EE 331: Microprocessor
31
MPU: Fetch-Decode-Execute
(14-bits)
(8-bits)
movf 25, w
EE 331: Microprocessor
32
MPU: Fetch-Decode-Execute
(14-bits)
(8-bits)
addlw 04
Adds literal (i.e., a constant) 04 to w and save into w
movwf 26
Move the contents of w into File h26 (save results)
Dr. KHAN WAHID 2010-11 (Term 1)
EE 331: Microprocessor
33
2-Stage Pipelining
Any execution of memory access will be on the Data store and each store has its own bus
Pipelining: While instruction n is being fetched, instruction (n 1) is being executed (see SAQ3.3)
EE 331: Microprocessor
34
2-Stage Pipelining
next current
Any execution of memory access will be on the Data store and each store has its own bus
The instruction codes for both the current and the immediate next instructions are held in the two Instruction registers IR2 and IR1 respectively During each cycle, except for the first, one fetch and one execution are proceeding simultaneously
EE 331: Microprocessor
35
2-Stage Pipelining
Assume that, the PC has been reset to h000 and has just finished the Cycle 1 (Fetch) Cycle 2 (Fetch) Increment PC (h001) to point to inst 2 Simultaneously move the inst word 1 down the pipeline from IR1 to IR2 PC (h001) to the Program stores address bus The inst word 2 then appears on the Program stores data bus and is loaded into IR1 Cycle 2 (Execute) The operand address h25 moves to the FAR and out onto the FA bus The content is read onto the Data stores data bus and loaded into the FDR The ALU is configured to the Pass Through mode, which feeds the data through to the Working register
EE 331: Microprocessor
36
2-Stage Pipelining
Cycle 3 (Fetch) Increment PC (h002) to point to inst 3 Simultaneously move the inst word 2 down the Pipeline from IR1 to IR2 PC (h002) to the Program stores address bus The inst word 3 then appears on the Program stores data bus and is loaded into IR1 Cycle 3 (Execute) The ALU is configured to the Add mode and the literal (which is part of inst word 2) is added to the data in W The ALU output, (NUM_1 + 4), is placed in W
EE 331: Microprocessor
37
2-Stage Pipelining
Cycle 4 (Fetch) Increment PC (h003) to point to inst 4 Simultaneously move the inst word 3 down the Pipeline from IR1 to IR2 PC (h003) to the Program stores address bus The inst word 4 then appears on the Program stores data bus and is loaded into IR1 Cycle 4 (Execute) The operand address (i.e., NUM_2) h26 to the FAR and out onto the FA bus The ALU is configured to the Pass Through mode, which feeds the contents of W through to the FDR and onto the Data stores data bus The data in the FDR is written into the Data store at the address on the Data stores address bus and becomes the new data in NUM_2
EE 331: Microprocessor
38
2-Stage Pipelining
EE 331: Microprocessor
39
File Direct
Instructions that specify the File address where their target operand is located use this type of addressing; e.g., movf h25,w designates File h25 as the target
001000
EE 331: Microprocessor
40
The leftmost six bits (#13 through #8): operation code, or op-code
Every instruction has a unique op-code, and it is this pattern that the decoding circuits use to define what type of instruction it is
The middle bit (#7) labeled d defines the destination of the outcome
x x
y x+y
EE 331: Microprocessor
41
The leftmost six bits (#13 through #8): operation code, or op-code
Every instruction has a unique op-code, and it is this pattern that the decoding circuits use to define what type of instruction it is
The middle bit (#7) labeled d defines the destination of the outcome
The rightmost seven bits (#6 through #0) define the File address
E.g. File h25 is b010_0101 Only a bank of 27 = 128 Files can be directly addressed; that is File 00 through File h7F
001000
EE 331: Microprocessor
42
The leftmost six bits (#13 through #8): operation code, or op-code
Every instruction has a unique op-code, and it is this pattern that the decoding circuits use to define what type of instruction it is
The outcome is always in the Working register and so there is no need for a destination bit nor Data store address
The literal is limited to the range b00000000 b11111111 (h00 hFF, or decimal zero through 255),
EE 331: Microprocessor
43
PIC16F84A
Let us now extend the BASIC architecture (discussed earlier) to PIC16F family Two parts in common:
EE 331: Microprocessor
44
Fetch Unit
Primarily concerned in fetching instructions down into the Pipeline from the Program store Program Store (16F84A)
Flash EEPROM type 1024 (1k) instructions can be stored (8192 instructions in PIC16F876/7) 14-bit long instruction
EE 331: Microprocessor
45
Primarily concerned in fetching instructions down into the Pipeline from the Program store Program Store (16F84A)
Flash EEPROM type 1024 (1k) instructions can be stored (8192 instructions in PIC16F876/7) 14-bit long instruction
EE 331: Microprocessor
46
Program Counter
13-bit register normally increments after each fetch, effectively acting as a binary counter However, there are a few instructions, (such as goto, call) that will cause execution of the program to jump to another part of the Program store Thus PCs normal up count can be overridden If the program attempts to go to any instruction above 1024, the address will wrap around back to address zero
6
EE 331: Microprocessor
47
Program Counter
In all cases the PC will be cleared on a reset; that is, the first instruction is always in location h000 (reset vector) In 16F84A, out of the 13-bits, only the lower 10-bits are physically connected (hardwired)
PIC16F628 uses 11-bits (211 = 2K), 16F874 uses 12-bits (212 = 4K), 16F877 and 16F886 uses all 13 bits (213 = 8K)
EE 331: Microprocessor
48
Instruction Decoder
uses logic circuitry to decode the 6-bit op-code of the 14-bit instruction gates the appropriate addresses and data to the correct execution units circuitry configures the ALU
All PIC MCU have an internal circuitry to generate time-related sequences of operations commanded by the Instruction Decoder
EE 331: Microprocessor
49
Q1: Decode Q2: Read Q3: Process Q4: Write To A consequence is that an instruction cycle takes four external clock periods (1/fosc) to complete
Example: with a 4MHz crystal, the instruction cycle rate is fosc/4, or 1MHz => 1 instruction takes 1us (4/fosc) to complete
EE 331: Microprocessor
50
Needs
EE 331: Microprocessor
51
movf h25, w
001000_0_0100101 ffffffff
001000_0_0100101
001000
0100101
d=0
d=1
EE 331: Microprocessor
52
addlw h04
111110_00000100
111110_00000100
111110
00000100
d=0
d=1
EE 331: Microprocessor
53
movwf h26
000000_1_0100110 ffffffff
000000_1_0100110
000000
0100110
d=0
d=1
EE 331: Microprocessor
54
Some MCUs have on-chip oscillator that supplies synchronized clock pulses
Usually configured to use quartz crystal or ceramic resonator for frequency stabilization Instructions are not executed at the rate imposed by the oscillator itself, but several times slower (do you remember WHY?)
Because each instruction is executed in several steps In some MCU, the same number of cycles is needed to execute any instruction, while in others, the execution time is not the same for all instructions
EE 331: Microprocessor
55
Some popular clock frequencies for PIC16 series For the fastest clock frequency, 20 MHz (the instruction cycle freq. is 5 MHz => 5,000,000 instructions per sec; period of 200 ns) The slightly cheaper version of the controller, the 16F84-04, with maximum clock frequency of 4 MHz (1 us)
Simple timing applications, using software delay loops and the counter/timer
The result is very low power, but strictly no high-speed calculations! E.g., digital clock, wristwatches (WHY used?)
http://seng.ulster.ac.uk/eme/sidk/quintessential/Chapters_2ed/Chapter4/ch4_9_2.htm
EE 331: Microprocessor
56
Clock Oscillator
Resistor-Capacitor, P = CV2f
Crystal Oscillator
Any mechanical distortion of the material causes a voltage to be produced across opposite sides of it; similarly, if a voltage is applied to the material, a mechanical distortion results Crystal vibration occurs at a fixed and remarkably stable frequency a great advantage
EE 331: Microprocessor
57
Four different oscillator modes, allowing implementation of RC, crystal or ceramic oscillators, and external clock source
RC resistorcapacitor
An external R-C must be connected to pin 16. This is the lowest cost way of getting an oscillator, but should not be used when any timing accuracy is required. This mode is intended for low-frequency crystal applications (up to 200kHz) and gives the lowest power consumption possible. This is the standard crystal configuration (1MHz 4MHz) This is a higher drive version of the XT configuration (4MHz 20MHz). It recognizes that higher frequency crystals, and ceramic resonators in general, require a higher drive current. It leads to the highest current consumption of all the oscillator modes.
LP low power
XT crystal
HS high speed
EE 331: Microprocessor
58
Four different oscillator modes, allowing implementation of RC, crystal or ceramic oscillators, and external clock source
Pin 15
EE 331: Microprocessor
59
EE 331: Microprocessor
60
All PIC MCU families feature a Sleep mode which effectively turns off the internal clock oscillator => using sleep instruction. Once asleep, the contents of the Data store are retained provided that the supply voltage remains above 1.5V (VDR in the Data sheet). The PIC MCU can be awakened either by resetting the device, by an enabled interrupt request, or if the enabled Watchdog timer overflows. [SID pp. 277] Dr. KHAN WAHID
2010-11 (Term 1)
EE 331: Microprocessor
61
A last in, first out (LIFO) special memory structure characterized by only two operations push and pop At any given time, the SP holds the address of the next free location in the stack
EE 331: Microprocessor
62
Eight 13-bit registers are stacked below and are connected to the PC
Used to hold past states of the PC to remember the jumping-off point when a subroutine is called up Also used for goto instruction No push and pop instructions in 16F84A Details to follow
Call subroutine (push PC to stack) Return from subroutine (pop PC from stack)
Dr. KHAN WAHID 2010-11 (Term 1)
EE 331: Microprocessor
63
PIC16F84A Microcontroller
We have so far discussed the Fetch unit Now let us look into the Execute unit
EE 331: Microprocessor
64
Acknowledgments
These slides have been prepared by Khan Wahid and may contain material copyrighted by:
The Quintessential PIC Microcontroller, Sid Katzen, 2nd edition, 2005, ISBN: 978-1-85233-942-5 Designing Embedded Systems with PIC Microcontrollers: Principles and Applications, Tim Wilmshurt, 2007, ISBN: 9780750667555 PIC16F84A Data Sheet, 35007b, Microchip Technology Inc. Microprocessors/Microcontrollers Course Notes, Delmar Cengage Leaning, ISBN: 1435453816 PIC Microcontroller and Embedded Systems using Assembly and C for PIC18, Mazidi, Mckinlay, Causey, 2008 PIC Microcontrollers, Milan Verle, free online (www.mikroe.com/en/books/picmcubook/)