Pages: 1 2 [3]   Go Down
Author Topic: How to reset micros() to start/zero ?  (Read 7211 times)
0 Members and 1 Guest are viewing this topic.
Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Follow ups at the following thread:

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

Wk
Logged


ottawa, canada
Offline Offline
God Member
*****
Karma: 5
Posts: 989
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  }
}
Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 6
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

True, although people don't expect to get more roll-overs.

Korman
« Last Edit: December 17, 2010, 02:19:31 am by Korman » Logged

Pages: 1 2 [3]   Go Up
Jump to: