input/output systems
TRANSCRIPT
![Page 1: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/1.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
1
Input/Output Systems
Processor needs to communicate with otherdevices:
• Receive signals from sensors
• Send commands to actuators
• Or both (e.g., disks, audio, video devices)
![Page 2: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/2.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
2
I/O Systems
Communication can happen in a variety ofways:
• Binary parallel signal (e.g., the interfacethat you used for your robot)
• Serial signals (e.g., lab 3)
• Analog (e.g., encoding a value to becommunicated as a voltage)
![Page 3: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/3.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
3
I/O SystemsMany devices are operating independently
of the processor – except whencommunication happens
• We say that these devices are actingasynchronously of the processor
• The processor must have some way ofknowing that something has changed withthe device (e.g., that it is ready to send orreceive information)
![Page 4: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/4.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
4
Last Time
Basics of computer architecture
• Special-purpose registers (programcounter, …)
• General-purpose registers (fast, temporarymemory)
• Machine instructions
• Instruction decoding
![Page 5: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/5.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
5
Today
• Lab 4
• I/O via polling
• Serial interface
• I/O with interrupts
![Page 6: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/6.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
6
Semester Plan
• Tu April 18:– Lecture: I/O and lab 4– Reading: ESP chapters 3 & 4
• Wed April 19: Homework 3 available– Due Mon May 2
• Th April 20:– Lecture: interrupts, DMA
• Tu April 25:– Lecture: PWM and H-bridges
![Page 7: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/7.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
7
Semester Plan
• Th April 27:– Lecture: Device interaction– Reading: TBA
• Tu May 3:– Lecture: Multitasking and scheduling– Reading: ESP Chapters 5.x, 6.1, 6.2
• Th May 5:– Lecture: final review, discuss homework 3– Lab 4 due (@5:00)
• Fri May 13: Final exam (0800!)
![Page 8: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/8.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
8
Lab 4
• Lab 2: beacons were not distinguishable
• This lab: the robots will follow a specificsequence of beacons
• Each beacon:– Has its own ID #
– Encodes the ID # of the beacon that is next in thesequence
• Move toward the current beacon while “looking”for the 2nd beacon (to the left or right)
![Page 9: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/9.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
9
Lab 4
• 1 processor for robot control (lab 2)
• 1 processor for Ired serial processing (lab3)
• The two processors must interact in someway
![Page 10: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/10.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
10
Lab 4 Hints
• Keep in mind that the beacons are nolonger transmitting continuously– Signal strengths will inherently be lower
– You will only see signals on occasion (we canadjust this timing if you need it)
• Communication– Parallel digital
– Serial (digital): see coming slides
![Page 11: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/11.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
11
Lessons Learned from Labs 2 & 3
• Timing
• Debugging is the black hole
• Implement and test in stages
• Control the experiments
![Page 12: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/12.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
12
I/O By Polling
One possible approach: the processorcontinually checks the state of the device:
do {
x = PINB & 0x10;
}while(x == 0);
y = PINC …
![Page 13: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/13.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
13
I/O By Polling
What is wrong with this approach?
![Page 14: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/14.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
14
I/O By Polling
What is wrong with this approach?
• In embedded systems, we are typicallymanaging many devices at once
![Page 15: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/15.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
15
I/O By Polling
• We can potentially be waiting for a longtime before the state changes– We call this busy waiting
• The processor is wasting time that couldbe used to do other tasks
What is one way to solve this?
![Page 16: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/16.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
16
I/O By Polling: An Alternative
Alternative: do something while we arewaiting
do { x = PINB & 0x10;
<go do something else>}while(x == 0);
y = PINC …
![Page 17: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/17.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
17
Serial Communication
• In lab 3, you implemented a serial receiverin software
• Hardware implementations are verycommon:– Our mega 8 has a Universal, Asynchronous
serial Receiver/Transmitter (UART)– Handles all of the bit-level manipulation– You only have to interact with it on the byte
level
![Page 18: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/18.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
18
Mega8UART
![Page 19: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/19.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
19
Mega8UART
• Transmit pin(PD1)
![Page 20: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/20.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
20
Mega8UART
• Transmit pin(PD1)
• Transmitshift register
![Page 21: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/21.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
21
Mega8UART
• Receive pin(PD0)
![Page 22: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/22.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
22
Mega8UART
• Receive pin(PD0)
• Receiveshift register
![Page 23: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/23.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
23
Mega8 UART C Interface
ioinit(): initialize the port
getchar(): receive a character
kbhit(): is there a character in the buffer?
putchar(): put a character out to the port
See the Atmel FAQ from the main class webpage
![Page 24: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/24.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
24
Mega8 UART C Interface
printf(): formatted output
scanf(): formatted input
See the LibAvr documentation (or astandard C reference)
![Page 25: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/25.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
25
Serial I/O by Polling
int c;
while(1) {
if(kbhit()) {
// A character is available forreading
c = getchar();
<do something with the character>
}
<do something else while waiting>
}
![Page 26: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/26.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
26
I/O By Polling: An Alternative
Polling works great … but:
• We have to guarantee that our “somethingelse” does not take too long (otherwise,we may miss the event)
• Depending on the device, “too long” maybe very short
![Page 27: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/27.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
27
I/O by Polling
In practice, we typically reserve this pollingapproach for situations in which:
• We know the event is coming very soon
• We must respond to the event very quickly
(both are measured in nano- to micro-seconds)
![Page 28: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/28.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
28
An Alternative: Interrupts
• Hardware mechanism that allows someevent to temporarily interrupt an ongoingtask
• The processor then executes an interrupthandler (a small piece of code)
• Execution then continues with the originalprogram
![Page 29: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/29.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
29
Some Sources of Interrupts(Mega8)
External:• An input pin changes state• The UART receives a byte on a serial input
Internal:• A clock• Processor reset• The on-board analog-to-digital converter
completes its conversion
![Page 30: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/30.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
30
Interrupts
There are many possible interrupts
• How do we know which one has occurred?
• How does the processor respond to aspecific interrupt?
![Page 31: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/31.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
31
InterruptsHow do we know which interrupt has
occurred?• The mega8 hardware identifies each
interrupt with a unique integer
How does the processor respond to aspecific interrupt?
• The processor stores an interrupt table inprogram memory
![Page 32: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/32.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
32
Mega8 Interrupt Table Implementation
![Page 33: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/33.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
33
Mega8 Interrupt Table Implementation
Address inthe programmemory
![Page 34: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/34.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
34
Mega8 Interrupt Table Implementation
Changeprogramcounter tothe locationidentified by“EXT_INT1”
![Page 35: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/35.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
35
Last Time
• Lab 4 (now due in 2 weeks!)
• I/O by polling
• Serial I/O using UARTs
• I/O by interrupts
![Page 36: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/36.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
36
Today
• Interrupts
• Stacks
• Serial processing using interrupts andbuffers
![Page 37: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/37.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
37
![Page 38: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/38.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
38
![Page 39: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/39.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
39
Interrupt ExampleSuppose we are executing the
“something else” code:
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
![Page 40: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/40.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
40
An ExampleSuppose we are executing the
“something else” code:
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
![Page 41: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/41.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
41
An ExampleSuppose we are executing the
“something else” code:
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
![Page 42: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/42.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
42
An ExampleAn interrupt occurs (EXT_INT1):
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
![Page 43: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/43.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
43
An ExampleAn interrupt occurs (EXT_INT1):
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PCrjmp EXT_INT1
![Page 44: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/44.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
44
An ExampleAn interrupt occurs (EXT_INT1):
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PCrjmp EXT_INT1
remember this location
![Page 45: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/45.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
45
An ExampleExecute the interrupt handler
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PCrjmp EXT_INT1
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
![Page 46: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/46.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
46
An ExampleExecute the interrupt handler
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
![Page 47: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/47.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
47
An ExampleExecute the interrupt handler
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
![Page 48: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/48.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
48
An ExampleExecute the interrupt handler
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
![Page 49: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/49.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
49
An ExampleReturn from interrupt
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
PC
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
![Page 50: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/50.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
50
An ExampleReturn from interrupt
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETI
PC
![Page 51: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/51.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
51
An ExampleContinue execution with original
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETIPC
![Page 52: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/52.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
52
An ExampleContinue execution with original
LDS R1 (A)
LDS R2 (B)
CP R2, R1
BRGE 3
LDS R3 (D)
ADD R3, R1
STS (D), R3
EXT_INT1:
LDS R1 (G)
LDS R5 (L)
ADD R1, R2
:
RETIPC
![Page 53: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/53.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
53
Interrupt Routines
• Generally a very small number ofinstructions– We want a quick response so the processor
can return to what it was originally doing
• Register use– If the interrupt routine makes use of registers,
then it must restore their state beforereturning
– We accomplish this through the use of astack
![Page 54: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/54.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
54
The Stack
A hardware-supported data structurecomposed of:
• A block of memory
• A stack pointer (SP) that indicates thecurrent top of the stack
![Page 55: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/55.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
55
The Stack (an example)
0x45 SP
![Page 56: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/56.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
56
The Stack (an example)
0x45 SP
Operation: PUSH R1
(assume R1 contains 0x31)
![Page 57: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/57.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
57
The Stack (an example)
0x45SP0x31
Operation: PUSH R1
(assume R1 contains 0x31)
![Page 58: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/58.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
58
The Stack (an example)
0x45SP0x31
Now perform: PUSH R5
(assume R5 contains 0xF3)
![Page 59: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/59.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
59
The Stack (an example)
0x45
SP0x31
Now perform: PUSH R5
(assume R5 contains 0xF3)
0xF3
![Page 60: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/60.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
60
The Stack (an example)
0x45
SP0x31
The interrupt routine (orfunction) now performs itsjob …
0xF3
![Page 61: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/61.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
61
The Stack (an example)
0x45
SP0x31
The interrupt routine (orfunction) now performs itsjob (changing R1 andR5)… and now restoresthe state of R5 and R1 …
0xF3
![Page 62: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/62.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
62
The Stack (an example)
0x45
SP0x31
Now perform: POP R5
0xF3
![Page 63: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/63.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
63
The Stack (an example)
0x45
SP0x31
Now perform: POP R5R5 now is set to the valuethat is on the top of thestack (0xF3) …
0xF3
![Page 64: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/64.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
64
The Stack (an example)
0x45SP0x31
Now perform: POP R5R5 now is set to the valuethat is on the top of thestack (0xF3) … and thestack pointer isincremented
![Page 65: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/65.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
65
The Stack (an example)
0x45SP0x31
Now perform: POP R1R1 receives the value onthe top of the stack (0x31)
![Page 66: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/66.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
66
The Stack (an example)
0x45 SP
Now perform: POP R1R1 receives the value onthe top of the stack (0x31)and the SP is incremented
![Page 67: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/67.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
67
The Stack
In addition to the temporary storage ofregister values, the stack is also used to:
• Pass parameters to a function
• Store the return location for use after aninterrupt or a function call
• Store the value of the status register
![Page 68: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/68.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
68
Stack Manipulation in the Mega8
In the Mega8 and with our gcc compiler:
• Stack manipulation is typically hidden fromus
• This is true for functions as well asinterrupt routines
![Page 69: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/69.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
69
Back to Receiving Serial Data…
With this solution, how long can “something else” take?
int c;
while(1) {
if(kbhit()) {
// A character is available forreading
c = getchar();
<do something with the character>
}
<do something else while waiting>
}
![Page 70: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/70.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
70
Receiving Serial Data
How can we allow the “something else” totake a longer period of time?
![Page 71: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/71.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
71
Receiving Serial Data
How can we allow the “something else” totake a longer period of time?
• The UART implements a 1-byte buffer
• Let’s create a larger buffer…
![Page 72: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/72.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
72
Last Time
• Interrupt basics
• The stack
• Serial interrupt example
![Page 73: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/73.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
73
Today
• Finish serial interrupt example
• Counter/timer components of the mega8
• Direct memory access (DMA)
• Pulse-width modulation (PWM)
![Page 74: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/74.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
74
Receiving Serial Data
Creating a larger buffer. This will be aglobally-defined data structure composedof:
• N-byte memory space:char buffer[BUF_SIZE];
• Integers that indicate the first element inthe buffer and the number of elements:
int front, nchars;
![Page 75: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/75.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
75
Buffered Serial Data
Implementation:
• We will use an interrupt routine to transfercharacters from the UART to the buffer asthey become available
• Then, our main() function can remove thecharacters from the buffer
![Page 76: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/76.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
76
Interrupt HandlerSIGNAL(SIG_UART_RECV) {
// Handle the character in the UART buffer
int c = getchar();
if(nchars < BUF_SIZE) {
buffer[(front+nchars)%BUF_SIZE] = c;
nchars += 1;
}
}
![Page 77: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/77.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
77
Reading Out Characters
int get_next_character() {
int c;
if(nchars == 0)
return(-1); // Error
else {
// Pull out the next character
c = buffer[front];
--nchars;
front = (front + 1)%BUF_SIZE;
return(c);
}
}
![Page 78: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/78.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
78
An Updated main()
int c;
while(1) {
do {
c = get_next_character();
if(c != -1)
<do something with the character>
while(c != -1);
}
<do something else while waiting>
}
![Page 79: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/79.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
79
Buffered Serial Data
This implementation captures the essenceof what we want, but there are somesubtle things that we must handle ….
![Page 80: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/80.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
80
Buffered Serial Data
Subtle issues:
• The reading side of the code must makesure that it does not allow the buffer tooverflow– But at least we have BUF_SIZE times more
time
• We have a shared data problem …
![Page 81: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/81.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
81
The Shared Data Problem
• Two independent segments of code thatcould access the same data structure atarbitrary times
• In our case, get_next_character() could beinterrupted while it is manipulating thebuffer– This can be very bad
![Page 82: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/82.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
82
Solving the Shared Data Problem
• There are segments of code that we wantto execute without being interrupted
• We call these code segments criticalsections
![Page 83: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/83.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
83
Solving the Shared Data Problem
There are a variety of techniques that areavailable:
• Clever coding
• Hardware: test-and-set instruction
• Semaphores: software layer above test-and-set
• Disabling interrupts
![Page 84: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/84.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
84
Disabling Interrupts
• How can we modify get_next_character()?
• The it is important that the critical section be asshort as possible
Assume:
• serial_receive_enable(): enable interrupt flag
• serial_receive_disable(): clear (disable) interruptflag
![Page 85: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/85.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
85
Modified get_next_character()int get_next_character() {
int c;
serial_receive_disable();
if(nchars == 0)
serial_receive_enable();
return(-1); // Error
else {
// Pull out the next character
c = buffer[front];
--nchars;
front = (front + 1)%BUF_SIZE;
serial_receive_enable();
return(c);
}
}
![Page 86: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/86.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
86
Initialization Detailsmain()
{
nchars = 0;
front = 0;
// Enable UART receive interrupt
serial_receive_enable();
// Enable global interrups
sei();
:
![Page 87: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/87.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
87
Enable/Disable Serial InterruptOne bit of UCSRB determines whether the
serial receive interrupt is enabled ordisabled. Here is the code:
inline void serial_receive_enable(void) { UCSRB |= _BV(RXCIE); // Enable serial receive interrupt
}
inline void serial_receive_disable(void) { UCSRB &= ~_BV(RXCIE); // Disable serial receive interrupt
}
Sample code in discussion board/atmel FAQ
![Page 88: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/88.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
88
Enabling/Disabling Interrupts
• Enabling/disabling interrupts allows us toensure that a specific section of code (thecritical section) cannot be interrupted– This allows for safe access to shared
variables
• But: must not disable interrupts for a verylong time
![Page 89: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/89.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
89
Next Time
• Hardware timers
• Direct Memory Access
• Pulse Width Modulation
![Page 90: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/90.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
90
Counter/Timers in the Mega8
The mega8 incorporates three counter/timerdevices. These can:
• Be used to count the number of eventsthat have occurred (either external orinternal)
• Act as a clock
• Trigger an interrupt after a specifiednumber of events
![Page 91: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/91.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
91
Timer 0
• Input source:– Pin T0 (PD4)
– System clock• Potentially divided by a “prescaler”
• 8-bit counter
• When the counter turns over from 0xFF to0x0, an interrupt can be generated
![Page 92: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/92.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
92
Timer 0 Implementation
• Clock input to 10-bit counter
• Output bits: 3, 6, 8, and 10
![Page 93: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/93.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
93
Timer 0 Implementation
• Clock input to 10-bit counter
• Output bits: 3, 6, 8, and 10
![Page 94: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/94.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
94
Timer 0 Implementation
• Clock input to 10-bit counter
• Output bits: 3, 6, 8, and 10
![Page 95: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/95.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
95
Timer 0 Implementation
• Clock input to 10-bit counter
• Output bits: 3, 6, 8, and 10
![Page 96: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/96.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
96
Timer 0 Implementation
• Clock input to 10-bit counter• Output bits: 3, 6, 8, and 10
– These serve to divide the clock by thespecified number of counts
![Page 97: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/97.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
97
Timer 0 Implementation
MUX selects betweenthese different inputs
![Page 98: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/98.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
98
Timer 0 Implementation
MUX selects betweenthese different inputs
• Control bits determinesource
![Page 99: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/99.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
99
Timer 0 Implementation
MUX selects betweenthese different inputs
• 000: No input
![Page 100: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/100.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
100
Timer 0 Implementation
MUX selects betweenthese different inputs
• 001: System clock
![Page 101: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/101.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
101
Timer 0 Implementation
MUX selects betweenthese different inputs
• 010: System clock div 8
![Page 102: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/102.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
102
Last Time
• Interrupt-based serial interface
• Shared data problem– Critical sections
– Disabling interrupts
• Counter/timers
![Page 103: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/103.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
103
Today
• More about timer/counters– Implementing accurate delays
– Timers and interrupts
• Direct memory access
• Pulse-width modulation
![Page 104: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/104.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
104
Administrivia
• Lab 4 due in 1 week
• Homework 3 due on Monday
![Page 105: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/105.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
105
Grades to Date
• 61%graded sofar
• Blackboardweightedgrades areincorrect
![Page 106: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/106.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
106
Timer 0 Implementation
MUX selects betweenthese different inputs
• 011: System clock div 64
![Page 107: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/107.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
107
Timer 0 Implementation
MUX selects between thesedifferent inputs
• 110: Falling edge of pin T0
![Page 108: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/108.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
108
Timer 0 Implementation
MUX selects between thesedifferent inputs
• 111: Rising edge of pin T0
![Page 109: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/109.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
109
Timer 0
• TCNT0: 8-bitcounter (a register)
• TCCR0: controlregister
![Page 110: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/110.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
110
Timer 0
• Clock source fromprevious slide
![Page 111: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/111.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
111
Timer 0
• Increment counteron every low-to-hightransition
![Page 112: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/112.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
112
Timer 0 Example
Suppose:
• 16MHz clock
• Prescaler of 1024
• We wait for the timer to count from 0 to156
How long does this take?
![Page 113: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/113.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
113
Timer 0 Example
mssdelay 109948000,000,16
156*1024≈== µ
![Page 114: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/114.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
114
Timer 0 Code Exampletimer0_config(TIMER0_PRE_1024); // Prescale by 1024
timer0_set(0); // Set the timer to 0
// Do something else for a while
while(timer0_read() < 156) {
};
// Break out at ~10 ms
See Atmel FAQ for example code
![Page 115: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/115.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
115
Timer 0 Example
Advantage over delay_ms():
• Can do other things while waiting
• Timing is much more precise– We no longer rely on a specific number of
instructions to be executed
![Page 116: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/116.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
116
Timer 0 Example
Disadvantage:
• “something else” cannot take very muchtime
What is the solution?
![Page 117: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/117.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
117
Timer 0 Interrupt
What is the solution?
• Use interrupts!
• We can configure the timer to generate aninterrupt every time the timer’s counterrolls over from 0xFF to 0x00
![Page 118: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/118.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
118
Timer 0 Example II
Suppose:
• 16MHz clock
• Prescaler of 1024
How often is the interrupt generated?
![Page 119: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/119.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
119
Timer 0 Example II
msinterval 384.16000,000,16
256*1024==
How many counts do we need so that wetoggle the state of PB0 every second?
![Page 120: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/120.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
120
Timer 0 Example II
0352.61384.16
1000==
ms
mscounts
How many counts do we need so that wetoggle the state of PB0 every second?
We will assume 61 is close enough.
![Page 121: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/121.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
121
Example II: Interrupt Routine
SIGNAL(SIG_OVERFLOW0) {
++counter;
if(counter == 61) {
// Toggle output state every 61st interrupt:
// This means: on for ~1 second and then off for ~1 sec
PORTB ^= 1;
counter = 0;
};
};
See Atmel FAQ for example code
![Page 122: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/122.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
122
Example II: Initialization // Initialize counter
counter = 0;
// Interrupt occurs every (1024*256)/16000000 = .016384 seconds
timer0_config(TIMER0_PRE_1024);
// Enable the timer interrupt
timer0_enable();
// Enable global interrupts
sei();
while(1) {
// Do something else
};
![Page 123: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/123.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
123
Timer 0 with Interrupts
This solution is particularly nice:
• “something else” does not have to worryabout timing at all– PB0 state is altered asynchronously
• Note that we can still have the shared dataproblem (but not in this example)
![Page 124: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/124.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
124
Other Timers
Timer 1:
• 16 bit counter
Timer 2:
• 8 bit counter
![Page 125: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/125.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
125
Flow of Data in I/O
Back to our serial interrupt handlerexample…
• How does the data flow through theprocessor?
![Page 126: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/126.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
126
Interrupt Handler
SIGNAL(SIG_UART_RECV) {
// Handle the character in the UART buffer
int c = getchar();
if(nchars < BUF_SIZE) {
buffer[(front+nchars)%BUF_SIZE] = c;
nchars += 1;
}
}
![Page 127: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/127.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
127
Data Flow on Each Interrupt
Byte arrives atserial device
![Page 128: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/128.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
128
Data Flow on Each Interrupt
Interruptroutine loadsbyte into aregister
![Page 129: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/129.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
129
Data Flow on Each Interrupt
Interruptroutine thenwrites byteout to bufferin RAM
![Page 130: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/130.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
130
Flow of Data in I/OWith each transfer:• The byte value moves from the device to a
register• And then moves from the register to RAM
This is OK when we have very little data tomove
• But: when there is a lot of data, we canwaste a lot of CPU time in this doubletransfer
![Page 131: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/131.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
131
Moving a Lot of Data
Direct memory access:
• CPU gives control of the data bus to thedevice itself
• Device generates the address andread/write signals
• Once transfer is complete, CPU takescontrol back
![Page 132: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/132.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
132
Data Flow During DMA
Device writesdata directlyinto RAM
• Many bytesaretransferred ata time
![Page 133: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/133.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
133
Data Flow During DMA
• This data flow technique is common invideo, audio, and disk transfers
• Enables the CPU to perform someoperations in parallel
• Note: the mega8 itself does not supportDMA (but your home computer does)
![Page 134: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/134.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
134
Last Time
• Timers
• Timer interrupts
• Direct memory access
![Page 135: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/135.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
135
Today
• Pulse-width modulation (PWM)
• DC motor control with H-bridges
• Multitasking
![Page 136: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/136.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
136
Next Time
• Homework 3 discussion
• Final exam preparation– Friday, May 13th @ 8:00
![Page 137: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/137.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
137
Next Topic: Information Encoding
We have talked about various forms ofinformation encoding:
• Analog: use voltage to encode a value
• Parallel digital
• Serial digital
![Page 138: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/138.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
138
Next Topic: Information Encoding
An alternative: pulse-width modulation(PWM)
• Information is encoded in the timebetween the rising and falling edge of apulse
![Page 139: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/139.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
139
PWM Example:
RC Servo Motors
• 3 pins: power (red),ground (black), andcommand signal (white)
• Signal pin expects aPWM signal
![Page 140: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/140.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
140
PWM Example
Internal circuit translates pulse width into a goalposition:
• 0.5 ms: 0 degrees• 1.5 ms: 180 degrees
![Page 141: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/141.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
141
RC Servo Motors
• Internal potentiometer measures thecurrent orientation of the shaft
• Uses a Position Servo Controller: thedifference between current andcommanded shaft position determinesshaft velocity.
• Mechanical stops limit the range of motion– These stops can be removed for unlimited
rotation
![Page 142: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/142.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
142
PWM Example II:Controlling LED Brightness
What is the relationship of current flowthrough an LED and the rate of photonemission?
![Page 143: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/143.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
143
Controlling LED Brightness
What is the relationship of current flowthrough an LED and the rate of photonemission?
• They are linearly related (essentially)
![Page 144: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/144.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
144
Controlling LED Brightness
Suppose we pulse an LED for a given periodof time with a digital signal: what is therelationship between pulse width andnumber of photons emitted?
![Page 145: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/145.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
145
Controlling LED BrightnessSuppose we pulse an LED for a given period of
time with a digital signal: what is the relationshipbetween pulse width and number of photonsemitted?
• Again: they are linearly related (essentially)
• If the period is short enough, then the humaneye will not be able to detect the flashes
![Page 146: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/146.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
146
Controlling LED Brightness
We need:
• To produce a periodic behavior, and
• A way to specify the pulse width (or theduty cycle)
How do we implement this in code?
![Page 147: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/147.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
147
Controlling LED Brightness
How do we implement this in code?
One way:
• Interrupt routine increments an 8-bitcounter
• When the counter is 0, turn the LED on
• When the counter reaches some“duration”, turn the LED off
![Page 148: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/148.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
148
Interrupt Implementation
SIGNAL(SIG_OVERFLOW0) {++counter;if(counter == 0)
PORTB |= 1;if(counter >= duration)
PORTB &= 0b11111110;}
![Page 149: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/149.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
149
Initialization Details
• Set up timer
• Enable interrupts
• Set duration in some way– In this case, we will slowly increase it
What does this implementation look like?
![Page 150: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/150.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
150
Initializationint main(void) {
DDRB = 0xFF;PORTB = 0;
// Initialize countercounter = 0;
duration = 0;
// Interrupt configuration timer0_config(TIMER0_NOPRE); // No prescaler // Enable the timer interrupt timer0_enable(); // Enable global interrupts sei();
:
![Page 151: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/151.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
151
PWM Implementation
What is the resolution (how long is oneincrement of “duration”)?
![Page 152: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/152.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
152
PWM Implementation
What is the resolution (how long is one incrementof “duration”)?
• The timer0 counter (8 bits) expires every 256clock cycles
(assuming a 16MHz clock)
st µ1616000000
256==
![Page 153: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/153.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
153
PWM Implementation
What is the period of the pulse?
![Page 154: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/154.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
154
PWM Implementation
What is the period of the pulse?
• The 8-bit counter (of the interrupt) expires every256 interrupts
mst 096.416000000
256*256==
![Page 155: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/155.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
155
Doing “Something Else”:
unsigned int i;
while(1) {for(i = 0; i < 256; ++i)
duration = i;
delay_ms(50);};
};}
![Page 156: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/156.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
156
LEDs to DC Motors
• Current (ideally) isproportional to the torqueproduced by the motor
• Direction of current flowdetermines torquedirection
How can a digital inputcontrol torque magnitude?
www.tpub.com
www.pcgadgets.com
![Page 157: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/157.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
157
LEDs to DC Motors
How can a digital inputcontrol torque magnitude?
• Use PWM!
How do we handle torquedirection?
www.tpub.com
www.pcgadgets.com
![Page 158: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/158.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
158
LEDs to DC Motors
How do we handle torquedirection?
• +5V to north 0V to south
• 0V to north +5V to south
How would we implementthis?
www.tpub.com
www.pcgadgets.com
![Page 159: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/159.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
159
DC Motor Control
One possibility…
• Connect motordirectly to the I/O pins
Two directions:
• PD2: 1; PD3: 0
• PD2: 0; PD3: 1
![Page 160: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/160.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
160
DC Motor Control
One possibility…
• Connect motordirectly to the I/O pins
What is wrong with thisimplementation?
![Page 161: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/161.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
161
DC Motor ControlWhat is wrong with this
implementation?
• Our I/O pins cansource/sink at most 20mA of current
• This is not very muchwhen it comes tomotors…
How do we fix this?
![Page 162: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/162.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
162
Simple H-Bridge
![Page 163: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/163.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
163
Simple H-Bridge
Whathappenswith theseinputs?
0
01
1
![Page 164: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/164.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
164
Simple H-Bridge
Whathappenswith theseinputs?
• Motorturns inonedirection
0
01
1
![Page 165: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/165.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
165
Simple H-Bridge
How abouttheseinputs?
1
10
0
![Page 166: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/166.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
166
Simple H-Bridge
Whathappenswith theseinputs?
• Motorturns inthe otherdirection!
1
10
0
![Page 167: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/167.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
167
Simple H-Bridge
How abouttheseinputs?
1
01
0
![Page 168: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/168.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
168
Simple H-Bridge
Whathappenswith theseinputs?
• We shortpower toground
• … verybad
1
01
0
![Page 169: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/169.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
169
Simple H-Bridge
How can weprevent aprocessorfromaccidentallyproducingthis case?
1
01
0
![Page 170: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/170.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
170
Modified H-Bridge
We introduce alittle logic toensure theshort neveroccurs
![Page 171: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/171.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
171
Modified H-Bridge
What happenswith thisinput? 0
![Page 172: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/172.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
172
Modified H-Bridge
What happenswith thisinput? 0
0
01
1
![Page 173: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/173.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
173
Modified H-Bridge
What happenswith thisinput?
• Motor turnsin onedirection
00
01
1
![Page 174: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/174.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
174
Modified H-Bridge
How about thisinput?
1
![Page 175: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/175.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
175
Modified H-Bridge
What happenswith thisinput? 1
1
10
0
![Page 176: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/176.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
176
Modified H-Bridge
How about thisinput?
• Motor turnsin the otherdirection
11
10
0
![Page 177: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/177.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
177
Modified H-Bridge
11
10
0This implementation
is nice because weonly need onedirection bit ofcontrol
• What are wemissing?
![Page 178: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/178.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
178
Modified H-Bridge
11
10
0
What are wemissing?
• Control of torquemagnitude
• Let’s introduce asecond PWM input
What would this looklike?
![Page 179: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/179.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
179
PWM and Direction Control
![Page 180: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/180.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
180
PWM and Direction Control
Whathappenswith thisinput?
0x
![Page 181: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/181.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
181
PWM and Direction Control
Whathappens?
• No currentflow
0x
0
0
0
0
![Page 182: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/182.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
182
PWM and Direction Control
Whathappensnow?
1x
0
0
0
0
![Page 183: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/183.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
183
PWM and Direction Control
Whathappensnow?
• ‘x’determinesmotordirection
1x
x
x’
x’
x
![Page 184: Input/Output Systems](https://reader031.vdocument.in/reader031/viewer/2022030322/589449541a28abfb698bbb74/html5/thumbnails/184.jpg)
Andrew H. Fagg: EmbeddedReal-Time Systems: Input/Output
184
PWM and Direction Control
Direction
With thePWM input,we cancontroltorque