Jump to content

Millis not regular on oscilloscope

Recommended Posts

I've written a 7-segment refresh routine that runs every 2 millis at the beginning of loop().  It starts this way:


void loop() {

  CURmillis = millis();
  if ((CURmillis - PREVmillis) > 1) {    // 2ms refresh period = 71 Hz per segment

    PREVmillis = CURmillis;

       // Turn the current segment OFF while making changes - prevents ghosting


The digitalWrite to pin 18 has been added so I can measure how long the refresh routine takes to run.  Pin 18 is turned off  as the last instruction in loop().  So the idea is to put my scope on pin 18, and see how much time the routine takes to run, and make sure there's no chance of an overrun.

The good news is that the routine runs in about 25 uSec, so it will never overrun the next milli.  But the bad news is that the scope shows that millis do not occur regularly.  Pin 18 generally triggers the scope at 2 ms intervals, but there are occasional triggers at other points.  I can't tell if the extra triggers are early or late, but they never go away.

When I did this test on the assembler version of this code, with the Watchdog timer in interval mode, and the clock running at 1 MHz, the pin 18 triggers are absolutely regular.  So Energia is doing something in the background that's causing this behavior.  Of course it has to execute whatever is done to update the millis value, but I can't iimagine that this would take much time.  But I guess there could be some "beat" in the interaction of the background millis interrupt and the loop() execution frequency.

Is there any way to get rid of this millis flutter?  Is it possible to hook into the interrupt that Energia uses for millis?  If so, I could put my routine into the ISR, and loop() would be empty.



Link to post
Share on other sites
  • 1 month later...

I just wanted to say that I think I found the answer to the non-regular millis, at least in the Arduino IDE, and I suspect it's the same in Energia.  The interrupt that updates millis is actually a bit slower than 1ms.  So periodically, the millis value is incremented by 2 so as to keep the average rate at exactly 1ms.  Since my IF statement executes every other milli, it will execute after only 1ms when the millis count increments by 2.  And that's why the scope showed the irregular trigger.


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