Go Down

Topic: Double Interrupt on PPS (Read 6157 times) previous topic - next topic

bastukee

Also, correct me if I'm mistaken, wouldn't another if statement inside of the for loop add greater overhead?
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

graynomad

#16
Jan 07, 2011, 10:25 am Last Edit: Jan 07, 2011, 10:32 am by graynomad Reason: 1
Volatile basically means that "things" may change the variable in a manner that's not obvious when the compiler looks at the code. Such as say a input register that changes state when inputs change. To quote from the Netrino site

Quote
C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.


Frankly I don't know why it should be required in your exact code because the compiler should know what's going on, and as it's working maybe that is in fact the case. Still, it's good practice to use the qualifier for vars shared with ISRs.

One place this will bite you is with optimisation

Code: [Select]
loop () {
  long i;

  for (i = 0; i < 100; i++ ) {
      i = i;
  }

  pulse_pin ();

}

In this simple loop I am just trying to output some pulses to see some timing for whatever reason and I want to vary the loop time by changing the 100. But no matter what I change it to, even 1,000,000 the frequency of the pulse stays the same.

The compiler has decided that the loop actually doesn't do anything and has removed it from the run time code. Changing it to

volatile long i;

fixes the problem by causing the compiler to butt out.

Quote
wouldn't another if statement inside of the for loop add greater overhead?

Yes but there's plenty of fat because of the delay, so adjust the

delayMicroseconds(1931);

to accomodate the extra code in the loop.



______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

bastukee

Thanks for the tips, with your code and eliminating some statements I found how to run my code even smaller and smoother than before.

Code: [Select]
int pinout = 12;

void setup()
{
 pinMode(pinout, OUTPUT);
 pinMode(2, INPUT);
 attachInterrupt(0, pulsePPS, RISING);
}

void loop(){
}

void pulsePPS() //interrupt routine
{
 delayMicroseconds(35);
 for (int i = 0; i < 50 ; i++) { //create 49 pulses
   digitalWrite(pinout, HIGH);
   delayMicroseconds(50); //50us pulse
   digitalWrite(pinout, LOW);
  if (i < 49) { //If statement removes delay on last pulse
     delayMicroseconds(1943); // tweak this for correct pulse spacing
  }
 }
}

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Go Up