avr_basics.md 2.2 KB


title: AVR Basics date: 2021-01-13 categories: [cheatsheets, embedded]

tags: [avr, reversing, basics]

AVR Basics

Port Operation Registers

  • DDRx = Data Direction Register
  • PORTx = Pin Output Register
  • PINx = Pin Input Register

DDRx - Data Direction Register

This register is used to tell if a output PIN is used as input or output.

  • 1 = Output
  • 0 = Input

The register looks like this

| Bit No. | 7    | 6    | 5    | 4    | 3    | 2    | 1    | 0    |
| Name    | DDx7 | DDx6 | DDx5 | DDx4 | DDx3 | DDx2 | DDx1 | DDx0 |
| Init.   | 0    | 0    | 0    | 0    | 0    | 0    | 0    | 0    |

The register can be set like this:

// E.g. for Port C
DDRC = (1<<DDC0)|(1<<DDC4)|(1<<DDC5)|(1<<DDC7);

// the same as
DDRC = 0x1B;

// in Binary: 0b10110001

That means:

  • PC7 = Output(1)
  • PC6 = Input(0)
  • PC5 = Output(1)
  • PC4 = Output(1)
  • PC3 = Input(0)
  • PC2 = Input(0)
  • PC1 = Input(0)
  • PC0 = Output(1)

PINs marked as Input Pins have the capability to read the voltage-level at that PIN. In contrast, PINs marked as Output Pins can be set to either HIGH or LOW.

PORTx - Pin Output Register

This Register determines whether the output of the corresponding output pins should be HIGH or LOW.

The register looks like this

| Bit No. | 7    | 6    | 5    | 4    | 3    | 2    | 1    | 0    |
| Name    | Px7  | Px6  | Px5  | Px4  | Px3  | Px2  | Px1  | Px0  |
| Init.   | 0    | 0    | 0    | 0    | 0    | 0    | 0    | 0    |
  • Set the PINS:

    PORTC = (1<<PD0)|(1<<PD3)|(1<<PD6);
    

    Info: PORTx can only modify the PINS that are marked as output pins with DDRx.

    PINx - Pin Input Register

    In contrast to the PORTx register, this register is used to read the state of the corresponding pin x.

    ```

| Bit No. | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | Name | Px7 | Px6 | Px5 | Px4 | Px3 | Px2 | Px1 | Px0 | | Init. | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |


To read the PINS state:

```C
if (PINC == 0b01000000)
    PORTC = 0x0B;
else
    PORTC = 0x00

Info: As already noted aboved, PINx can only read the PINS that are marked as input pins with DDRx.