Skip to main content
Topic: How to test digital read speed? (Read 9185 times) previous topic - next topic

How to test digital read speed?

i'm wanting to test digital read speed of arduino in two scenarios...
1.  using the built-in like x=digitalRead(pin)
2.  using direct port manipulation like x=PORTC0

I can obviously test output very easily.. have it output on a pin, then put a scope on it and measure the cycle period.

Anyone know a logical way i can test input speed?

Re: How to test digital read speed?

Reply #1
I was offered a potential solution.  since i can tell my output speed, just have a loop do this:

write high
write low

clock that waveform

now do this:

write high
read
write low

now the difference is the read speed.

It makes sense, i'll update if it works when i can test.

Re: How to test digital read speed?

Reply #2
Looking forward to the update. Are you using a logic analyzer or capture and time the output?
Got a question? Please ask in the forum for the fastest answers.

Re: How to test digital read speed?

Reply #3
Well, i have an OLS, but i've not figured out how to use it yet (how to set up the client software appropriately).  Granted, that's mostly my fault since i haven't taken much initiative yet.  I'm using a Rigol DS1052E oscilloscope to check the timing. 

Just to note, I'm basing my timing on a baseline "clocking" with directly accessing a port for output and toggling on and off.  The code i'm using for that is

DDRD = 0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
//three cycles generated above
//instruction to test here
//four cycles generated below
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;
PORTD |= 0b10000000;
PORTD &= ~0b10000000;


So i should see three cycles, then be low for the same amount of low i got on my normal clock cycle PLUS the amount of time it takes to execute the command, then four cycles.

The PORTD commands above seem to take approx 124nS high and 124nS low  (if i don't compare it to what it was it brings it down to 62nS (half the processor cycles i assume).  The nS range is definitely accurate enough for what i'm using it for.  I'll update with my findings when i know more.  I'm also going to post the logic, procedure, and code on my website when i get it all figured out.

Re: How to test digital read speed?

Reply #4
Maybe you could use a slower clock like 1MHz and internal 1/8 divider to get even better results.

Ultimately you want to know how many cycles does it take, right? Why not use data sheet then:)

Re: How to test digital read speed?

Reply #5
I'm actually using the arduino IDE, but with direct port manipulation for some reads and writes.  I was trying to accurately determine if there's any timing difference due to the arduino overhead on the ATMega.  I guess I can get the cycle time from the datasheet, but i don't know how to tell how many clock cycles each of the instructions take (i'm not that knowledgable about microcontrollers).  It seems that if i do a  write with no logic, it takes 62nS (i'll assume one clock cycle?).  If i have it do a bitwise AND (or OR) and write, it takes twice as much time (i'll assume two clock cycles?).  I'm not sure how much an IF... Else or a Do... While, or any other type of logic actions will take.  I just figured that if i can use the scope to prove the timing, it'll be more accurate than my minimal understanding of how everything works "under the covers". 


I need the timing as fast and accurate as possible, because i'm trying to work with NTSC video, capture a reading, capture another, and compare. 

I'm using an LM1881 video sync seperator for timing of the reading of the input signal looking at only odd (or even, i can't remember right now) lines, certain line numbers, at certain places along the line.  Basically taking an opamp comparator measurement of single spots in a 14x12 matrix evenly spaced across the screen, then compare that to the next capture of the 14x12 matrix and see what changes (if anything).  So when it's grabbing an odd line, and x number of back porch (clamp) is detected, read the value of the comparator circuit (1/0) and that's one point in the matrix.

i'm using Dave Chatting's EyeShield code and scematic as the baseline, and modifying hardware and code to make it better suit my needs.  He is outputing an 8x8 matrix on an LED matrix display.  i don't need to output it, just compare two matrix reads to see if the video changes, and how much.  His loop does not seem to be as efficient as it could be using PORTx reads, he's doing "if (!digitalRead(inputPin))"so that's why i'm trying to determine the direct port access speed.  I know digitalRead is much slower because of the Arduino overhead.

I also found (the other day) the "Video Experimenter" shield, which i haven't played with the hardware or software yet, but looks to have more capability for what i'm wanting to do.  He's still using digitalReads rather than direct port reading, so i think i may be able to improve on the accuracy of his code, but I am trying to figure out how much difference (if any) it makes since NTSC video has specific timing.

If you have any suggestions on any of the stuff i was rambling about, please let me know.  Like i said, i've played with the arduino a little in the past, but that's about it.  Prior to that i had electronics in high school, and have played around with the 200-in-one type of electronics kit as a kid, thats it.  This is the first "real" project i'm undertaking (granted reusing schematic pieces and code parts from several sources and mashing them together and modifying them for my use) so i'm just trying to understand everything.

Re: How to test digital read speed?

Reply #6
Quote
Prior to that i had electronics in high school, and have played around with the 200-in-one type of electronics kit as a kid, thats it.

That's a great place to start :)
Got a question? Please ask in the forum for the fastest answers.

Re: How to test digital read speed?

Reply #7
The Arduino read and write functions can take somewhere from 40 to 80 cycles to be done, if you are toggling a pin you can write to the PINx register when in output and that will do an atomic toogle in a single cycle of the desired bit in the desired port, when you want the fastest thing thats as fast as the Avr can go.

Re: How to test digital read speed?

Reply #8
Senseo, that's what i found.  I know i got the timing down really small doing PINx and PORTx read/writes, but i want to actually measure the specific timing and post it.  I've been slammed at work (where my scope is at the moment) so i haven't had a chance to get the timing tested/recorded.  However, with the help of others on the boards, i have a good understanding of how to test both write and read times.

Will update when i know more.

Thanks!

Re: How to test digital read speed?

Reply #9
use avr-objdump to generate a .lst file which is the assembly code that your C code creates, then you can exactly predict the timing since you know the length of each instruction exactly, which is given in the datasheet for your microcontroller

Re: How to test digital read speed?

Reply #10
[quote author="frank26080115"]use avr-objdump to generate a .lst file which is the assembly code that your C code creates, then you can exactly predict the timing since you know the length of each instruction exactly, which is given in the datasheet for your microcontroller[/quote]

unfortunately, i'm not extremely microcontroller savy... i'm just learning and documenting my journey... I understand concepts and what little i've played around with... i'll do some searching to see how to use avr-objdump and see what i can figure out in that respect.  I'm more of a visual learner with very abstract logic train of thought... i still want to physically time it where i can see it with a scope :)  it's more fun