Timing

Hii

My question is if i make a pin high and suddenly make it low for how much time it will be in high state.

For example

digitalWrite (10, HIGH);
digitalWrite (10,LOW);

In this pin 10 will be set for how much time?

Thanks

Undefined. First off, was it high to begin with? Second, the amount of time it takes to process the command and set the pin low depends on the clock Speed of the cpu. Third, what else is attached to the pin? Something that is trying to pull it one way or the other will affect the Timing. To find out, you will Need to put a scope on it.

first low then high then again low.

It is arduino uno so clock speed is 16MHz.

Nothing is connected to it.

Someone measured digitalWrite() at about 6 microseconds. I would expect a pulse time near that.

If you need faster pulse times, use direct port manipulation (about 0.4 microseconds) or hardware timers (maybe down to 0.0625 microseconds?).

Using direct port manipulation, you can get it down to one or two clock cycles, 62.5 or 125ns.

// assuming it was low already, pulse D8 on an Uno high for the shortest time period:
PORTB = 0b00000001;
PORTB = 0b00000000;

CrossRoads:
Using direct port manipulation, you can get it down to one or two clock cycles, 62.5 or 125ns.

// assuming it was low already, pulse D8 on an Uno high for the shortest time period:

PORTB = 0b00000001;
PORTB = 0b00000000;

Shouldn't that be:

// assuming it was low already, pulse D8 on an Uno high for the shortest time period:
PINB = 0b00000001;  // Toggle D8
PINB = 0b00000001;  // Toggle it back

[/quote]

johnwasser:
Shouldn't that be:

// assuming it was low already, pulse D8 on an Uno high for the shortest time period:

PINB = 0b00000001;  // Toggle D8
PINB = 0b00000001;  // Toggle it back

PINX is for input only, afaik.

PaulMurrayCbr:
PINX is for input only, afaik.

Writing to PINx toggles all bits that are written with one (at least on a 328 or 2560).

PaulMurrayCbr:
PINX is for input only, afaik.

Not so, read the AVR datasheets. A write to a PINx register bit will toggle the matching PORTx pin.
It's a feature. You don't have to check a pin or use a mask to achieve fast pin change.

IIRC Nick Gammon uses that trick in his Arduino VGA tutorial.

BTW, the Arduino functions are slow because they assume a need to set the port up first which with direct port manipulation is the responsibility of the programmer.

See http://forum.arduino.cc/index.php?topic=4324.0 for extensive (but perhaps out-of-date) discussion.

LOL, I was running IDE 0023 clear up until IDE 1.05 was out just because the changes that killed my breadboard duino programming method, iffy as it was.

No longer kicking and screaming, Nick saved the day!

In response to a question in this Forum, the execution time of various Arduino Instructions were measured and reported. The experimental setup was:

Timer-1 of TC1 was started at clkTc1 = 16 MHz just before the execution of the target instruction (say, digitalWrite(4, HIGH). The Timer-1 was stopped just after the execution of the target instruction. The execution time of the target instruction was equal to clcTC1 pulses so accumulated by Timer-1. The following timings were measured:

1. digitalWrite(4, HIGH); : 59 cycles ===> 3.687 uS
2. digitalWrite(4, LOW); : 61 cycles ===> 3.812 us
3. DDRD = 0b00001000; : 4 cycles ===> 0.250 us

//--------------------------------------------------------------------------------------------------

exet.doc (44 KB)

GolamMostafa:
1. digitalWrite(4, HIGH); : 59 cycles ===> 3.687 uS
2. digitalWrite(4, LOW); : 61 cycles ===> 3.812 us
3. DDRD = 0b00001000; : 4 cycles ===> 0.250 us

Note: Setting the DDR (Data Direction Register) is equivalent to pinMode(), not digitalWrite(). Setting the register to 0b00001000 will set Pin 4 as an OUTPUT, but it will also set the other 7 pins as INPUT, thus it is not a valid substitute for pinMode(4, OUTPUT). You need bitSet(DDRD, 4);

You need bitSet(DDRD, 4);

You are absolutely right; but, my examples codes were demonstrative codes and were intended to collect only the execution times. The three instructions of my example were no way linked/related to each other.

Whandall:
Writing to PINx toggles all bits that are written with one (at least on a 328 or 2560).

GoForSmoke:
Not so, read the AVR datasheets. A write to a PINx register bit will toggle the matching PORTx pin.

! That's handy. You learn something every day.

Thanks to all.

I got my answer.

Hii

I have another doubt. In arduino SPI sclk frequency is how much and what is the low time and high time ?

Same arduino uno board and 16 MHz clock.

SPI clock has a system clock divider, the default is 4.

If you run at 16MHz the default SPI is 4MHz, one byte takes 8x4 cycles to transmit.

It is best to run SPI through the SPI hardware port. Next best is using a serial port to do master-mode SPI and last is bit-banging your own SPI in software, which won't run nearly as fast.

Do you know that hardware SPI is bi-directional? It can read data bits in as it clocks data bits out.

Nick Gammon has a tutorial on the subject that includes code to make an Arduino work as and SPI slave device.
http://www.gammon.com.au/spi