Generating 10us Pulse Width signals

Hi,

I'm new to Arduino and trying to generate 4 periodic signals with 10us pulse width.
I have read some forums that show how to generate these pulses but they are not suitable for my case.
So here are my codes:

void setup() {
    pinMode(0, OUTPUT);
    pinMode(1, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    PORTD = B00000000;
}

void loop() {
    PORTD = B10000000;
    delayMicroseconds(10);
    PORTD = B00000000;
    delayMicroseconds(10);
    PORTD = B01000000;
    delayMicroseconds(10);
    PORTD = B00000000;
    delayMicroseconds(20);
    PORTD = B00100000;
    delayMicroseconds(10);
    PORTD = B00000000;
    delayMicroseconds(10);
    PORTD = B00010000;
    delayMicroseconds(10);
    PORTD = B00000000;
    delayMicroseconds(20);
}

But when I measured the signals I notices that my pulse width is not 10us but about 9us.
Like this:


Is there a way to solve this issue? And does "delayMicroseconds" lead to this?

1. I think there are 4 separate signals on 4 DPins? What is the reference DPin?
2. I think they are continuous signals? Correct?
3. I think the ON-time is 10-us and OFF-time is 90-us. Correct?
4. The signals are displaced by 10-us from the previous one. Correct?

That might be due to the absolute accuracy of your Arduino clock frequency combined with the accuracy of your oscilloscope.
Does you Arduino have a crystal clock, or a ceramic resonator?

In fact your signal should be longer than the delay value because you don't take into account the time it takes to execute the bit manipulation lines.

Hi,

  1. Yes they are separate signals on 4 pins. But there are correlations between them. Like this figure:

    I'm not sure what "reference DPin" means, but I think you mean this signal at the top?
  2. Yes they are continuous signals.
  3. Yes so it's like a PWM signal 10us ON and 90us OFF.
  4. Like the figure in 1, dis1 and sw1 are displaced by 10us and dis2 and sw1 are displaced by 20us, and dis2 and sw2 are displaced by 10us.

HAve I missed something?
What hardware are you using?

As youre new to the forum you should read this:

What scope are you using ?

I'm using Uno R3. Previously I was using digitalWrite but I noticed that was too slow for generating us-range signals. I also read some posts teaching how to generate PWM signals using Timer but I still don't know how to generate the displace between each signal.

Thanks. I'm using Uno R3.

I believe (on AVR MCUs) micros are incremented by 4, so you can get 4, 8, 12, 16, etc. Try 12 and 24.

When you wind up the time base on your scope so that your pulse covers over half of the screen and measure it again using the cursors, Do you still get the same pulse width. I suspect you will not, it will be a more accurate one.

As I understand it that only applies to the micros timer, this is a delay and it works as you would expect.

Yes, that's what I meant, the Arduino delayMicroseconds() function. Could probably do it with a hardware timer but I haven't had the need to learn AVR timers so far.

Excellent description. Yes -- ds1 is the reference.
All your required signals could be generated accurately and precisely using Timer Module(s) of the ATmega328P MCU of the UNO Board.

In the mean time you can have a look of the attached file for Timer/Counter Modules of the said MCU.
Ch-10TCLec.pdf (443.2 KB)

Well this says not:-
Arduino reference

And this says about micros()
micro()
So you see the resolution you said only applies to the micros() timer, not the delay.

@jzz777
Note the page about the the delayMicro seconds says:-

This function works very accurately in the range 3 microseconds and up to 16383. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times. Larger delay times may actually delay for an extremely brief time.

So it looks like you are using it in not the most accurate way.
One way round this might be to use the in line assembler and have a sequence of 16 nop operations.