indirtwetrust

All Outputs Go High on Startup

12 posts in this topic

I wrote a sketch to fire 2 outputs back and forth with a pot to adjust the on time of the outputs and a pot to adjust the off time.  The outputs are just LEDs for now and there is a 3rd LED that turns on when either output is on (so I can adjust the pots later when the outputs are only driving silent, solid state relays.)

Every time the MSP430G2553 is powered up or reset, in the Launchpad or a bread board, all of the pins configured as outputs go high for about a second before the program starts to run

There is a thread that comes up with a google search of the problem where everyone just accepts that this is what the MSP430 does but it seems to be something that the underlying Energia code is doing because the C code does not make this happen.  I altered the Blink source file in CCS to make the 2 onboard LEDs toggle back and forth. When the board is powered up running that code, both outputs don't go high. The LEDs just start to toggle back & forth as programmed.

Has anyone else dealt with this?  I really can't have all the outputs turn on at the same time with my project. Is there a way to write the Energia sketch to avoid this or do I have to learn how write my code in C?

I hope you can excuse my ignorance.  I just started trying to program a week ago.  I took it up to make this one project when I decided it was going to be a little too complicated to build with discrete components.

I really appreciate any help/advise.

Share this post


Link to post
Share on other sites

Hi @indirtwetrust and welcome to 43oh.

It always helps to post a simplified version of your code that demonstrates the issue so that others can replicate it.  Perhaps you did not set the pins low before setting them to outputs?  If not, I suspect the default is for Energia is to immediately set output to high.  See the code below where I set pins low first:

 

/* Test output state on powerup and reset
 * G2553 LaunchPad without crystal
 * Energia V17
 */

void setup()
{
  pinMode(PUSH2, INPUT_PULLUP);   
  digitalWrite(6, LOW); 
  pinMode(6, OUTPUT);
  digitalWrite(7, LOW);
  pinMode(7, OUTPUT);
  digitalWrite(8, LOW);
  pinMode(8, OUTPUT); 
  digitalWrite(9, LOW);
  pinMode(9, OUTPUT);
  digitalWrite(10, LOW);
  pinMode(10, OUTPUT);  
}

