Access multiple I2C sensors with C2000 and simulink
8 views (last 30 days)
I am trying to use a TI C2000 microcontroller to access data from an external adc using I2C. This ADC has multiple channels so I would like to be able to retrive data from all of them. Right now I have code setup to pull data from one channel and it works great. It can communicate with the ADC, retrieve the data, and show it on a matlab scope in real time (using external mode). I can even switch between channels with a manual switch that changes the I2C TX block's data command.
However, I am lost as to how to cycle between these channels periodically and see them both simultaneously. I've seen this example which uses function calls to sequence multiple TX blocks, but it still only uses one recieve block as it seems the data from both sensors comes at the same time. In my case I need to send a seperate command to the ADC for each channel (e.g. 255 for channel 8 and 223 for channel 4) and read the value sent by the ADC after each command. I tried doing a similar approach with function calls chaining together multiple TX and RX blocks in subsystems, but I could not get data from either channel when I tried that.
If someone has an example of something like this being done or any advice on what direction to take I would very much appreciate it. I am also curious how these blocks work (I am actually surprised that the given example works.) Does each block trigger repeatedly? If so is once per the sample rate or some other speed? Does the RX block wait for the TX block to complete before it tries to read and vice versa? Also, how does this change when you put the blocks in a subsystem? Does each one go once and then move on to the next subsytem or is there more to it?
I have attached files for both of my attempts, let me know if you see what is going wrong in the sequeced function call version. When I probe the sda line I can see some action, so it is doing something, but it's not quite there.
Thanks in advance,
said bensebaa on 23 Mar 2021
Hi Kyle Goodrick.
I wanted to know if you resolved this problem? if yes, could you please, tell me how to get data from different chanels without adding a switch and without changing Data lengh?
Please can you refer the example model ' c28x_i2c_eeprom_interrupt'. From this example model one can observe the use of C28x Hardware interrupt block. This block is used on the I2C Rx interrupt. Using this approach is more practical as there is a delay between I2C Tx and I2C Rx which depends on factor like I2C bus speed, number of bits to exchange and response time of the slave. So when using the Tx and Rx in back2back we need to consider this delay using a while loop before we can read the data. Otherwise, interrupts can be setup on the Rx so that we can do other work while the Master is receving the I2C data and let's know once that is complete. A few approach one can take for reading all the ADC channels using this scheme is to send the subsequent channel request from inside the ISR and capture the received data in seprate buffers. You can also the Rx FIFO to be able to read the data from all channels at onetime.
You may also look in to the implementation of the example model 'c28x_i2c_sensor' here we are sequencing the Tx and Rx operations such that in current execution step time we read data for the data request sent in previous step time. So we read data delayed by one sample time w.r.t the request that was sent to slave device for the data read. Please ensure the sample time duration is such that it gives enough time for the incoming data to reach Master before read.