Go Down

Topic: How to reset micros() to start/zero ? (Read 10 times) previous topic - next topic

WilliamK Govinda

Ok, I took some time to figure out how exactly timers work out and how to use them for my project. It does look much better now, I used portions of the Tone code.

Now a question. What happens if my code takes too long inside an interrupt? How can I check if that happened?

As in ...
Code: [Select]
ISR(TIMER2_COMPA_vect)
{
   // CRAZY long code //
 unsigned long iou = 0;
 for (unsigned long pp=0; pp<99999999; pp++) { iou += pp+12; }
}


I know the code should be short. But I need to know how can I check if I made something too complex for the HZ calls I'm doing.

I'm pretty sure this is yet another stupid thing I'm doing, so, please, bear with me while I learn this up.... and thanks again for helping me out. ;-)

Wk

WilliamK Govinda

AHHHHH!!! I just finished up my new MyTimer Library plus 2 Examples. One is a Midi Clock at 960 PPQ, and it works great! Doesn't miss a pulse at all. I played it into my notebook and its perfect. The clock is like 0.1% slower than what other clocks I tested, but that's why they created MIDI-Sync messages for. In any event, this is a MASTER clock, so I don't really care if its 0.1% slower than other devices BPM clocks.

I uploaded a new ZIP file and will also post the new examples here.

http://arduino.wusik.com

http://www.wusik.com/arduino/Libraries/MyTimer/MyTimer.zip

Wk

WilliamK Govinda

Follow ups at the following thread:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292438829/0

Wk

bill2009

Quote
Even in cases of overflows, endtime-startime will always be the correct elapsed duration, as long as the types of endtime and starttime match the the type returned by your counter, which means unsigned long for millis() and micros().

If you start adding durations to timestamps or comparing timestamps with each other, your program will fail because of the overflow.

The sane conclusion to this is to only use code such as:

if (endtime - starttime >= delay) {

Korman


Belated thanks, I've learned something genuinely useful today.

Coding Badly

Quote
Even in cases of overflows, endtime-startime will always be the correct elapsed duration, as long as the types of endtime and starttime match the the type returned by your counter, which means unsigned long for millis() and micros().

A correction... The variable types do not have to match the type returned by the function.  Any variables used must be unsigned and should all be the same type.

For example, this works for ranges up to 255 milliseconds...

Code: [Select]
unsigned char Previous;
unsigned char Current;

void setup( void )
{
 Previous = millis();
}

void loop( void )
{
 Current = millis();

 if ( Current - Previous >= 100 )
 {
   // This runs every 100 milliseconds
   Previous = Current;
 }
}

Korman

#35
Dec 17, 2010, 08:18 am Last Edit: Dec 17, 2010, 08:19 am by Korman Reason: 1
True, although people don't expect to get more roll-overs.

Korman

Go Up