void loop()
{
  if (digitalRead(PUSH2) == LOW){
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  else{
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
}

The logic analyzer in the screen shot below is set to capture for 5 seconds.  When I start it, the LaunchPad is running and I am pushing the user button on P1_3, PUSH1, every second or so.  As expected when P1_3 goes low, the output pins go high.

PUSH1.thumb.JPG.8a123e6d8a66924bcf38aea927b6ef21.JPG

In the next screen shot I wait about a second into the run to plug in the LaunchPad.  As can be seen, everything is low to start and then P1_3 goes high as soon as it is reached in setup().  The rest stay low.  It takes a while to start up because I don't have the crystal installed on this LaunchPad and Energia tries for a while to start it before giving up.  The same thing if I do a reset - the output pins don't start out high.

Startup.thumb.JPG.98fc08aaee82986d28bb1d3d97a08a46.JPG

 

zeke likes this

Share this post


Link to post
Share on other sites

Ya, that was it!  Thank you so much.  I had no idea I had to set the output low before declaring it an output.

Share this post


Link to post
Share on other sites

Thanks again for your help.  I attached the Sketch I have so far.  It works as is for my original project but after adding an LCD for experimentation, I would like to re-write it so that the sensorValues update continuously instead of waiting until the delays end.  I haven't been able to figure out how to do it yet, I was looking at the multi-tasking examples but I get errors when trying to compile those sketches with multiple tabs.  Then to go 1 step further, it would be cool if the delay could be affected by turning the pot after it got the "delay sensorValue" command.

Auto Call Program.txt

Share this post


Link to post
Share on other sites

The multi-tasking examples are for the MCUs that have the resources (processing power, memory, etc) to handle the TI-RTOS overhead.  The value line series (like the 2553 you are using) are not capable of running the real time operating system.  The MSP432 is a good example of a MCU that can run the multi-tasking environment.

Share this post


Link to post
Share on other sites
if (delay == bad){        // and delay is to be avoided
	! use delay
}
  • Set up a variable that records the start of the period  - e.g. startTime =  millis()
  • Set another variable to hold the end of the period - e.g. finishTime = startTime + sensorTime
  • Keep the loop going without a delay
  • Keep reading values
  • At the top of the loop check millis() to see if finishTime has been reached, if so take appropriate action
  • Also check to see if the pot has changed more than some predetermined amount - if so, make desired changes

There are quite a few tutorials around about how to avoid using delay(), e.g. this one turned up when I googled:  http://playground.arduino.cc/Code/AvoidDelay

There are some traps to avoid when using this method - e.g. use unsigned long, overflow, etc.  The tutorials should touch on this.

Share this post


Link to post
Share on other sites

Thanks I will try that.  I have read that delay should be avoided but I also read that delay in Energia doesn't just hold up the processor. I'll start with reading the link.

Share this post


Link to post
Share on other sites

MSP430 Energia uses a lower power mode with delay() than Arduino but it still blocks other code from running.  interrupts are another possible solution. 

Share this post


Link to post
Share on other sites

This is the best I could do so far.  I start with one output high, the first delay works and the output goes off, then the other output goes on (with a flicker)  and locks up there,  how can I start and stop the timers in the code in the IF statements?  Also, i'll need to use some kind of variable to switch on and off, 1 or 0, true or false to keep the outputs alternating.  I tried this but the format would not work if ((millis()-timer1)>sensorValue1) && toggle = 0){ ...  

Is there a decent cheat sheet for structure & formatting besides the single page one Have?  I'll attach it too.

Auto Call Program.txt

energia_cheatsheetv01c.pdf

Share this post


Link to post
Share on other sites

I have always used the reference page in Energia:  http://energia.nu/reference/

Googling something like "vary time potentiometer arduino" will likely turn up code close to what you want.  I don't see where you reset the time in your code, i.e. timer1 and timer0. One section of code is repeated and I assume that is not your intention.  I would do something like this:

/*
 * Varies the flashing period of the onboard red LED using a potentiometer
 * attached to a MSP430G2553.  Tested with Energia V17.
 */
 
const int potPin = 7;  
const int minTime = 500;
const int maxTime = 10000;
unsigned long waitTime = 0;
unsigned long startTime = 0;
int ledState = HIGH;

void setup()
{
  Serial.begin(9600);
  pinMode(RED_LED, OUTPUT);
  startTime = millis();  
  digitalWrite(RED_LED, ledState);
}

void loop()
{
  waitTime = map(analogRead(potPin), 0, 1023, minTime, maxTime);
  if ((startTime + waitTime) <= millis())
  {
    Serial.println(waitTime);
    ledState = !ledState;
    startTime = millis();
    digitalWrite(RED_LED, ledState);
  }
}  

 

Share this post


Link to post
Share on other sites

Well I think I got it !   I sure appreciate your help.  A couple of things had me hung up though.  I couldn't get the if statements to recognize the state of an actual assigned output like && (relayPinUP == LOW).  I had to make variables I called stateUP / stateDN in addition to my "toggle" variable to keep track of which  output's turn it is to fire.  But then when I tried to "digitalWrite(stateDN, HIGH)" or LOW, the variable would   not change state.  I had to use the "!" to toggle it instead.  Also, not having a good understanding of how the timer worked I was trying to use a timer1 & a timer0, 1 for on time, 0 for off time but I had to reset both in every if statement.  It wasn't until it was all working that I realized I could just use 1 timer.  what is the difference between using & or && and = or ==?  it seems to work both ways.

Let me know what you think, if I could make it better or anything.  I'm going to add the LCD code to it again so I can see the values of the pots change in real time now.

Thanks again

Auto Call Program.txt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now