How can I send I2C commands?

Hi Everyone,

I'm new to using I2C. I have a device (a DSP board) and I want to send some data from an Uno over I2C to it's registers to make it do something.

The DSP board is I2C master and has address 118 (0x76)

I have a program loaded on the DSP board with a register address (0x0016) holding a 4 byte value (32bit unsigned).

If the value is set 0 (0x00, 0x00, 0x00, 0x00) the board will steady light and LED.
If the value is set 1 (0x01, 0x00, 0x00, 0x00) the board will flash the LED.

How can I code the Arduino Uno to send that data to control the LED, please?

Thank you!

The DSP board is I2C master and has address 118 (0x76)

That's impossible. It's either an I2C master or it has address 118 but not both.

I have a program loaded on the DSP board with a register address (0x0016) holding a 4 byte value (32bit unsigned).

The I2C standard only describes the bus protocol but it doesn't tell anything about the higher levels. So there's no standard of how to write a register (although many I2C devices do that in a similar way) and that's especially true if the register is 32bit wide. So post a manual of that DSP device that describes the I2C requests necessary to write that register.

Thanks for your help!

It is an Analog Devices ADAU1452. Datasheet

In that case I think it must be a slave.

Currently it is connected to a USB programmer and the software 'Sigma Studio' can both program the DSP board eeprom so that the board self-boots and also control it in real time by writing to the registers. Conveniently, it shows the data being written.

I want to do that block writing from an Uno rather than the PC so I can control the DSP once in my device.

The flashing LED is just a practice example.

Use your favorite search engine with the keywords "arduino adau1452" for examples.

jremington:
Use your favorite search engine with the keywords "arduino adau1452" for examples.

I have done that. Most examples want to load the DSP board eeprom from the MCU which is not what I'm trying to do.

I tried to get this to compile for the Uno but didn't have much luck - Interfacing SigmaDSP Processors with a Microcontroller [Analog Devices Wiki]

That seems overly complex when all I want to do is spit some values at the right register.

The section titled "SLAVE CONTROL PORTS" in the datasheet that you linked (Pages 42-46) contain the required information for writing and reading the device's registers over I2C.

gfvalvo:
The section titled "SLAVE CONTROL PORTS" in the datasheet that you linked (Pages 42-46) contain the required information for writing and reading the device's registers over I2C.

Thanks. I'm using the master control port, not slave ports which I think should make things more simple. It says you can access all registers from the master port.

I've no idea if the requirements for writing to the I2C port on the DSP are the same as the Arduino wire.h library. Do you know?

This is my first time using I2C and it is a bit overwhelming, so a bit of hand-holding would be apprecaited!

ssashton:
Thanks. I'm using the master control port, not slave ports which I think should make things more simple.

That makes no sense to me.

It says you can access all registers from the master port.

Please provide the section and page number in the data sheet where it says that. This is what I found:

Capture.JPG

Capture.JPG

This is my first time using I2C and it is a bit overwhelming, so a bit of hand-holding would be apprecaited!

You have chosen an extremely complex device for your learning experience. You should start with something much simpler so that you understand the basics of I2C communication. It is not trivial.

Have you figured out yet what you want to do with this beast? Keep in mind that few people on this forum are likely to be interested in such a complex device, and probably will not be motivated to do much research for you.

That said, beginning on page 43 in the ADAU1452 data sheet (I2C slave port), information on basic communications and reading/setting register values is presented in great detail. Page 97 starts listing the device registers, and there are hundreds of them.
.
Study that material until you have a reasonable understanding of what is required. Learn to read and write one register. Then you will be in a position to ask a sensible question. You are not there yet.

I did it! :slight_smile:

Here is the code I wrote.

#include <Wire.h>

void setup() {

  byte subAddress[2]; // 2-byte sub-address (0x0016).

  subAddress[0] = 0x00;
  subAddress[1] = 0x16;

  byte dataValue1[4]; // 4-byte data value.

  dataValue1[0] = 0x00;
  dataValue1[1] = 0x00;
  dataValue1[2] = 0x00;
  dataValue1[3] = 0x00;

  byte dataValue2[4]; // 4-byte data value.

  dataValue2[0] = 0x01;
  dataValue2[1] = 0x00;
  dataValue2[2] = 0x00;
  dataValue2[3] = 0x00;

   
  Wire.begin(); // join i2c bus
  Wire.setClock(400000); // Set 400KHz frequency
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue1, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
  delay(10000);
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue2, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
 }

