0

It's my understanding that the following lines are equivalent. They seem to be interchangeable in my sketch:

DDRB = 1<<DDB0;

and

pinMode(0, OUTPUT);

Is the first form (DDRB...) setting a micro-controller address directly? Is that assembler?

And is the second form (pinMode) using an included library? Maybe the avr/io.h library?

On a Digispark ATTiny85.

  • 2
    They are not interchangeable. DDRB = 1<<DDB0; sets PB0 to output, and it also sets every other I/O pin to input. It's not assembler, no. In assembler you would write sbi _SFR_IO_ADDR(DDRB), 0. – Edgar Bonet Jan 2 '18 at 14:07
  • 1
    DDRB |= 1<<DDB0; would be equivalent to pinMode(0, OUTPUT);. The latter has the side-effect of being slower though. – Gerben Jan 2 '18 at 19:16
  • @EdgarBonet if not assembler, what is it? C? Is it really the same language as pinMode(), just setting lower-level bits directly? thx – johny why Jan 3 '18 at 1:44
  • 1
    It is C (or C++, they are the same for this statement). The pinMode() form invokes a library function which does the same thing as the other form. – Russell Borogove Jan 3 '18 at 4:54
1

Yes, on a simple microcontroller like the ATTiny the two lines of code are equivalent. The function pinMode(...) just looks up what pins relate to what ports in a table and set the right registers.

Using DDRB instead of pinMode(...) is fine as long as you are never going to run your code on anything else (or give it to anyone else to run), since you have now hard coded into your sketch what pins are to be used. It is more efficient to use DDRB though. You sacrifice portability for efficiency.

However, for more complex microcontrollers that have many many functions on a single pin, just manipulating DDRB (or the equivalent on the target microcontroller) may not be enough. You may need to switch the pin into GPIO mode, disable analog inputs on the pin, or other operations as well. All that will be wrapped up in the pinMode(...) function for you by the Arduino API so you don't need to worry about it.

So, in short, if you don't care about portability, and you do care about efficiency and code size (which may well be the case on an ATTiny) then yes use DDRB and its friends.

  • i understand that analogWrite() eliminates the need to enable output-mode completely -- neither DDRB nor pinMode() statements are needed. Is analogWrite() even less efficient than pinMode() + analogWrite()? -thx – johny why Jan 3 '18 at 1:48
  • 1
    The pinMode is redundant. PWM is a special mode that is not controlled by GPIO but a signal from a timer. – Majenko Jan 3 '18 at 1:50
  • "as long as you are never going to run your code on anything else" --I assume the DDRB syntax is valid at least for the entire ATTiny line, if not beyond. No? – johny why Jan 3 '18 at 5:29
  • 1
    @johnywhy: It even works on the ATmegas. Note, however, that for setting pin 0 to output on an Arduino Uno you would use DDRD instead of DDRB, because pin 0 on the Uno is on port D rather than port B. With the pinMode() function you use the pin numbers that are printed on the silkscreen of the board, and you don't have to worry about the pin to port mapping, because the function takes care of this for you. You sacrifice efficiency for convenience. – Edgar Bonet Jan 3 '18 at 9:58
  • 1
    @johnywhy "anything else" even includes the same chip in a different board with different pins being needed to be manipulated. If you need to use pin B3 instead of pin B0 on a different board with the same chip you will have to change your code. Using pinMode, as long as both pins are numbered the same in the IDE, there will be nothing you need to change. – Majenko Jan 3 '18 at 15:05
1

DDRB

is a register. Exactly: Port B Data Direction Register. See datasheet for ATtiny, chapter 10.4. Register Description and 10.2.1 Configuring the Pin. This part will explain why

DDB0

value was used. Using this way of setting direction of a pin is much faster than pinMode(0, OUTPUT);
The possible problem is when you want to use same code to program different Arduino. In this case you have to remember to check all direct registers operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.