This is an old revision of the document!


Digitale Inputs/Outputs

Alle Busse am AVR sind sowohl lesbar als auch schreibbar, wenn sie im normalen logischen Input/Output (I/O) Modus verwendet werden. AVR Busse werden mit Buchstaben des lateinischen Alphabets benannt: A,B,C usw.. Einige AVR haben keine Bus A auch wenn es einen Bus B gibt. Jeder Bus ist ein 8-Bit Bus und jeder Bit hat normalerweise seinen eigenen Pin am Controller. Pins werden mit Nummern gezählt und fangen bei Null an. Für beide Richtungen im Bus gibt es ein seperates Register. Zusätzlich gibt es noch ein Register der die tatsächliche Richtung des Bus entscheidet, ein Bitwert von 1 markiert den Bus als Output und eine 0 als Input. Damit hat der Bus drei Register:

  • PORT - um den Outputwert des Bus zu setzen.
  • PIN - um den Input des Bus zu lesen
  • DDR - Um die Richtung zu setzen.

Example

Aufgabe: Mache die Pins 0-3 an Bus B zum Input, die Pins 4-7 zum Output, setze Pin 5 “high” und lies die Werte der Pins 0-3 zu einer Variable. Der C-Code sieht wie folgt aus:

#include <avr/io.h>
 
int main()
{
	unsigned char x;
 
	// Pins 0-3 als Input, 4-7 als Output
	DDRB = 0xF0;
 
	// Setzt Pin 5 "high"
	PORTB |= (1 << PIN5);
 
	// Liest den Wert der Pins 0-3
	x = PINB & 0x0F;
}

In this example, the inputs are used in Hi-Z (high impedance) mode. In essence, the input does not put virtually any load on the source of the signal. This mode might be necessary, if the pin is used as a data bus. It is wise to use a pull-up resistor on the input, if the pin is used for a button, a switch or any other solution, where the input is connected to ground. For that, the output bit of the corresponding pin must be set high in the input mode - as a result, a resistor is placed between the supply voltage and the input, which keeps the input voltage high unless it is being pulled down by something. The goal of a pull-up resistor is to prevent floating of the input due to statical electricity and other interferences. After booting the controller, all IO buses are in the high impedance input mode by default.

Normalerweise werden die Pins am IO Bus auch für andere Peripherals genutzt, neben den logischen Anschlüssen. Falls man eine alternative Funktion des Pins nutzen will, kann man den IO Pin Mode im AVR Datasheet finden. Zum Beispiel wenn man ein ADC Kanal als Input nutzen will, sollte der Pin im Input Modus sein, und um ein PWM Signal zu generieren, sollte er im Output Mode sein. Auf der anderen Seite, suchen sich einige Peripherals den IO Pin Modus selbst aus.

de/avr/io.1279816340.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0