Go Down

Topic: [Solved] Are pins forced to LOW after constructor calls? (Read 887 times) previous topic - next topic

olikraus

Feb 22, 2013, 10:37 pm Last Edit: Feb 23, 2013, 12:16 am by olikraus Reason: 1
On my Due i have the following code (Arduino IDE 1.5.2). It should force pin 8 to HIGH, but pin 8 will be LOW instead.
Code: [Select]

// THIS DOES NOT WORK ON THE DUE, PIN 8 WILL BE LOW
class pin {
 public:
   pin() {
     pinMode(8, OUTPUT);
     digitalWrite(8, HIGH);
   }
   int z;
};

class pin x;

void setup() {
 x.z++;
}
void loop() {
}


If I put the constructor call into the setup(), it will work. So this is fine and pin 8 will be HIGH:
Code: [Select]

class pin {
 public:
   pin() {
     pinMode(8, OUTPUT);
     digitalWrite(8, HIGH);
   }
   int z;
};

void setup() {
 class pin x;
 x.z++;
}
void loop() {
}


On the Uno, both examples produce HIGH at pin 8.

Any ideas? Did I miss something? I had looked into this forum, but did not see any thead about this topic.

Unfortunately this currently breaks my library: During the constructor call a do a low active reset on an external device. Of course i expect, that the output pin stays HIGH within setup() and loop(). But this is not true and my external device will be put into reset again.

Oliver

AWOL

In your first example, your constructor runs before "init()".
Take a look at the source of "init()".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

olikraus

hmmm good hint. So here is the main loop (objdump):
Code: [Select]
int main( void )
{
   802a4: b508      push {r3, lr}
init();
   802a6: f000 fc83 bl 80bb0 <init>

delay(1);
   802aa: 2001      movs r0, #1
   802ac: f000 fd76 bl 80d9c <delay>

#if defined(USBCON)
USBDevice.attach();
   802b0: 4806      ldr r0, [pc, #24] ; (802cc <main+0x28>)
   802b2: f000 f849 bl 80348 <_ZN10USBDevice_6attachEv>
#endif

setup();
   802b6: f7ff ff35 bl 80124 <setup>

for (;;)
{
loop();
   802ba: f7ff ff3b bl 80134 <loop>
if (serialEventRun) serialEventRun();


And, yes, fully correct, the "init()" code includes this:
Code: [Select]
// Disable pull-up on every pin
  for (int i = 0; i < PINS_COUNT; i++)
   80bea: e004      b.n 80bf6 <init+0x46>
  digitalWrite(i, LOW);


Thanks AWOL. Good point.
I expect that this will break many existing libs...

Oliver

AWOL

Quote
I expect that this will break many existing libs
which is why many libraries have a "begin" method which must be explicitly called.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

olikraus

hmmm... I never had seen this relationship. Thanks for clarification. Karma+

Oliver

Go Up