Arduino Uno using Spi for fast digitalread

Thread Starter

demir-ali

Joined Jul 13, 2024
321
Hi , I need to check the frequency of a digitalwave. It is important to do this precise. I must be able to differ periods like 28us 26us and more.
Since arduino provides 1us delay at it's shortest , it is very useless for this. I think I must be able to do this using Spi. Since spi can read up to 8mhz which gives 1us/8 , I can track when a digital signal goes 5 and goes 0 precisely.
Can I use arduino uno as a perfect slave which reads continously ?
How can I manipulate the sck pin ?
 

panic mode

Joined Oct 10, 2011
4,866
you want to check frequency of some signal?
why not count pulses in some time interval and calculate frequency from there?
 

Thread Starter

demir-ali

Joined Jul 13, 2024
321
Can I use smth like this If I write an algorithm without spi ?
NOP WAIT CODE:
void wait500ns() {
  // 8 NOP = 8 x 62.5 ns = 500 ns
  asm volatile (
    "nop\n\t"  // 1. NOP = 62.5 ns
    "nop\n\t"  // 2. NOP = 62.5 ns
    "nop\n\t"  // 3. NOP = 62.5 ns
    "nop\n\t"  // 4. NOP = 62.5 ns
    "nop\n\t"  // 5. NOP = 62.5 ns
    "nop\n\t"  // 6. NOP = 62.5 ns
    "nop\n\t"  // 7. NOP = 62.5 ns
    "nop\n\t"  // 8. NOP = 62.5 ns (Toplam = 500 ns)
  );
}

void setup() {
  pinMode(13, OUTPUT);  // SCK pini veya başka bir dijital pin
}

void loop() {
  digitalWrite(13, HIGH);  // Pin HIGH yap
  wait500ns();             // 500 ns bekle
  digitalWrite(13, LOW);   // Pin LOW yap
  wait500ns();             // 500 ns bekle
}
 

panic mode

Joined Oct 10, 2011
4,866
hmmm...something is not right.... you started by mentioning read operation and checking frequency.
so, where is the read? i see only digitalWrite...
 

Thread Starter

demir-ali

Joined Jul 13, 2024
321
hmmm...something is not right.... you started by mentioning read operation and checking frequency.
so, where is the read? i see only digitalWrite...
I did not share the code I read. This code is written by chatgpt that is claimed to be delaying 500ns
 

panic mode

Joined Oct 10, 2011
4,866
you can always read or write IO in a loop. but... the question is but what is it exactly that you wish to accomplish? because contents of yours posts are contradictory.
1728334692637.png
1728334701638.png
 
Last edited:

Thread Starter

demir-ali

Joined Jul 13, 2024
321
Hi mate , I asked 2 questions. First , can I manipulate the spi to do this ?
Secondly , gpt suggest this code for wait less than 1us.
There is nothing contradicts.
"I must be able to differ periods like 28us 26us and more.
Since arduino provides 1us delay at it's shortest , it is very useless for this. "
Here I clearly explained why would I need such a methodology or the gpts function.
I do not ask for the algorithm , I ask for if I can check wheter the signal input is high or low quickly.
 

panic mode

Joined Oct 10, 2011
4,866
syntax looks ok. function is also ok if you need something that generates fixed frequency square wave. if that works for you, great....
 

Thread Starter

demir-ali

Joined Jul 13, 2024
321
syntax looks ok. function is also ok if you need something that generates fixed frequency square wave. if that works for you, great....
loop is uninportant part there. if that nop function works , probably with using direct operation through registers , I can read quickly. I need reads around 1us/8 per read.
 

BobTPH

Joined Jun 5, 2013
11,463
To capture accurate pulse widths, you would set an input to interrupt on change and capture a running timer on each transition. At 16 MHz that would give you 62.5 nsec resolution.
 

Thread Starter

demir-ali

Joined Jul 13, 2024
321
To capture accurate pulse widths, you would set an input to interrupt on change and capture a running timer on each transition. At 16 MHz that would give you 62.5 nsec resolution.
yes , to count , I need some delay under microseconds. nop seems reasonable for me
 
Top