Jump to content

Strange millis() behaviour

Recommended Posts

Hmmm, this test sketch



// Testing fixed millis, micros and delay functions

void setup()
  Serial.print("us resolution measured=");
  unsigned long us = micros();
  unsigned long us2 = micros();
  while(us2==us) us2 = micros();

void loop()
  unsigned long ms = millis();
  unsigned long us = micros();
  Serial.print(ms / 1000 / 60 / 60);
  Serial.print(ms / 1000 / 60 % 60);
  Serial.print(ms / 1000 % 60);
  unsigned long now;
    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
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() {

void loop() {
  Serial.print("Time = ");      



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.

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...