4
Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D ) DDRx Data Direction Register: Legt für jeden Pin i fest, ob er als Eingang (Bit i =0) oder als Ausgang (Bit i =1) verwendet wird. 7 6 5 4 3 2 1 0 DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0 R/W R/W R/W R/W R/W R/W R/W R/W PORTx Data Register: Ist Pin i als Ausgang konfiguriert, so legt Bit i den Pegel fest (0=GND sink, 1=Vcc source). Ist Pin i als Eingang konfiguriert, so aktiviert Bit i den internen Pull-Up-Widerstand (1=aktiv). 7 6 5 4 3 2 1 0 PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 R/W R/W R/W R/W R/W R/W R/W R/W PINx Input Register: Bit i repräsentiert den Pegel an Pin i (1=high, 0=low), unabhängig von der Konfiguration als Ein-/Ausgang. 7 6 5 4 3 2 1 0 PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 R R R R R R R R Verwendungsbeispiele: 3–5 und 3–8 [1, S. 66] c dl GSPiC (Teil C, SS 11) 14 μC-Systemarchitektur | 14.5 Ports 14–14

Beispiel ATmega32: Port-Register - FAU · Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D) DDRx Data Direction Register: Legt für jeden

Embed Size (px)

Citation preview

Page 1: Beispiel ATmega32: Port-Register - FAU · Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D) DDRx Data Direction Register: Legt für jeden

Beispiel ATmega32: Port-Register

Pro Port x sind drei Register definiert (Beispiel für x = D)

DDRx Data Direction Register: Legt für jeden Pin i fest, ob er als Eingang(Bit i=0) oder als Ausgang (Bit i=1) verwendet wird.

678155B–AVR–07/09

ATmega32A

12.4.5 PORTB – Port B Data Register

12.4.6 DDRB – Port B Data Direction Register

12.4.7 PINB – Port B Input Pins Address

12.4.8 PORTC – Port C Data Register

12.4.9 DDRC – Port C Data Direction Register

12.4.10 PINC – Port C Input Pins Address

12.4.11 PORTD – Port D Data Register

12.4.12 DDRD – Port D Data Direction Register

Bit 7 6 5 4 3 2 1 0

PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 PORTB

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0 DDRB

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 PINB

Read/Write R R R R R R R R

Initial Value N/A N/A N/A N/A N/A N/A N/A N/A

Bit 7 6 5 4 3 2 1 0

PORTC7 PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0 PORTC

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDC7 DDC6 DDC5 DDC4 DDC3 DDC2 DDC1 DDC0 DDRC

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

PINC7 PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0 PINC

Read/Write R R R R R R R R

Initial Value N/A N/A N/A N/A N/A N/A N/A N/A

Bit 7 6 5 4 3 2 1 0

PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 PORTD

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0 DDRD

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

PORTx Data Register: Ist Pin i als Ausgang konfiguriert, so legt Bit i den Pegelfest (0=GND sink, 1=Vcc source). Ist Pin i als Eingang konfiguriert, soaktiviert Bit i den internen Pull-Up-Widerstand (1=aktiv).

678155B–AVR–07/09

ATmega32A

12.4.5 PORTB – Port B Data Register

12.4.6 DDRB – Port B Data Direction Register

12.4.7 PINB – Port B Input Pins Address

12.4.8 PORTC – Port C Data Register

12.4.9 DDRC – Port C Data Direction Register

12.4.10 PINC – Port C Input Pins Address

12.4.11 PORTD – Port D Data Register

12.4.12 DDRD – Port D Data Direction Register

Bit 7 6 5 4 3 2 1 0

PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 PORTB

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0 DDRB

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 PINB

Read/Write R R R R R R R R

Initial Value N/A N/A N/A N/A N/A N/A N/A N/A

Bit 7 6 5 4 3 2 1 0

PORTC7 PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0 PORTC

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDC7 DDC6 DDC5 DDC4 DDC3 DDC2 DDC1 DDC0 DDRC

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

PINC7 PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0 PINC

Read/Write R R R R R R R R

Initial Value N/A N/A N/A N/A N/A N/A N/A N/A

Bit 7 6 5 4 3 2 1 0

PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 PORTD

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0

DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0 DDRD

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W

Initial Value 0 0 0 0 0 0 0 0

PINx Input Register: Bit i repräsentiert den Pegel an Pin i (1=high, 0=low),unabhängig von der Konfiguration als Ein-/Ausgang.

688155B–AVR–07/09

ATmega32A

12.4.13 PIND – Port D Input Pins Address

Bit 7 6 5 4 3 2 1 0

PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 PIND

Read/Write R R R R R R R R

Initial Value N/A N/A N/A N/A N/A N/A N/A N/AVerwendungsbeispiele: �→ 3–5 und �→ 3–8 [1, S. 66]

c� dl GSPiC (Teil C, SS 11) 14 µC-Systemarchitektur | 14.5 Ports 14–14

Page 2: Beispiel ATmega32: Port-Register - FAU · Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D) DDRx Data Direction Register: Legt für jeden

Peripheriegeräte – Register (Forts.)

Memory-mapped Register ermöglichen einen komfortablen ZugriffRegister �→ Speicher �→ VariableAlle C-Operatoren stehen direkt zur Verfügung (z. B. PORTD++)

Syntaktisch wird der Zugriff oft durch Makros erleichtert:

#define PORTD ( * (volatile uint8_t*)( 0x12����Adresse: int

)

� �� �Adresse: volatile uint8_t* (Cast �→ 7–17 )

� �� �Wert: volatile uint8_t (Dereferenzierung �→ 13–4 )

) PORTD ist damit(syntaktisch) äqui-valent zu einervolatile uint8_t-Variablen, die anAdresse 0x12 liegt

Beispiel

#define PORTD (*(volatile uint8_t*)(0x12))

PORTD |= (1<<7); // set D.7uint8_t *pReg = &PORTD; // get pointer to PORTD*pReg &= ~(1<<7); // use pointer to clear D.7

c� dl GSPiC (Teil C, SS 11) 14 µC-Systemarchitektur | 14.3 Peripherie 14–8

Page 3: Beispiel ATmega32: Port-Register - FAU · Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D) DDRx Data Direction Register: Legt für jeden

Das erste C-Programm für einen µ-Controller

„Hello World“ für AVR-ATmega (SPiCboard)

#include <avr/io.h>

void main() {// initialize hardware: LED on port D pin 7, active lowDDRD |= (1<<7); // PD7 is used as outputPORTD |= (1<<7); // PD7: high --> LED is off

// greet userPORTD &= ~(1<<7); // PD7: low --> LED is on

// wait foreverwhile(1){}

}

Übersetzen und Flashen (mit AVR Studio) � Übung

Ausführen (SPiCboard): (rote LED leuchtet)

c� dl GSPiC (Teil B, SS 11) 3 Java versus C – Erste Beispiele | 3.1 Ausgabe 3–4

Page 4: Beispiel ATmega32: Port-Register - FAU · Beispiel ATmega32: Port-Register Pro Port x sind drei Register definiert (Beispiel für x = D) DDRx Data Direction Register: Legt für jeden

Übungsplattform: Das SPiCboard

ATmega32-µCJTAG-Anschluss8 LEDs2 7-Seg-Elemente2 Taster1 Potentiometer1 Fotosensor

LED 7 (Blue 1, PA5)

LED 6 (Green 1, PA6)

LED 5 (Yellow 1, PA7)

LED 4 (Red 1, PC7)

LED 3 (Blue 0, PC6)

LED 2 (Green 0, PC1)

LED 1 (Yellow 0, PC0)

LED 0 (Red 0, PD7)

LED!Reihe (active low)

(entp

rellt

, PD2)

Taste

r 0

(pre

llt, P

D3)

Taste

r 1

PB0

PB4

PB5

PB6

PB1

PB3

PB2

Potentiometer (POTI) an ADC1

Fotowiderstand

an ADC0

ISP!Anschluss

JTAG!Anschluss

Stromversorgung

Zehner (Connector PD0)

Einer (Connector PD1)

(active low)

7!Segment!Anzeigen

Ausleihe zur Übungsbearbeitung möglichOder noch besser �→ selber Löten

c� dl GSPiC (Teil A, SS 11) 2 Organisation | 2.2 Übung 2–4