The standard Arduino way of doing this is to use pinMode()
. It's not
fast, but it's easy to understand, especially for beginners. And it's
portable across architectures.
If you want to go fast, then you should skip the Arduino functions and use port manipulation, and avr-libc. The standard avr-libc way of doing what you want is:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
This compiles to two assembly instructions: one for loading a CPU register with the constant, and the other to copy the register to the DDRF IO port. Notice that you set the bits for the pins you want to set as outputs, and you clear the bits for the inputs. By default, all the pins start as inputs.
The _BV()
here is not a function call: it's a macro that expands to
(1 << (x))
. The way you write the expression to the right of the =
sign is irrelevant as long as it is recognised to be a compile-time
constant. If you want to see for yourself, you can disassemble the
generated binary with the command
avr-objdump -S -C my_prog.elf > my_prog.lst
You get an assembly listing interspersed with your source. Doing this on the example above yields:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
100: 87 e0 ldi r24, 0x07 ; 7
102: 80 bb out 0x10, r24 ; 16
Here, the ldi
instruction means "load immediate", i.e. load a register
(r24) with a constant value (0x07). The "out" instruction copies that
register into port 0x10 (namely DDRF). Both are single cycle
instructions, so the whole sequence takes two CPU cycles.
The standard Arduino way of doing this is to use pinMode()
. It's not
fast, but it's easy to understand, especially for beginners. And it's
portable across architectures.
If you want to go fast, then you should skip the Arduino functions and use port manipulation, and avr-libc. The standard avr-libc way of doing what you want is:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
This compiles to two assembly instructions: one for loading a CPU register with the constant, and the other to copy the register to the DDRF IO port. Notice that you set the bits for the pins you want to set as outputs, and you clear the bits for the inputs. By default, all the pins start as inputs.
The _BV()
here is not a function call: it's a macro that expands to
(1 << (x))
. The way you write the expression to the right of the =
sign is irrelevant as long as it is recognised to be a compile-time
constant.
The standard Arduino way of doing this is to use pinMode()
. It's not
fast, but it's easy to understand, especially for beginners. And it's
portable across architectures.
If you want to go fast, then you should skip the Arduino functions and use port manipulation, and avr-libc. The standard avr-libc way of doing what you want is:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
This compiles to two assembly instructions: one for loading a CPU register with the constant, and the other to copy the register to the DDRF IO port. Notice that you set the bits for the pins you want to set as outputs, and you clear the bits for the inputs. By default, all the pins start as inputs.
The _BV()
here is not a function call: it's a macro that expands to
(1 << (x))
. The way you write the expression to the right of the =
sign is irrelevant as long as it is recognised to be a compile-time
constant. If you want to see for yourself, you can disassemble the
generated binary with the command
avr-objdump -S -C my_prog.elf > my_prog.lst
You get an assembly listing interspersed with your source. Doing this on the example above yields:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
100: 87 e0 ldi r24, 0x07 ; 7
102: 80 bb out 0x10, r24 ; 16
Here, the ldi
instruction means "load immediate", i.e. load a register
(r24) with a constant value (0x07). The "out" instruction copies that
register into port 0x10 (namely DDRF). Both are single cycle
instructions, so the whole sequence takes two CPU cycles.
The standard Arduino way of doing this is to use pinMode()
. It's not
fast, but it's easy to understand, especially for beginners. And it's
portable across architectures.
If you want to go fast, then you should skip the Arduino functions and use port manipulation, and avr-libc. The standard avr-libc way of doing what you want is:
DDRF = _BV(PF2) // set PF2 as output
| _BV(PF1) // also PF1
| _BV(PF0); // and PF0
This compiles to two assembly instructions: one for loading a CPU register with the constant, and the other to copy the register to the DDRF IO port. Notice that you set the bits for the pins you want to set as outputs, and you clear the bits for the inputs. By default, all the pins start as inputs.
The _BV()
here is not a function call: it's a macro that expands to
(1 << (x))
. The way you write the expression to the right of the =
sign is irrelevant as long as it is recognised to be a compile-time
constant.