I have a few questions about the Arduino library functions for setting digital I/O pin mode and writing values.
The function pinMode(pin, INPUT or OUTPUT) sets whether a pin is an input or output. Is there a corresponding way to read what the pinMode is for a given pin?
If I try to use digitalWrite() to a pin that has been configured as an input, does anything bad happen or is it just ignored?
The function digitalWrite() sets one pin to either HIGH or LOW. Is there a similar function to write all the pin values at once, or perhaps 8 at a time?
Thanks,
Mike Marks
You could, but if you think you need to do so in your code, I'd suggest rethinking/rewriting your program. If you manage to leave pins in a state that you're not sure about, this means you're not really in control of what you're doing.
It activates the internal pullup resistor.
Yes. Write 0xff to PORTC, PORTD etc. Again, the question is why you'd need this. The only conceivable scenario would be that the delay of 62ns (assuming an ATMega328p running at 16MHz) between toggling individual pins through direct port manipulation is too long of a delay. If that's the case, I'm very interested in hearing about what kind of project you're working on.
There is by reading the port direction register (assuming an AVR architecture, others may refer to it differently). But, there's no reason for doing so. Your code sets the mode, so you obviously know what it is.
To be a bit more complete, writing HIGH to an input pin activates the internal pull-up resistor and writing LOW de-activates the internal pull-up resistor.
The below two snippets achieve the same
// set pin to input, internal pull-up de-activated
pinMode(yourPin, INPUT);
// activate internal pull-up
digitalWrite(yourPin, HIGH);
// set pin to input with internal pull-up activated.
pinMode(yourPin, INPUT_PULLUP);