Jump to content
Sign in to follow this  
rycco

strange millis() overflow

Recommended Posts

Hello, i have some strange problem while using Energia on my Stellaris Launchpad. On reference sites is written that millis overflows after approximately 50 days, but mine wont last longer then 53 seconds.

 

Here is the code used just for displaying

int blueL = 40;
int blueState = 0;
unsigned long time;
unsigned long test_time;

void setup() {
  Serial.begin(9600);
  pinMode(blueL,OUTPUT);
  time = millis();
}

void loop() {
  if (millis() - time > 1000) {
   digitalWrite(blueL,blueState);
   blueState = !blueState;
   time = millis(); 
  }
  test_time = millis();
  Serial.print("current time in millis : ");
  Serial.print(test_time);
  Serial.print("\tlast blinked time in millis : ");
  Serial.println(time);
}

And here is what i get in time of overflow

current time in millis : 53176	last blinked time in millis : 52790
current time in millis : 53247	last blinked time in millis : 52790
current time in millis : 53319	last blinked time in millis : 52790
current time in millis : 53391	last blinked time in millis : 52790
current time in millis : 53463	last blinked time in millis : 52790
current time in millis : 53535	last blinked time in millis : 52790
current time in millis : 53607	last blinked time in millis : 52790
current time in millis : 53679	last blinked time in millis : 52790
current time in millis : 63	last blinked time in millis : 37
current time in millis : 129	last blinked time in millis : 37
current time in millis : 196	last blinked time in millis : 37
current time in millis : 262	last blinked time in millis : 37
current time in millis : 329	last blinked time in millis : 37
current time in millis : 396	last blinked time in millis : 37
current time in millis : 462	last blinked time in millis : 37

 Its just about 53 seconds before overflow. Does anyone have some advice ?

 

Thanks

Share this post


Link to post
Share on other sites

I'm not sure how it's done in Energia, but on the regular Arduino, the regular serial library uses a buffer that can easily be overrun, another strange thing with your code is it doesn't look like this is happening (I think you should see something like "ccccccccc...." when it happens).

It must be blocking, your loop takes about 64ms, that is consistant with the length of the string you're sending.

I'll dig out a stellaris and try it.

Share this post


Link to post
Share on other sites

I'm using millis() with uint32_t variables and everything goes fine.

uint32_t chrono;
uint16_t i = 0;

void setup()
{
  Serial.begin(9600);
  chrono = millis();  
}

void loop()
{
  chrono = millis();
  Serial.print(i, DEC);
  Serial.print("\t");
  Serial.println(chrono, DEC);
  i++;
  delay(999);
}

Output gives

 

86 85796

87 86794
88 87791
89 88789
90 89786
91 90784
92 91782
93 92779
...
100 99763
101 100761
102 101758
103 102756
104 103754
105 104751

 

I strongly recommend using int{8|16|32|64}_t types instead of int, long, long long, ... for 2 reasons:

  • int et al. types vary from one platform to another, int could be 2- or 4-byte long depending on the MCU;
  • int{8|16|32|64}_t types let you know exactly how many bits you're using

:smile:

Share this post


Link to post
Share on other sites

Same for me and same if using micros(), in the core these functions use the watchdog and an uint32 variable is incremented 160 times per ms, so it should reset after about 26843s or about 7.5hours (160 times less than the normal 50 days)

So there's something else not working.

Share this post


Link to post
Share on other sites

Rei Vilo,

 

 this is what i got ... (copypasted ur code)

42      41957
43      42956
44      43955
45      44954
46      45953
47      46952
48      47951
49      48950
50      49949
51      50948
52      51947
53      52946
54      258
55      1257
56      2256
57      3255
58      4254
59      5253
60      6252
61      7251

i have no idead why is this happening ...

 

 

EDIT - misspelling

Share this post


Link to post
Share on other sites

Hello, i have some strange problem while using Energia on my Stellaris Launchpad. On reference sites is written that millis overflows after approximately 50 days, but mine wont last longer then 53 seconds.

 

Here is the code used just for displaying

int blueL = 40;
int blueState = 0;
unsigned long time;
unsigned long test_time;

void setup() {
  Serial.begin(9600);
  pinMode(blueL,OUTPUT);
  time = millis();
}

void loop() {
  if (millis() - time > 1000) {
   digitalWrite(blueL,blueState);
   blueState = !blueState;
   time = millis(); 
  }
  test_time = millis();
  Serial.print("current time in millis : ");
  Serial.print(test_time);
  Serial.print("\tlast blinked time in millis : ");
  Serial.println(time);
}

And here is what i get in time of overflow

current time in millis : 53176	last blinked time in millis : 52790
current time in millis : 53247	last blinked time in millis : 52790
current time in millis : 53319	last blinked time in millis : 52790
current time in millis : 53391	last blinked time in millis : 52790
current time in millis : 53463	last blinked time in millis : 52790
current time in millis : 53535	last blinked time in millis : 52790
current time in millis : 53607	last blinked time in millis : 52790
current time in millis : 53679	last blinked time in millis : 52790
current time in millis : 63	last blinked time in millis : 37
current time in millis : 129	last blinked time in millis : 37
current time in millis : 196	last blinked time in millis : 37
current time in millis : 262	last blinked time in millis : 37
current time in millis : 329	last blinked time in millis : 37
current time in millis : 396	last blinked time in millis : 37
current time in millis : 462	last blinked time in millis : 37

 Its just about 53 seconds before overflow. Does anyone have some advice ?

 

Thanks

This is issue is related to Stellaris (StellarPad). A fix for this was made a while back and will be in the upcoming release.

https://github.com/energia/Energia/commit/84f14077b3c8c107ae58e023421746295c44d271

Share this post


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.

Sign in to follow this  

×
×
  • Create New...