Send 14 bytes during one CLK interval in SPI

Hello everyone,

I have a question regarding SPI. I'm currently working on a custom microscope project where I need to adjust a mirror (MR-E-2 from Optotune, find the manual here or look for it on the homepage of Optotune) via the SPI interface using an Arduino Uno board. So far, I was not able to control the mirror.

I wrote a very simple code for starters. Later in the project, the Arduino is supposed to receive mirror positions it will then transfer to the mirror. That is the reason I created a function called writeFloat().

My code is:

#include <SPI.h>

short writeCom = 0x0001;

void setup() {
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  digitalWrite(SS, HIGH);
  SPI.begin();
}

void writeFloat(short regAd1, short regAd2, float data1, float data2) {
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
  digitalWrite(SS, LOW);
  SPI.transfer(writeCom);
  SPI.transfer(regAd1);    // X register adress
  SPI.transfer(regAd2);    // Y register adress
  SPI.transfer(data1);    // data to write in regAd1
  SPI.transfer(data2);    // data to write in regAd2
  digitalWrite(SS, HIGH);
  SPI.endTransaction();
}

void loop() {
  writeFloat(0x5000, 0x5100, 0x3d4ccccd, 0xbda3d70a);
  delayMicroseconds(5);
}

The mirror needs to receive a signal frame consisting of 14 bytes:

2 bytes write/read command (here 0x0001 for write)
2 bytes register adress for x axis
2 bytes register adress for y axis
4 bytes of data to be written in the x register adress (here a float giving the current in x direction n amperes)
4 bytes of data to be written in the y register adress (here a float giving the current in y direction in amperes)

The values of the bytes in my code are taken from an example in the manual.

I connected the pins with the mirror following SPI - Arduino Reference.

I checked the wires connecting the Arduino and the mirror with an oscilloscope and what I see is that the clock signal goes up after every SPI.transfer(). But for the mirror to correctly process the command, it needs to receive the whole frame during one clock interval. My question now is: Is it possible to send 5 variables of different data types consisting of 14 bytes during one clock interval? Thanks in advance!

PS: This is my first time using Arduino, setting up an SPI connection and using this forum. I hope I gave you all the information you need.

In the SPI world the 'clock interval' and 'clock signal' would normally refer to the SPI CLK signal and the SPI interface will generate 8 of those clocks for every byte you want to read\write with SPI.transfer().

In the period between NSS going LOW and being returned HIGH you can with most SPI devices carry out as many SPI.transfer()s as you need.

The datasheet shows it's a regular SPI with 14 bytes per transaction (from slaveSelectLow to slaveSelectHigh)

It's easier to put all your data in a struct and send the "content" of the struct byte-by-byte using the SPI.transfer(buffer, length) command

// define struct

struct mirror {
		uint16_t command;
		uint16_t regAd1;
		uint16_t regAd2;
		float data1;
		float data2;
	};

struct mirror testData;
struct mirror *ptr_testData = &testData;

// add data to struct

	ptr_testData->command = 0x1234;
	ptr_testData->regAd1 = 0xABCD;
	ptr_testData->regAd2 = 0x1111;
	ptr_testData->data1 = 12.3456;
	ptr_testData->data2 = 8.132874;

// send the data

SPI.transfer((uint8_t *) ptr_testData, 14);

I think that should work...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.