2.4.2.1 Direct Pin Configuration

The GPIO basic functionality is controlled using the three registers that reside in the extended I/O Register space. This space does not allow bit manipulation instructions, and the configuration update for one pin is done using the Read-Modify-Write instructions. The hardware Read-Modify-Write functionality ensures a safe and correct change of the drive values and/or input and sense configuration, but it is translated into three assembler instructions. The following code snippet shows the PA7 pin configuration as output:

C Code Example:

    PORTA.DIR |= 0x80;

Assembler Code:

    ;Load PORTA.DIR address into Z-pointer
    LDI R30, 0x00; 
    LDI R31, 0x04;
    LDI R24, Z     ;Read content of PORTA.DIR register
    ORI R24, 0x80  ;Logic or with 0x80
    ST  Z, R24     ;Store result into PORTA.DIR register  

To speed up the access and to offer more flexibility, the new AVR Dx microcontrollers offer a new subset of registers that allow the update of the GPIO configuration for multiple pins in one instruction:

Register (Note)Description
PORTx.DIRSETWriting a ‘1’ to any bit in this bit field will set the corresponding bit in PORTx.DIR, which will configure the corresponding pin as an output pin and enable the output driver
PORTx.DIRCLRWriting a ‘1’ to any bit in this bit field will clear the corresponding bit in PORTx.DIR, which will configure the corresponding pin as an input-only pin and disable the output driver
PORTx.DIRTGLWriting a ‘1’ to any bit in this bit field will toggle the corresponding bit in PORTx.DIR
PORTx.OUTSETWriting a ‘1’ to any bit in this bit field will set the corresponding bit in PORTx.OUT, which will configure the corresponding pin to be driven high
PORTx.OUTCLRWriting a ‘1’ to any bit in this bit field will clear the corresponding bit in PORTx.OUT, which will configure the corresponding pin to be driven low
PORTx.OUTTGLWriting a ‘1’ to any bit in this bit field will toggle the corresponding bit in PORTx.OUT
Note: Writing ‘0’ to any bit position in the registers has no effect.

Using the direct pin configuration feature, the code for the configuration of the PA7 pin as output becomes:

C Code:

    PORTA.DIRSET = 0x80;

Assembler Code:

    ;Load PORTA.DIRSET address into Z-pointer
    LDI R30, 0x01; 
    LDI R31, 0x04;
    LDI R24, 0x80     ;Load mask
    ST  Z, R24        ;Write to the PORTA.DIRSET register