I/O pin

Nastavení I/O pinů

28. 3. 2013

Overview

Each port pin consists of three register bits: DDxn, PORTxn, and PINxn.

Registers:

DDRx … Data Direction Register

  • určuje který pin je vstupní a který výstupní (0 = vstup, 1 = výstup)
  • defaultně = 0

PORTx … Data Register

  • přímo nastavuje hodnotu na daném pinu (pokud je pin nastaven jako vstupní, aktivuje pull up resistor)
  • defaultně = 0

PINx … Port Input Pins (read only)

  • přečte hodnotu na daném pinu (vstup)
    // nastaveni pinu (5 na portu D)
    PORTD &= ~(1<<PD5);     // na 0
    PORTD |= (1<<PD5);      // na 1

    // Pin B0 jako výstupní
    DDRB |= (1<<DDB0);

    // PortC jako vstupni
    DDRC = 0;

    // pin 3, 4, 5 portu D jako vystupni
    DDRD |= (1<<DDD3)|(1<<DDD4)|(1<<DDD5);

      //PORTC = 1; nevim co udela

    /* Define pull-ups and set outputs high */
    PORTB = (1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0);

    /* Define directions for port pins */
    DDRB = (1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0);



    /* Read port pins */
    unsigned char i;
    i = PINB;

    /* Test pinu 5 na portu C */
    if (PINC & (1<<PINC5))
    {

}

c:\Programs\wi­navr\avr\inclu­de\avr\iomx8.h

/* I/O registers */

/* Port B */

#define PINB    _SFR_IO8 (0x03)
/* PINB */
#define PINB7   7
#define PINB6   6
#define PINB5   5
#define PINB4   4
#define PINB3   3
#define PINB2   2
#define PINB1   1
#define PINB0   0

#define DDRB    _SFR_IO8 (0x04)
/* DDRB */
#define DDB7    7
#define DDB6    6
#define DDB5    5
#define DDB4    4
#define DDB3    3
#define DDB2    2
#define DDB1    1
#define DDB0    0

#define PORTB   _SFR_IO8 (0x05)
/* PORTB */
#define PB7     7
#define PB6     6
#define PB5     5
#define PB4     4
#define PB3     3
#define PB2     2
#define PB1     1
#define PB0     0

/* Port C */

#define PINC    _SFR_IO8 (0x06)
/* PINC */
#define PINC6   6
#define PINC5   5
#define PINC4   4
#define PINC3   3
#define PINC2   2
#define PINC1   1
#define PINC0   0

#define DDRC    _SFR_IO8 (0x07)
/* DDRC */
#define DDC6    6
#define DDC5    5
#define DDC4    4
#define DDC3    3
#define DDC2    2
#define DDC1    1
#define DDC0    0

#define PORTC   _SFR_IO8 (0x08)
/* PORTC */
#define PC6     6
#define PC5     5
#define PC4     4
#define PC3     3
#define PC2     2
#define PC1     1
#define PC0     0

/* Port D */

#define PIND    _SFR_IO8 (0x09)
/* PIND */
#define PIND7   7
#define PIND6   6
#define PIND5   5
#define PIND4   4
#define PIND3   3
#define PIND2   2
#define PIND1   1
#define PIND0   0

#define DDRD    _SFR_IO8 (0x0A)
/* DDRD */
#define DDD7    7
#define DDD6    6
#define DDD5    5
#define DDD4    4
#define DDD3    3
#define DDD2    2
#define DDD1    1
#define DDD0    0

#define PORTD   _SFR_IO8 (0x0B)
/* PORTD */
#define PD7     7
#define PD6     6
#define PD5     5
#define PD4     4
#define PD3     3
#define PD2     2
#define PD1     1

#define PD0     0