1

What is the fastest way to read a digital pin status in an Arduino Due? Is it possible to achieve speed in the Nanosecond area?

With which type of code I can achieve this?

Edit: This question is not a duplicate please see my comment.

William Roy
  • 515
  • 3
  • 10
  • 22
  • 1
    Yes, write to or read from the correct GPIO / PORT registers directly. Best case this needs only a few CPU cycles to read, i.e. a few nanoseconds. The processing time after that unknown, dependent on what you want to do with the read out data. Duplicate of http://arduino.stackexchange.com/questions/9117/sam3x8e-arduino-due-pin-io-registers. Also read http://forum.arduino.cc/index.php?topic=260731.0 . – Maximilian Gerhardt Mar 06 '17 at 12:27
  • 1
  • 1
    No it is not a duplicate. There you will find the answer how to set a digital pin as an output, but NOT how to set it as in input and read its pin state. Or atleast it is not explained detailed enough.

    Also you will not find the appropiate answer to the question "the fastest" but only sadly an alternative to the digitalWrite.

    You will not find any type of code example to set a pin as an input and to read from it, which actually for beginners like me would be very vital, if you would not forget that espiacially the "unnormal" register commands are very cryptic for a beginner.

    – William Roy Mar 06 '17 at 19:50
  • ns? no. us? yeah. – dandavis Mar 06 '17 at 20:39
  • The OneWire library uses it: https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.h – Jot Mar 06 '17 at 22:59
  • @Jot : Where exactly? Could you please post as snippet? – William Roy Jun 16 '17 at 20:23
  • @WilliamRoy all the defines and the directRead and directMode and so on functions are fast direct port writing. The OneWire.cpp show how a bitmask is stored and the DIRECT... defines are used. – Jot Jun 16 '17 at 22:31
  • @WilliamRoy Please see the GPIO library for a portable high speed Arduino library. https://github.com/mikaelpatel/Arduino-GPIO – Mikael Patel Oct 01 '18 at 05:54

2 Answers2

2

If the pin is declared as input, let's say pin 13:

uint32_t pin_status;
pin_status = PIOB->PIO_PDSR & PIO_PDSR_P27;

If the pin is declared as output, let's say pin 13:

uint32_t pin_status;
pin_status = PIOB->PIO_ODSR & PIO_ODSR_P27;
dda
  • 1,588
  • 1
  • 12
  • 17
0

Read the In out data register would be the fastest.

dannyf
  • 2,770
  • 10
  • 13