Storing multiple bytes

Hi All,

I am writing a program that uses I2C between a Leonardo and an AMC7812B. I need to pull the data on all 16 channels. Any guidance on the best way to store 32 bytes? An array possibly...?

Do not crosspost!

Mark

holmes4: Do not crosspost!

Mark

Do not make false accusations.

AWOL.

Any guidance on the best way to store 32 bytes? An array possibly...?

Yes an array of ints will do it.

holmes4: Do not crosspost!

Mark

I did not cross post. AWOL moved my post...

Grumpy_Mike: Yes an array of ints will do it.

Okay thanks, wouldn't it be more efficient to use a byte array instead of int array?

The AMC7812B gives you 12-bit's per channel. How do you want to store that in a byte? ;)

wouldn’t it be more efficient to use a byte array instead of int array?

No.
It takes the same amount of memory and with bytes you have to spend extra instructions packing and unpacking bytes.

Grumpy_Mike: No. It takes the same amount of memory and with bytes you have to spend extra instructions packing and unpacking bytes.

Great Thanks Mike.

septillion: The AMC7812B gives you 12-bit's per channel. How do you want to store that in a byte? ;)

True. So since the registers are 16 bit would it be beneficial to add 0's for the 4 missing bits?

When do you want to add them? And how...

Also, I2C already gives you byte chunks because it can only transfer a byte at a time and whole bytes only.

If you really want to you can save it in 24 bytes, 12 ints or 6 longs.... But what would mean channels are smeared across multiple variables (or array elements) and I doubt that's worth the 8 byte memory save...

So since the registers are 16 bit would it be beneficial to add 0's for the 4 missing bits?

That is automatically done when you read the A/D.

Hi Guys,

I have a follow on question regarding this project. I am still using an Arduino Leonardo as an I2C master to an AMC7812B. I am having trouble determining if it is more efficient for me to define each ADC register that I need (which is all 16) and explicitly read from each register in its own transmission. Or to loop through all of them starting at channel 0. Either way I am trying to read all 16 ADC registers in the most efficient and/or beneficial way possible.

Dennis

You loop thought them all, that way you do not have the overhead of the device address and the register to read.

Grumpy_Mike: You loop thought them all, that way you do not have the overhead of the device address and the register to read.

That was my assumption. Is there a good example of how to do this somewhere. I am unsure of how to shift the register pointer to the next channel.

This is the way I see it in my mind. But I am new to the realm of hardware and I do not understand some things involved with what the circuit is doing. I am also unsure if I am shifting the register pointer correctly…

byte adcChannel = 0x23;

void read_adc()
{
  for ( int chan = 0; chan < 16; chan++ ) 
  { 
    Wire.beginTransmission( ADC_ADDR );            // Transmit to ADC device
    Wire.write( adcChannel );                            // Point to channel register
    Wire.endTransmission();                              // Stop transmission

    Wire.requestFrom( ADC_ADDR, 2 );               // Request 2 bytes of data from the register
    while ( Wire.available() ) 
    {
      adcData[chan] = Wire.read();                         // Get the first byte (MSB)
      adcData[chan] = adcData[chan] << 8;             // Shift byte to high byte
      adcData[chan] = adcData[chan] + Wire.read(); // Get the second byte (LSB) 
    }
    adcChannel++;                                              // Increment the channel 
  }
}

You have to set up the chip to work as you want it. Their are register bits that control the configuration. From page 31 of the data sheet:-

Auto mode is a continuous operation. In auto mode, each analog channel within the specified group is converted sequentially and repeatedly.

Page 51 shows you how to write a single byte, Page 52 shows you how to write multiple bytes. The next two pages cover reading in a similar way. Page 60 shows you the register map of the device. Register address 23 to 32 are the DAC registers, you need to read a word ( two bytes ) from each one. Note your code reads from register 0 to 15.

Pages 61 to 80 break down those registers into bytes, telling you what each byte does.

It is a big data sheet, it needs a lot of study.

Note your code reads from register 0 to 15.

I think what the op is doing will read from 0x23 to 0x32

Within his for loop which interates over an array holding the data he uses

  Wire.write( adcChannel );  // Point to channel register
 adcChannel++;

I have not looked at the data sheet, but as you suggest there is probably a way to do a 16 register sequential reading without the overhead of repeated using

Wire.beginTransmission( ADC_ADDR );            // Transmit to ADC device
    Wire.write( adcChannel );                            // Point to channel register
    Wire.endTransmission();                              // Stop transmission

cattledog: I think what the op is doing will read from 0x23 to 0x32

Within his for loop which interates over an array holding the data he uses

  Wire.write( adcChannel );  // Point to channel register
 adcChannel++;

Yes this should read the correct channels because I am starting the count at register 0x23 and incrementing at the end of the for loop.

@Mike

I will look back at the datasheet, I am sure I will have more questions. This EE stuff is gonna be the death of me. LOL

Grumpy_Mike:
Page 60 shows you the register map of the device. Register address 23 to 32 are the DAC registers, you need to read a word ( two bytes ) from each one. Note your code reads from register 0 to 15.

Are you sure?

I think that the code you posted is indeed the optimal way to read the 16 ADC registers. It is the same as Figure 104. Read Multiple Registers Using the Reading Single Word from Any Register Method.

The data sheet states in relation to the i2c interface

The address pointer does not change after the current register is accessed. To change the pointer, the master issues a slave address byte with the R/W bit low, followed by the pointer register byte

I do not believe that there is a method to read the 16 registers sequentially without this preamble for each channel.

    Wire.beginTransmission( ADC_ADDR );           
    Wire.write( adcChannel );                          
    Wire.endTransmission();                            

    Wire.requestFrom( ADC_ADDR, 2 );

cattledog:
I do not believe that there is a method to read the 16 registers sequentially without this preamble for each channel.

At least not using Arduino IDE…The reason I am using Arduino IDE instead of Atmel Studio is because this code is for an ATmega32U4 found in the Arduino Leonardo. I hope that as long as I am incrementing the adcChannel value correctly, it should be reading all 16 channels.