Using interrupt on Arduino to give synchronous bit output.

I have a device I’m trying to control with the Arduino.

It works like this.

  1. Arduino sets and holds ‘ready line’ to 5V high.
  2. Device sees ready line high and sends 16 clock pulses for two bytes of messages to receive at a rate of about 8.3Khz.
  3. On each clock pulse the Arduino sets the data output to whatever the bit should be (0 or 1)
  4. When the two bytes have been sent, the ‘ready line’ is set back to 0v low.

The problem I have is getting the Arduino to sync to the clock pulses. With some Googling I came across interrupts.

The thing is I’ve read you can’t pass arguments to a interrupt function, not even global variables?

My idea would be to prepare the byte to be transmitted in an array and then send the ready line high and when the first clock pulse falling edge was received it would trigger an interrupt to output the bit.

To do this though I would need to be able to tell the interrupt what state to output (0 or 1) and to increment the counter so it would know what place of the byte it was at.

Any ideas?

Thanks,
Jack

Provide a link to the device it looks very like SPI with the arduino as the slave.

Mark

It's not SPI as far as I can tell. The device is an old 80s synth. I'm treating it like a black box.

Have a look at the Arduino reference on add interrupt.
there’s a load of info on it in there you can set it to falling edge.
call another function from within the interrupt, may work.
A newbie myself but trying to help.

I should point out there is zero documentation for it apart from circuit diagrams.

If the clock on the other device is reliable perhaps you could just treat the data as serial input at a non-standard baud rate? 8.3khz is pretty slow.

...R

jackdamery:
The thing is I’ve read you can’t pass arguments to a interrupt function, not even global variables?

You can’t pass arguments, no. But you can read global variables in an ISR (they’re not ‘passed’, they’re just ‘there’). You can also write to global variables defined as ‘volatile’.

You can call a function from an ISR, but if the function you are calling is only ever called from the ISR (it’s never called from anywhere else), then a function has no advantage over just putting the code right in the ISR itself.

My idea would be to prepare the byte to be transmitted in an array and then send the ready line high and when the first clock pulse falling edge was received it would trigger an interrupt to output the bit.

Even if this is not true SPI, holmes4 has the right idea. The ATmega328’s SPI hardware can help. It’s basically a shift register, so as your black box toggles the clock pin, the SPI hardware will automatically spit out the data bits for you so you don’t have to worry it in software. In software, all you have to do is keep the SPI buffer fed with bytes in a timely manner (which should be no problem at <9kHz).

Whether or not you should use interrupts or the SPI hardware (or even just a tight software poll loop) depends on what else your Arduino needs to be doing and how fast your black box expects a new bit after toggling the clock. Does your Arduino need to service other time-sensitive tasks at the same time, or is sending bits to this black box the only thing it needs to worry about?

Are there any other lines on the device (other than power or ground). Another way would be to load the 16 bits to a pair of shift reg's and then let it clock the data out of the shift reg's. Are you sure that it's the device that provides the clock?

Mark