Jump to content
43oh

Strange millis() behaviour


Recommended Posts

Hmmm, this test sketch

 

 

// Testing fixed millis, micros and delay functions

void setup()
{
  Serial.begin(9600);
  Serial.print("us resolution measured=");
  unsigned long us = micros();
  unsigned long us2 = micros();
  while(us2==us) us2 = micros();
  Serial.println(us2-us);
}

void loop()
{
  unsigned long ms = millis();
  unsigned long us = micros();
 
  Serial.print(ms / 1000 / 60 / 60);
  Serial.print(":");
  Serial.print(ms / 1000 / 60 % 60);
  Serial.print(":");
  Serial.print(ms / 1000 % 60);
  Serial.print("\tmillis()=");
  Serial.print(ms);
  Serial.print("\tmicros()=");
  Serial.println(us);
 
  unsigned long now;
  do
  {
    now = millis();
  } while(now - ms < 1000);
 
  if(now < ms) Serial.println("Overflow!");
}

 

with the "<b>volatile unsigned long wdt_millis = 0xffff0000;</b>" hack gives me this output (about 1 minute in)

 

 

1193:2:44    millis()=4294964822    micros()=63062016
1193:2:45    millis()=4294965822    micros()=64062016
1193:2:46    millis()=4294966822    micros()=65062016
Overflow!
0:0:0    millis()=526    micros()=66062080
0:0:1    millis()=1526    micros()=67062016

 

make sure to work with unsigned long or uint64_t when handling milliseconds to avoid unintended truncation of values.

Link to post
Share on other sites

Hi !

 

Sorry, I am a beginner.

I am testing  launchpad stellaris (EK-LM4F120XL).

Energia version 0101E0009

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print("Time = ");      
  Serial.println(millis()/1000);
  delay(1000);
}

Result:

...

Time = 49

Time = 50
Time = 51
Time = 52
Time = 53
Time = 0
Time = 1
Time = 2
Time = 3

...

 

Overflow on 54 ?. ??

 

What do I do wrong?

 

 

 

Link to post
Share on other sites

I see the same behavior with the official 0101E0009 build and the Stellaris Launchpad.

 

Looks like there was a major overhaul of wiring.c since then that might fix the issue. Unfortunately I couldn't get the newer version to work for me. Grabbing wiring.c alone seems to completely screw timing (i.e. Serial doesn't work anymore), and grabbing larger parts of the hardware folder causes compile errors about a missing syscalls.h.

 

PS: You might have more luck getting an answer over on Stellarisiti http://forum.stellarisiti.com/

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...