Embedded Systems 1
11.10.2016
Embedded Systems I Thema am 11.10.2016: AVR-Studio, Befehlsstruktur der Atmel-AVR-Familie, Adressierungsarten, Initialisierung der Peripheriemodule, Vorbesprechung Praktikum 1 und 2. Übungsaufgabe ACHTUNG: OHP-Handfolien mitschreiben! Miniprojekt
© Ulrich Schaarschmidt FH Düsseldorf, WS 2016/17
Literaturhinweise nn: ATMega1284P, Datenblätter der Fa. Atmel / Microchip,doc8059.pdf Nn: AVRxxx.pdf (xxx = 000 ... 999), Atmel Application Notes nn: ATMega16, auch ATMega32, Datenblätter der Fa. Atmel / Microchip,
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
2
1
Embedded Systems 1
11.10.2016
Anmerkungen zum Praktikum Der C-Compiler (WinAVR) ist im AVRStudio integriert. D.h., dass der Quellcode direkt in einem Editor-Fenster des AVRStudios eingegeben werden kann. Der eigentliche Programmablauf zur Erledigung von Aufgaben muss vom Software-Ingenieur vorher überlegt werden. 11.10.2016
Prof. Dr. U.G. Schaarschmidt
3
Programmierung für MCUs Muss der Programmierer mehr wissen als bei der PC-Programmierung? Direkter an der Hardware als beim PC, Ein-/Ausgabe-Bausteine müssen initialisiert werden (damit ein Treiber für ein I/O-Beinchen weiß, ob er z:B. Input oder Output in verschiedenen Spielarten sein soll); 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
4
2
Embedded Systems 1
11.10.2016
Programmierung für MCUs Was ist modulare Programmierung? Polling - was ist das? Was sind Interrupts – und muss man Angst davor haben? Zeitschleifen oder Timer/Counter-Module?
11.10.2016
Prof. Dr. U.G. Schaarschmidt
5
ATMEGA1284P -Architektur
Quelle: doc8059.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
6
3
Embedded Systems 1
11.10.2016
Blockdiagramm ATMEGA
11.10.2016
Prof. Dr. U.G. Schaarschmidt
7
Übersicht Memory Maps
Quelle: doc8059.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
8
4
Embedded Systems 1
11.10.2016
Universal-Arbeitsregister des AVR Quelle: doc8059.PDF
11.10.2016
Prof. Dr. U.G. Schaarschmidt
9
Die Ports – Ein-/ Ausgabemöglichkeiten Port A (PA7:PA0) Port A serves as analog inputs to the Analog-to-digital Converter. Port A also serves as an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port A pins that are externally pulled low will source current if the pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port A also serves the functions of various special features of the ATmega1284P as listed on page 78 [doc8059.pdf]. 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
10
5
Embedded Systems 1
11.10.2016
Die Ports – Ein-/ Ausgabemöglichkeiten Port B (PB7:PB0), Port C (PC7:PC0), Port D (PD7:PD0) Port B is an 8-bit bi-directional I/O port with internal pullup resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B also serves the functions of various special features of the ATmega1284P as listed on page 80 [doc8059.pdf]. 11.10.2016
Prof. Dr. U.G. Schaarschmidt
11
Ein- / Ausgabe ganz nah
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
12
6
Embedded Systems 1
11.10.2016
General digitale I/O
11.10.2016
Prof. Dr. U.G. Schaarschmidt
13
Wieso Datenrichtungsregister? Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in ”Register Description” on page 90 [doc8059.pdf], the DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address. The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin. If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running. If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero). 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
14
7
Embedded Systems 1
11.10.2016
PullUp – was ist das nun wieder? MCUCR – MCU Control Register Note: 1. Only available in the Atmel ATmega164PA/324PA/644PA/1284P. • Bit 4 – PUD: Pull-up Disable When this bit is written to one, the pull-ups in the I/O ports are disabled even if the DDxn and PORTxn Registers are configured to enable the pull-ups ({DDxn, PORTxn} = 0b01). See ”Configuring the Pin” on page 73 for more details about this feature.
11.10.2016
Prof. Dr. U.G. Schaarschmidt
15
Und was ist mit „Low Active“? Heißt: Eine logische „1“ an einem Portbit (z.B. PORTA3) ausgegeben erzeugt auf dem STK500 eine dunkle LED. Eine logische „0“ an einem Portbit (z.B. PORTA3) ausgegeben erzeugt auf dem STK500 eine leuchtende LED. Dies ist für „belastete“ Ausgänge eine übliche Schaltmethode, da die Ausgangsschaltung mehr Strom gegen Masse abführen kann als Strom zu liefern. 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
16
8
Embedded Systems 1
11.10.2016
Verschieden Register, nur um einen Port zu beschicken Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in ”Register Description” on page 90, the DDxn bits are accessed at the DDRx I/O address, the PORTxn bitsat the PORTx I/O address, and the PINxn bits at the PINx I/O address. The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one,Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin. If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running. If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).
11.10.2016
Prof. Dr. U.G. Schaarschmidt
17
Und diese Ports muss man für eine Aus- oder Eingabe bearbeiten …
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
18
9
Embedded Systems 1
11.10.2016
Nicht alle (aber fast alle) Befehle funktionieren auf allen Registern All the register operating instructions in the instruction set have direct and single-cycle access to all registers. The only exception are the five constant arithmetic and logic instructions SBCI, SUBI, CPI, ANDI and ORI between a constant and a register and the LDI instruction for load immediate constant data. These instructions apply to the second half of the registers in the register file (R16..R31). The general SBC, SUB, CP, AND and OR and all other operations between two registers or on a single register apply to the entire register file. 11.10.2016
Prof. Dr. U.G. Schaarschmidt
19
Register als doppelt lange Indesxregister nutzen The registers R26..R31 have some added functions to their general-purpose usage. These registers are address pointers for indirect addressing of the Data Space. The three indirect address registers X, Y, and Z are defined as:
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
20
10
Embedded Systems 1
11.10.2016
Die Pointer- (Zeiger-) Register X, Y und Z
Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
21
Register und SRAM The AT90S8515 contains 8K bytes On-chip InSystem Programmable Flash memory for program storage. Since all instructions are 16or 32-bit words, the Flash is organized as 4K x 16. The Flash memory has an endurance of at least 1000 write/erase cycles. The AT90S8515 Program Counter (PC) is 12 bits wide, thus addressing the 4096 program memory addresses. 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
22
11
Embedded Systems 1
11.10.2016
Speicheraufteilung The lower 608 data memory locations address the Register file, the I/O memory and the internal data SRAM. The first 96 locations address the Register file + I/O memory, and the next 512 locations address the internal data SRAM. An optional external data SRAM can be placed in the same SRAM memory space. This SRAM will occupy the location following the internal SRAM and up to as much as 64K - 1, depending on SRAM size. 11.10.2016
Prof. Dr. U.G. Schaarschmidt
23
Internes und externes SRAM Accessing external SRAM takes one additional clock cycle per byte compared to access of the internal SRAM. This means that the commands LD, ST, LDS, STS, PUSH and POP take one additional clock cycle. If the stack is placed in external SRAM, interrupts, subroutine calls and returns take two clock cycles extra because the 2-byte program counter is pushed and popped. When external SRAM interface is used with wait state, two additional clock cycles is used per byte. This has the following effect: Data transfer instructions take two extra clock cycles, whereas interrupt, subroutine calls and returns will need four clock cycles more than specified in the instruction set manual. 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
24
12
Embedded Systems 1
11.10.2016
Organisation des SRAM
11.10.2016
Prof. Dr. U.G. Schaarschmidt
25
Programm- und Daten – Adressierungsarten 1
OP = Operationscode-Anteil des Befehlswortes, der Operand befindet sich in Register d
Quelle: AT8535.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
26
13
Embedded Systems 1
11.10.2016
Programm- und Daten – Adressierungsarten 2
OP = Operationscode-Anteil des Befehlswortes, die Operanden befinden sich in den Registern Rr und Rd, Das Ergebnis wir in Register Rd abgelegt. Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
27
Programm- und Daten – Adressierungsarten 3
OP = Operationscode-Anteil des Befehlswortes, n bezeichnet das Register mit dem Byte, das auszugeben ist, p sind die 6 Bits des Befehlswortes, die den I/O-Port adressieren.
Quelle: AT8535.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
28
14
Embedded Systems 1
11.10.2016
Programm- und Daten – Adressierungsarten 4
OP = Operationscode-Anteil des Befehlswortes, Die 16-Bit-Datenadresse befindet sich im unteren Wort der 2-Wort Instruktion. Rr bzw. Rd bezeichnen das Quell- oder ZielRegister. Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
29
Programm- und Daten – Adressierungsarten 5
OP = Operationscode-Anteil des Befehlswortes, Die Adresse des Operanden ist das Ergebnis der Addition des Yoder Z-Registers mit dem 6-Bit-Displacement (a) aus dem Befehlswort. Quelle: AT8535.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
30
15
Embedded Systems 1
11.10.2016
Programm- und Daten – Adressierungsarten 6
OP = Operationscode-Anteil des Befehlswortes, Die Adresse des Operanden befindet sich im X-, Y-, oder ZRegister. Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
31
Programm- und Daten – Adressierungsarten 7
OP = Operationscode-Anteil des Befehlswortes, Das X-, Y- oder Z-Register wird vor der Operation dekrementiert und Die dann als Zeiger (Pointer) auf den Operanden. Quelle: AT8535.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
32
16
Embedded Systems 1
11.10.2016
Programm- und Daten – Adressierungsarten 8
OP = Operationscode-Anteil des Befehlswortes, Das X-, Y- oder Z-Register dient als Zeiger (Pointer) auf den Operanden wird nach der Operation inkrementiert. Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
33
Programm- und Daten – Adressierungsarten 9
Adressierung einer Konstante mittels LPM-Befehl. OP = Operationscode-Anteil des Befehlswortes, Das Z-Register dient als Zeiger (Pointer) auf eine Konstante im Programmspeicher, wobei das LSB zur Selektion des oberen (LSB=1) bzw. unteren (LSB=0) Konstanten-Bytes benutzt wird.. Quelle: AT8535.PDF 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
34
17
Embedded Systems 1
11.10.2016
Programm- und Daten – Adressierungsarten 10
Indirekte Adressierung des Programmes für IJMP und ICALL. OP = Operationscode-Anteil des Befehlswortes, Das Programm wird an der Adresse fortgeführt, auf die das Z-Register (Zeiger = Pointer) zeigt. D.h. der PC wird mit dem Inhalt von Z geladen. Quelle: AT8535.PDF 11.10.2016
Prof. Dr. U.G. Schaarschmidt
35
Programm- und Daten – Adressierungsarten 11
Relative Adressierung im Programm mit den Befehlen RJMP und RCALL. OP = Operationscode - Anteil des Befehlswortes, k = relativer Anteil der Sprungadresse, die aus dem PC plus 1 gebildet wird. Das Programm wird an der Adresse PC + k +1 fortgeführt. Die relative Adresse k reicht, Dank der 12 Bits von -2048 bis + 2047. 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
Quelle: AT8535.PDF
36
18
Embedded Systems 1
11.10.2016
Miniprojekt (auf Handfolien): Toilettenlüftersteuerung Startet ca. 30 Sekunden nach dem Lichteinschalten, Läuft nach dem Lichtauschalten noch 60 Sekunden weiter. -> Zeitschleifen, -> Timer/Counter, -> T/C mit Interrupt; 11.10.2016
Prof. Dr. U.G. Schaarschmidt
37
Übungsaufgabe 2 Nachdem Sie im Praktikum 1b über die Zeitverhältnisse von Prozessor auf dem STK500 und der AVR-Studio-Simulation gestolpert sind (nachgedacht haben), überlegen Sie, wie eine Ampelschaltung (Praktikum 2) ablaufen könnte. Zeichnen Sie hierzu ein Ablaufdiagramm bzw. Nassi-Schneider-Diagramm! Schreiben Sie das Programm in C!
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
38
19
Embedded Systems 1
11.10.2016
Übung zum Praktikum 2 vorbereiten Im Praktikum Nr. 2 wird eine Strassenkreuzung (Bild nächste Folie) an das STK500 angeschlossen (weitere Details Praktikumszettel). Überlegen Sie Timerstrategien hierzu und bereiten Sie die Programmierung der Timer/Counter-Register vor! Zuerst nur die Kreuzung, dann der Fussgängerüberweg mit Anforderungstaster (mit der Hauptkreuzung synchronisieren). 11.10.2016
Prof. Dr. U.G. Schaarschmidt
39
Übungsaufgabe 2
11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
40
20
Embedded Systems 1
11.10.2016
Ampelphasen
Hauptstrecke
Nebenstrecke
11.10.2016
Prof. Dr. U.G. Schaarschmidt
41
Übungsaufgabe Welche Funktionen / Definitionen der io.h und der Stdint.h nutzen Sie für die Ampelsteuerung? Bereiten Sie die Ansteuerung der A-D-WandlerProgrammierung vor. Suchen Sie die Konfigurationsbits für das AD-Wandlerpraktikum heraus und packen diese in entsprechende (C-) Konstante, die Sie dann in Ihrem Programm einsetzen können! 11.10.2016
FH Düsseldorf
(c) U.Schaarschmidt
Prof. Dr. U.G. Schaarschmidt
42
21