Minimum Value of delay() at M0

Hi I am trying to make pulses for a mosfet gate but in the order of ns. Is there something like delayNanoseconds() ? In addition what is the minimum delay time that i could use with M0 when it's clock speed is at 48MHz?

Well, if you could manage a 1 clock delay, perhaps with a nop; type command, the duration could be 1/48 MHZ, ~ 20.8ns.
So multilples of 20.8nS would be the pulse width limitations for regular IO commands.

CrossRoads: Well, if you could manage a 1 clock delay, perhaps with a nop; type command, the duration could be 1/48 MHZ, ~ 20.8ns. So multilples of 20.8nS would be the pulse width limitations for regular IO commands.

So using for example 5 times a nop; program will delay for a 5X20.8 ns? That is great! Thanks

I'd confirm with a scope and an IO pin.

Just uploaded the following sketch for looking at the difference between

delayMicroseconds(1) vs NOP;

Oscilloscope shows 1.44μs during NOP; and 2.5μs during delayMicrosends(1)

const int kPinLed2 = 2;

#define NOP asm volatile (“nop\n\t”)

void setup() {
// put your setup code here, to run once:
pinMode(kPinLed2,OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(kPinLed2,HIGH);
delayMicroseconds(1);
digitalWrite(kPinLed2,LOW);
delayMicroseconds(5);
digitalWrite(kPinLed2,HIGH);
NOP;
digitalWrite(kPinLed2,LOW);
NOP;
delayMicroseconds(5);
}

You’re spending a LOT more time doing the digitalWrites (and returning from and returning to loop()) than you are doing the delays. Writing to an output pin does not happen in zero time, especially when you use digitalWrite, which has relatively high overhead…

Also, IIRC, delayMicroseconds has a resolution of 4uSec.

Regards,
Ray L.

RayLivingston: You're spending a LOT more time doing the digitalWrites (and returning from and returning to loop()) than you are doing the delays. Writing to an output pin does not happen in zero time, especially when you use digitalWrite, which has relatively high overhead...

Also, IIRC, delayMicroseconds has a resolution of 4uSec.

Regards, Ray L.

Thanks Ray

Is there any other way for making a pulse in the order of ns duration? The lower i can achieve, the better!

Jeg_1: Thanks Ray

Is there any other way for making a pulse in the order of ns duration? The lower i can achieve, the better!

you couldl try to just go right at the registers using direct port manipulation.

You may also want to get familiar with timers?

You could use external hardware and delay one signal relative to another, gate one with the other to make narrower pulses if that is what you are after. Different logic families have different propagation delays thru the part, and you could also go lower tech with RC delays feeding into gates, etc. Direct port manipulation limits you to the system clock speed, the multiples of 20.8nS already discussed.

Why the extra characters here? ("nop\n\t")

You mention M0, is that the hardware you are using? Some of the posts seem to have 16 MHz Uno kind of times in them.

Jeg_1: Thanks Ray

Is there any other way for making a pulse in the order of ns duration? The lower i can achieve, the better!

The ONLY way you can create short pulses in software is to disable ALL interrupts during the period you need to generate the pulses. The minimum pulse width you can create will be limited by the number of instructions it takes to actually change the output. nSec duration pulses is only going to happen in hardware, NOT based on the CPU clock (which is incapable of generating pulses with width and spacing less than one clock cycle. Without more details on exactly what it is you're trying to accomplish, there's really nothing anyone here can suggest that is likely to be of much real help.

Regards, Ray L.

CrossRoads: Why the extra characters here? ("nop\n\t")

You mention M0, is that the hardware you are using? Some of the posts seem to have 16 MHz Uno kind of times in them.

Perhaps i made a mistake due to my ignorance in programming. I just copied this:

http://forum.arduino.cc/index.php?topic=43333.0

I thought it was a common practice to all arduino boards... Indeed my board is M0 and not UNO.

BulldogLowell: you could try to just go right at the registers using direct port manipulation.

You may also want to get familiar with timers?

Thanks for the material mate. I'll give it a look next days, and be back on this.

RayLivingston: The ONLY way you can create short pulses in software is to disable ALL interrupts during the period you need to generate the pulses. The minimum pulse width you can create will be limited by the number of instructions it takes to actually change the output. nSec duration pulses is only going to happen in hardware, NOT based on the CPU clock (which is incapable of generating pulses with width and spacing less than one clock cycle. Without more details on exactly what it is you're trying to accomplish, there's really nothing anyone here can suggest that is likely to be of much real help.

Regards, Ray L.

Dear Ray M0's clock frequency is at 48MHz. If i could make a pulse that short then it would be 20,8ns. More than adequate for my application. My initial purpose when i decided to mess with arduinos, was to make a board able to drive my mosfet switches through tc4420 drivers. Gate transformer isolation, and pulse duration at about 100ns minimum. I found it very convenient and simple if i could just vary a delay value. As i see it now it is not as simple as i thought. Even i feel too old for this i am ready to take it deeper. I have really got tired building each time different drivers for different applications. By finding a way with simple means to alter duration of pulses and time distance between them, it would be exactly what i would like to do.

Regards

If i could make a pulse that short then it would be 20,8ns.

You can't. Not in software, anyway. It would take at least two instructions (one to turn the pin on, one to turn it off), and that would probably require assembly language to set up in any sort of reliable way (ie, you might be able to get a ~40ns pulse every 500ns or so. Probably less often if you want to pulse several pins....

Now, "motor control" is one of the "killer apps" for microcontrollers, and you may find that there are special modes in the timers designed to support multiphase motors that will do exactly what you want. Or you might not. It is, however, almost certainly "beyond the capabilities of the Arduino environment" ...

CrossRoads: You could use external hardware and delay one signal relative to another, gate one with the other to make narrower pulses if that is what you are after. Different logic families have different propagation delays thru the part, and you could also go lower tech with RC delays feeding into gates, etc.

Ok. This is closer to me. I find it a very handy way! Thanks for the insight.

Regards

westfw: It is, however, almost certainly "beyond the capabilities of the Arduino environment" ...

Thank you westfw. I'll take a look to the motors drivers and timers also.

Have a nice day guys Regards