I'm intending to set up a digital delay using a circular buffer (in SRAM) with an Arduino Due (@84MHz). The lenth of the buffer determines the delay. I need about 1us resolution and the delay needs to be dynamically settable between 0 - 500us. I'll probably use most of the memory just for the buffer.

I've got a very basic setup going (with a fixed delay, no buffer), that just updates a temporary variable with the current state of the input signal and then writes the whole word to another port in order to set the state.

Next up is designing the buffer so I can add some delay to the output of the signal.

Filling the buffer using interrupts watching signals on the same port shouldn't be too difficult but I am wondering:

a) taking speed in consideration, which buffer method will have the least processing overhead? Memory stack or an array[], or someting else?

b) is there a way to read a port and set an output port in one clock cycle? I am now using REG_PIOD_ODSR=outbyte; but ideally it needs to point to a memory address directly.

  • What are you sampling / delaying with your project? how many bits is each sample?
    – benathon
    Apr 20 '15 at 22:33
  • @portforwardpodcast The idea is to signal two 3-phase IGBT's and be able to balance them independently, so be able to delay 1 set of three pulses relative to the other. The arduino is not generating the PWM's but simply intercepting them from a drive's circuit board and the outputting them.
    – captcha
    Apr 23 '15 at 5:44
  • So you are buffering 1 bit samples. It may sound counter intuitive but using a full byte to store a single bit May give more performance. This is because you avoid shifts and are just doing simple memory lookups. @amandoninc 's answer is correct, just use an array. As far as hand coding assembly i disagree. I bet you could get away with an output compare interrupt. Please comment back if you need more help
    – benathon
    Apr 23 '15 at 8:15
  • @portforwardpodcast Thanks, I'll give it a try and find out. At this stage the project is on hold but it's great to know how to move forward from here.
    – captcha
    Apr 23 '15 at 22:49

I would use an array, they're about as simple as it gets. 84 clock cycles per sample is very tight, but definitely possible. I'm guessing that's too close to the line to do any sort time testing - you will probably have to set up your loop to take exactly 84 instructions. This means you will probably need to hand-code in assembler. Be very mindful of branching instructions - both branches will need to take the same number of clock cycles.

The Uno (AtMega328) runs most (all?) instructions in one clock cycle, I don't know if the same applies to the Due. You would need to hand-code the assembler, check out how many cycles each instruction takes, and pad it out (there is a do nothing "NOP" instruction, which typically takes one cycle.


You can get all this work done for you by using a chip made for this purpose. This is called a Digital Delay Line, http://en.wikipedia.org/wiki/Digital_delay_line (or Analog Delay Line if you want analog values, e.g. for sound playback).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.