void loop() {
}

If I try to put the code in a loop so it continually switches the LED mode, I get the compile error “subAddress was not declared in this scope.” Why is that happening? Surely I don’t need to declare the subAddress byte every time it loops?

It's hard to tell what's wrong with the code that you didn't post.

I litteraly just put everything after wire.setClock in the loop instead of setup.

#include <Wire.h>

void setup() {

  byte subAddress[2]; // 2-byte sub-address (0x0016).

  subAddress[0] = 0x00;
  subAddress[1] = 0x16;

  byte dataValue1[4]; // 4-byte data value.

  dataValue1[0] = 0x00;
  dataValue1[1] = 0x00;
  dataValue1[2] = 0x00;
  dataValue1[3] = 0x00;

  byte dataValue2[4]; // 4-byte data value.

  dataValue2[0] = 0x01;
  dataValue2[1] = 0x00;
  dataValue2[2] = 0x00;
  dataValue2[3] = 0x00;

  Wire.begin(); // join i2c bus
  Wire.setClock(400000); // Set 400KHz frequency
   }

void loop() { 
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue1, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
  delay(10000);
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue2, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
  delay(10000);
}

Look up "scope". loop() doesn't know anything about variables declared and initialized in setup().

Thank you. I tried declaring the byte before void setup to make it a global variable, but it didn't like that either. How come?

It does work if I declare it inside void loop.

ssashton:
Thank you. I tried declaring the byte before void setup to make it a global variable, but it didn't like that either. How come?

How the smegging heck would we know if you don't show us what you tried? :o

ssashton:
It does work if I declare it inside void loop.

Then those are different variables from the ones in setup.

pcbbc:
How the smegging heck would we know if you don’t show us what you tried? :o

…because I just told you. I took the byte shown in the code above and declared it before void setup.

#include <Wire.h>

byte subAddress[2]; // 2-byte sub-address (0x0016).

  subAddress[0] = 0x00;
  subAddress[1] = 0x16;

  byte dataValue1[4]; // 4-byte data value.

  dataValue1[0] = 0x00;
  dataValue1[1] = 0x00;
  dataValue1[2] = 0x00;
  dataValue1[3] = 0x00;

  byte dataValue2[4]; // 4-byte data value.

  dataValue2[0] = 0x01;
  dataValue2[1] = 0x00;
  dataValue2[2] = 0x00;
  dataValue2[3] = 0x00;


void setup() {
  Wire.begin(); // join i2c bus
  Wire.setClock(400000); // Set 400KHz frequency
   }

void loop() { 
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue1, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
  delay(10000);
  Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.
  Wire.write(subAddress, 2); // Queue register sub-address
  Wire.write(dataValue2, 4); // Queue data value. Turn LED on solid.
  Wire.endTransmission(); // Send data queue and end transmission with stop bit.
  delay(10000);
}

When changing your code, ALWAYS post the latest version regardless of how trivial the change. It's really hard to envision code from words. And ALWAYS post the full code, not just the part where you think the problem is. If the problem was truly where you thought it was, you wouldn't need our help to fix it.

And, ALWAYS post the complete error message, not your paraphrased version of it.

"It doesn't work" is a piss-poor problem description.

"It doesn't work" is a piss-poor problem description.

The OP appears to be an extremely slow learner. I won't waste any more time.

It's snarky comments and ego trips like this that puts newbies like me off coding.

I joined this forum yesterday. I won't be back tomorrow. Good job! :slight_smile:

It's snarky comments and ego trips like this that puts newbies like me off coding.
I joined this forum yesterday. I won't be back tomorrow. Good job! :slight_smile:

Do you want to calm down and solve your problem, or do you want to leave the forum?

If its the former, you may want to review this previous thread about declaring , initializing and assigning variables, including arrays.