Class Processors
Class Processors
Components of a Microprocessor
Memory:
Storage of data Storage of a program Either can be temporary or permanent storage
Components of a Microprocessor
Instruction decoder:
Translates current program instruction into a set of control signals
Components of a Microprocessor
Many of these components must exchange data with one-another It is common to use a bus for this exchange
Collections of Bits
8 bits: a byte 4 bits: a nybble words: can be 8, 16, or 32 bits (depending on the processor)
Collections of Bits
A data bus typically captures a set of bits simultaneously Need one wire for each of these bits In the Atmel Mega2560: the data bus is 8bits wide In your home machines: 32 or 64 bits
Memory
What are the essential components of a memory?
A Memory Abstraction
We think of memory as an array of elements each with its own address Each element contains a value
It is most common for the values to be 8-bits wide (so a byte)
A Memory Abstraction
We think of memory as an array of elements each with its own address Each element contains a value
It is most common for the values to by 8-bits wide (so a byte)
Stored value
Address
Memory Operations
Read foo(A+5); reads the value from the memory location referenced by the variable A and adds the value to 5. The result is passed to a function called foo();
10
Memory Operations
Write A = 5; writes the value 5 into the memory location referenced by A
11
Types of Memory
Random Access Memory (RAM) Computer can change state of this memory at any time Once power is lost, we lose the contents of the memory This will be our data storage on our microcontrollers
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 12
Types of Memory
Read Only Memory (ROM) Computer cannot arbitrarily change state of this memory When power is lost, the contents are maintained
13
Types of Memory
Erasable/Programmable ROM (EPROM) State can be changed under very specific conditions (usually not when connected to a computer) Our microcontrollers have an Electrically Erasable/Programmable ROM (EEPROM) for program storage
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 14
CPU Exercise
15
Note: The concepts in the next ~50 slides below are covered in our acting exercise
16
Buses
In the simplest form, a bus is a single wire Many different components can be attached to the bus Any component can take input from the bus or place information on the bus
17
Buses
At most one component may write to the bus at any one time In a microprocessor, which component is allowed to write is usually determined by the code that is currently executing
18
19
Atmel Mega2560
8-bit data bus Primary mechanism for data exchange
20
Atmel Mega2560
32 general purpose registers 8 bits wide 3 pairs of registers can be combined to give us 16 bit registers
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 21
Atmel Mega2560
Special purpose registers Control of the internals of the processor
22
Atmel Mega2560
Random Access Memory (RAM) 8 KByte in size
23
Atmel Mega2560
Random Access Memory (RAM) 8 KByte in size Note: in high-end processors, RAM is a separate component
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 24
Atmel Mega2560
Flash (EEPROM) Program storage 256 KByte in size
25
Atmel Mega2560
Flash (EEPROM) In this and many microcontrollers, program and data storage is separate Not the case in our general purpose computers
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 26
Atmel Mega2560
EEPROM Permanent data storage
27
Atmel Mega2560
Arithmetic Logical Unit Data inputs from registers Control inputs not shown (derived from instruction decoder)
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 28
29
Machine-Level Programs
Machine-level programs are stored as sequences of atomic machine instructions Stored in program memory Execution is generally sequential (instructions are executed in order) But with occasional jumps to other locations in memory
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 30
Types of Instructions
Memory operations: transfer data values between memory and the internal registers Mathematical operations: ADD, SUBTRACT, MULT, AND, etc. Tests: value == 0, value > 0, etc. Program flow: jump to a new location, jump conditionally (e.g., if the last test was true)
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 31
32
33
Atmel Mega2560
Instruction decoder Translates current instruction into control signals for the rest of the processor
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 34
Atmel Instructions
35
37
38
41
42
45
46
An Example
A C code snippet: if(B < A) { D += A; }
49
An Example
The Assembly : A C code snippet: LDS R1 (A) LDS R2 (B) if(B < A) { CP R2, R1 D += A; BRGE 3 } LDS R3 (D) ADD R3, R1 STS (D), R3 .. Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers
50
An Example
A C code snippet: if(B < A) { D += A; }
Load the contents of memory location A into register 1
The Assembly : LDS R1 (A) PC LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) ADD R3, R1 STS (D), R3 ..
51
An Example
A C code snippet: if(B < A) { D += A; }
Load the contents of memory location B into register 2
The Assembly : LDS R1 (A) LDS R2 (B) PC CP R2, R1 BRGE 3 LDS R3 (D) ADD R3, R1 STS (D), R3 ..
52
An Example
A C code snippet: if(B < A) { D += A; }
Compare the contents of register 2 with those of register 1
This results in a change to the Andrew H. Fagg: Embedded Realstatus register Time Systems: Microcontrollers
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 PC BRGE 3 LDS R3 (D) ADD R3, R1 STS (D), R3 ..
53
An Example
A C code snippet: if(B < A) { D += A; }
Branch If Greater Than or Equal To: jump ahead 3 instructions if true
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 PC BRGE 3 LDS R3 (D) ADD R3, R1 STS (D), R3 ..
54
An Example
A C code snippet: if(B < A) { D += A; }
Branch if greater than or equal to will jump ahead 3 instructions if true
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) if true ADD R3, R1 STS (D), R3 .. PC
55
An Example
A C code snippet: if(B < A) { D += A; } The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) PC ADD R3, R1 STS (D), R3 ..
56
if not true
An Example
A C code snippet: if(B < A) { D += A; }
Load the contents of memory location D into register 3
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) PC ADD R3, R1 STS (D), R3 ..
57
An Example
A C code snippet: if(B < A) { D += A; }
Add the values in registers 1 and 3 and store the result in register 3
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) ADD R3, R1 PC STS (D), R3 ..
58
An Example
A C code snippet: if(B < A) { D += A; }
Store the value in register 3 back to memory location D
The Assembly : LDS R1 (A) LDS R2 (B) CP R2, R1 BRGE 3 LDS R3 (D) ADD R3, R1 PC STS (D), R3 ..
59
60
62
Atmel Mega2560
63
Atmel Mega2560
Pins are organized into 8-bit Ports: A, B, C L
But no I
64
Digital Input/Output
Each port has three special-purpose registers that control its behavior. For port B, they are:
DDRB: data direction register B PORTB: port output register B PINB: port input B
65
66
67
If nothing is connected to the pin, then the pin will appear to be in a random state
68
A First Program
Flash the LEDs at a regular interval How do we do this?
76
A First Program
How do we flash the LED at a regular interval? We toggle the state of PC0
77
A First Program
main() { DDRC = 0x3; while(1) { PORTC = PORTC | 0x1; // sets PC0 to 1 delay_ms(100); PORTC = PORTC & ~0x1; // set PC0 to 0 delay_ms(100); } }
78
A First Program
main() { DDRC = 0x3; while(1) { PORTC = PORTC ^ 0x1; delay_ms(100); } }
// flip PC0 to 1
79
A First Program
main() { DDRC = 1; // Set port C pin 0 as an output while(1) { PORTC = PORTC ^ 0x1; delay_ms(500); } }
81
A Second Program
main() { DDRC = 3; // Set port C pins 0, and 1 as outputs while(1) { PORTC = PORTC ^ 0x1; delay_ms(500); PORTC = PORTC ^ 0x2; delay_ms(250); PORTC = PORTC ^ 0x2; delay_ms(250); } }
// XOR bit 0 with 1 // Pause for 500 msec // XOR bit 1 with 1 // XOR bit 1 with 1
A Second Program
main() { DDRC = 3; // Set port C pins 0, and 1 as outputs while(1) { PORTC = PORTC ^ 0x1; delay_ms(500); PORTC = PORTC ^ 0x2; delay_ms(250); PORTC = PORTC ^ 0x2; delay_ms(250); } }
// XOR bit 0 with 1 // Pause for 500 msec // XOR bit 1 with 1 // XOR bit 1 with 1
Port-Related Registers
Some of the C-accessible registers for controlling digital I/O: Directional control DDRB DDRC DDRD Writing PORTB PORTC PORTD Reading PINB PINC PIND
84
Bit Masking
main() { DDRB = 0x38; : : uint8_t val; val = command_to_robot; PORTB = ???? } // // A value between 0 and 7 // Set pins B3, B4, B5 as outputs
Fill this in
86
Bit Masking
main() { DDRB = 0x38; : : uint8_t val; val = command_to_robot; PORTB = (PORTB & ~0x38) | ((val & 0x7)<<3); } // A value between 0 and 7 // Set the current B3-B5 to 0s // OR with new values (shifted // to fit within B3-B5 // Set pins B3, B4, B5 as outputs
87
88
outval = ??? }
89
: : unsigned short val, outval; val = PINB; outval = (val & 0xC0) >> 6; } // A short is 8-bits wide
90
Program download:
We will use in circuit programming: you will be able to program the chip without removing it from your circuit
Andrew H. Fagg: Embedded RealTime Systems: Microcontrollers 91