chipKIT® Development Platform

Inspired by Arduino™

digitalWrite / pinMode

Created Mon, 24 Feb 2014 03:00:03 +0000 by Jacob Christ

Jacob Christ

Mon, 24 Feb 2014 03:00:03 +0000

I noticed tonight that if you issue a digital write before a pin mode command that the stated of the pin is not necessarily what you set it to before issuing the pin mode command:

  digitalWrite(pin, LOW);   // set the LED off
  pinMode(pin, OUTPUT);     

Typically, I like to do this so that the state of the pin is known before setting the TRIS register to an output.

Is this intended operation? Is this a throw back to Arduino?



Mon, 24 Feb 2014 08:56:33 +0000

This is intended.

A digitalWrite when in INPUT mode, which is the default, sets the state if the pull-up resistor (if available) and not the value of the output pin.

It is part of the Arduino API specification since the dawn of time.

Jacob Christ

Mon, 24 Feb 2014 09:34:30 +0000

Is there wiring abstraction way to do what I am trying to achieve?

If not how about adding a digitalWrite (pin, PRE_HIGH) to fix the issue?



Mon, 24 Feb 2014 10:06:57 +0000

There is no defined way of doing it, no. It is something I have addressed in my experimental OO core, but that's a few years away from release yet ;)

Adding a third, optional, parameter to pinMode() would be a good way to go:

pinMode(4, OUTPUT, LOW);

... where that would first use the same code as digitalWrite() to set the LAT for the pin to the desired level, and then set the TRIS to output.

It's non-standard, obviously, but it expands on the existing spec keeping backwards compatibility.

Jacob Christ

Mon, 24 Feb 2014 15:29:44 +0000

I like your suggestion better. Maybe we should do a pull request on the Arduino side of things.