How to communicate with multiple I2C devices within one session?

5 views (last 30 days)
Hello All,
On my board there are two I2C devices: an ADC and a DAC. They are connected via one I2C bus and there are no base address conflicts. Via an Aardvark adapter and the ICT, I want to communicate with the devices within one session. What is the best way to create two I2C –object instances?
dac = i2c('aardvark',0,16);
adc = i2c('aardvark',0,76);
fopen(dac)
fopen(adc)
%write DAC
fwrite(dac,[bias 00 00])
%write control register of adc: not continuous, convert v1 v2 v3 v4
fwrite(adc,[1 95])
%write trigger register to start
fwrite(adc,[2 17])
for i=1:4
fwrite(adc,0); status = ['status : ' dec2bin(fread(adc,1))]
pause(0.5)
disp(status)
end
fclose(dac),delete(dac)
fclose(adc),delete(adc)
Doing it is similair to GPIB session with multiple instruments . However it generates an error:
Error using i2c/fopen (line 82)
Unsuccessful open: Cannot connect to the device. Possible reasons are another
application is connected, a driver is missing or the device is not available.
Error in aardvarktest2 (line 24)
fopen(adc)
Communication to each device separately is successful . But two devices in one session does not work. Anyone an idea how to communicate with two I2C devices in one session?
  1 Comment
Raptrick
Raptrick on 1 Apr 2016
For those of you who are interested: I got a nice hint from support. When the I2C object is open it is possible to modify the remote address. The code looks like this:
% base address of i2c devices
dac = 16;
adc = 76;
% make one i2c object for multiple ic's
ics = i2c('aardvark',0,0);
fopen(ics);
% select remote address of dac and write dac
ics.remoteaddress = dac;
fwrite(ics,[bias 250 00]);
% select remote adress of adc write and control register to config
ics.remoteaddress = adc;
fwrite(ics,[1 95]);
% write register to trigger conversion
fwrite(ics,[2 17]);
% poll status register of adc
for i=1:4
fwrite(ics,0); status = ['status : ' dec2bin(fread(ics,1))];
disp(status)
pause(0.5)
end
% close and delete
fclose(ics),delete(ics)
This works.
Patrick

Sign in to comment.

Answers (0)

Categories

Find more on Instrument Connection and Communication in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!