DigitalOut object does not retain its output value

26 Jun 2012

I find that my DigitalOut instance is not retaining its output level. My codes looks like this:

//Global variable
DigitalOut port5(p51);

void f1(int val){
   port5 = val;
}

void main(int){
   f1(1);
   //do other stuff
}

What I find is that port5 doesn't stay high. Why is that? My code is actually more complicated but basically does the same thing.

If I place a while(1) as the last line in the f1 function, however, port5 remains high.

Any thoughts on why this would happen?

Edit: This seems to happen even if I don't use DigitalOut and write to the GPIO direction and in/out registers directly. Once the function terminates, the output levels no longer stick.

26 Jun 2012

Hi ds s, This is an interesting observation and I think answers a question I have always wondered about! i.e. what happens when a program execution runs out of code?

I think you have found out!

Your while(1) at the end of f1 keeps the code executing.

if you put while(1) at the end of the program before the closing brace, I assume this would also work correctly?