Created Tue, 21 Jan 2014 00:22:54 +0000 by mitchjp
Tue, 21 Jan 2014 00:22:54 +0000
I have my max32 sampling some data and measuring the time between samples and all of this is triggered by a rotary encoder with a pretty high resolution (2000 ppr). The rotary encoder is hooked up to INT1 on the rising edge and all it does is increment a counter.
The code sort of goes like this:
>Read millis() and start the sampling.
>Once (count%20 == 0), ie every 20 clicks, read millis(), read sensor and send both via serial.
>Repeat until told to stop surveying.
The code works fine. I am posting because I am slightly worried that I might be losing time somewhere and it's difficult to see.
I have read that the millis() func is interrupt based and so if I am interrupting 2000 times a second (about full speed) am I going to lose much time on the millis func?
Bare in mind that there will only be maybe 1-10 millisecond between each sample and the survey only ever last 10 minutes or so, so the over flow of millis probably isn't important. But if the clock on the PIC is running at 80 MHz and I have a 32 bit number then every 53 seconds it will overflow and I guess an interrupt is called? Not too sure how this really works.
Thu, 23 Jan 2014 11:59:13 +0000
The millis() function is all linked to the chipKIT task scheduling system. That is driven by the Core Timer, which is separate to all other timers and is controlled by the internal management "Co-Processor". It's a constant free-running timer that cannot be stopped or changed. It runs at the speed of the CPU and just constantly ticks away.
The scheduling system works out when the next task needs to run and schedules an interrupt to occur against that core timer at the required time. That interrupt is set to be at priority level 7, so it will take precedence over all other interrupts. This ensures that no millis() ticks should get missed, unless interrupts are disabled at the time. To try and counter that there is code that examines the millis and compares it to what it thinks the millis should be according to when the interrupt should have been fired, and it tries to compensate for that.
So it is highly unlikely you are losing any time, or if you are it'll be very very minor.
Sat, 25 Jan 2014 00:34:27 +0000
But if the clock on the PIC is running at 80 MHz and I have a 32 bit number then every 53 seconds it will overflow and I guess an interrupt is called? Not too sure how this really works.
millis() returns an unsigned int - 32bit number, while the millis() increments in 1msecs steps. So the millis() will overflow in 49.7 days.
There is micros() as well, returning unsigned int, incrementing in 1 usecs steps, it overflows in 71.58minutes.