Pages: [1]   Go Down
Author Topic: delayMicroseconds() bug  (Read 1166 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
I Love Arduino!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you use delayMicroseconds, you risk data loss on the serial line.
For example, if you use the following code, some data sent is not echoed back!

Code:
char c;

void setup()
{
    Serial.begin(19200); //init the serial com
}

void loop()
{
  while (Serial.available()>0) { //check for data
       Serial.print(Serial.read()); //echo the data
  }
  
  delayMicroseconds(20000); //delay(20); //pause
  
}

If I send a lot of data, very quickly, like the following list, 1 2 3 4 5 6 7 8 9 10 11 12 13,
only some of the characters will be echoed back, for example: 1 2 8 9 10 11 12 13
(numbers 3 to 7 dissappeared, probably because they were being transfered during the pause)

If you use delay() there is no loss of data.

Tom
Logged

Thomas Ouellet Fredericks

Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
I Love Arduino!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, I see in the source code that interrupts are disabled during delayMicroseconds()
Should be a warning about that!

Tom
Logged

Thomas Ouellet Fredericks

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Good point.  I added a warning to the reference:
http://www.arduino.cc/en/Reference/DelayMicroseconds
Logged

Pages: [1]   Go Up
Jump to: