LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

NI-9402 I2C - cannot write multiple bytes

Hello,

 

I'm using the NI-9402 to communicate to a sensor using I2C, and I'm having trouble writing 8 bytes to the sensor.  I'm attaching my block diagram VI.

 

For this I2C communication, what I want to do is to write:

Slave Address 4C; Write = 0; ACK | 0x0D ; ACK | 0x40; ACK | Repeated Start |

Slave Address 4C; Write = 0; ACK | 0x0F; ACK | 0x42; ACK; Repeated Start |

Slave Address 4C; Write = 0; ACK | 0x20 ; ACK | 0x01; ACK | Repeated Start |

Slave Address 4C; Write = 0; ACK | 0x21; ACK | 0x08; ACK; STOP

 

However, with the code that I have, I am seeing on the scope:

Slave Address 4C; Write = 0; ACK | 0x0D ; ACK | 0x40; ACK | Repeated Start |

Slave Address 4C; Write = 0; ACK | 0x0F; ACK | 0x42; ACK; STOP

 

If I stop the program and run again, I see the other two commands:

Slave Address 4C; Write = 0; ACK | 0x20 ; ACK | 0x01; ACK | Repeated Start |

Slave Address 4C; Write = 0; ACK | 0x21; ACK | 0x80; ACK; STOP

 

I am not able to write 8 bytes of data. I only see either the first 4 bytes or the last 4 bytes. How do I fix this issue? Any help would be appreciated. Thanks.

0 Kudos
Message 1 of 10
(3,447 Views)

Please post VIs not pictures

CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
0 Kudos
Message 2 of 10
(3,412 Views)

Is there a specific reason you are using the 9402?  If on a PC, it would be A LOT simpler to just use an USB-845X device.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 10
(3,394 Views)

Hello, I'm attaching the VI below.  

 

I'm using the cRIO-9039 controller, and the NI-9402 is a module that works with it, so that's why I'm using the NI-9402.

 

Thanks. Any help would be appreciated.

0 Kudos
Message 4 of 10
(3,386 Views)

Amy4082001,

 

Have you tried probing the arrays that go into the For Loops that are writing the data to the I2C functions, and check if you see the same behavior? First portion of the data going in on the first run, and the second portion of the data in the second run?

 

I would recommend you tackle the problem step by step. Meaning, try sending just one message. If that succeeds, try sending two... and so on. It's definitely easier to understand which the problem might be, than trying to solve a four commands puzzle in one shot.

 

All the best,

0 Kudos
Message 5 of 10
(3,333 Views)

Yes, I tried probing, but I'm running in an FPGA/ cRIO-9039 environment, and there's no light bulb to troubleshoot and the probing doesn't seem to work (see the attached pic 1 for what the probing says).  The only way for me to troubleshoot is to connect an oscilloscope to the I2C pins.

 

So far, I've been able to:

  1. Write a command (slave address + register address + data) 
  2. Write a command and read back that this command has been written
  3. Write two commands (slave address + register address + data / start stop / slave address + register address + data)
  4. Write multiple data (slave address + register address + data1 + data2 + ...+data_n) for two commands

What I would like to do is:

  1. Be able to write infinite commands, separated by a repeated start (or start stop), but haven't been able to. 
0 Kudos
Message 6 of 10
(3,328 Views)

OK...

Initial thoughts (sorry I haven't installed LV 2017 yet so I haven't been able to open the VI you posted) are.

You have a simple process:

1) Initialize I2C Port

2) Configure Port

3) Write Port (Loop N Times)

4) Run ARM Port Function

5) Loop back to Step 2 (M times)

6) Read Port (P times)

 

I would suggest:

a)  making the Configure, looped write and Arm Port function into a subVI for your purposes and add a cluster with only the inpus that you intend to

change.

b) ensuring the I2C reference cluster going through the FOR loop passes round a Shift register.

c) handling errors between calling the subVI, and then running the subVI more times in a for loop (There is no need for all those locals on the clock divider, in fact if you change the input clock divider2, this could be the root cause of you issue.

d) ensuring the IP Port.vi has been run (by flow control) and defined the clock before all of the Configure Ports run (in case that is causing a subtle issue.)

e) checking that you are leaving enough time between sending commands and not encountering an unresponsive/ flow control related issue. (You may just be filling the bus and not allowing time for it to empty before attempting to send the next message.)

 

James

 

******* Edit: Scratch the subVI if you are not reconfiguring everytime anymore **************************

CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
0 Kudos
Message 7 of 10
(3,320 Views)

Hi James,

 

Thanks for your input.  Please note that I am able to write 2 commands so fundamentally, I know the VI is somewhat working (see attached scope pic for the 2 commands that I am seeing). However, the question still is – how do I write more than 2 commands?

 

I did try your suggestion (e) by:

  1. Adding a wait time (in ticks).  I waited 1 ticks, 100 ticks, 1000 ticks, 10k ticks, and I’m still only able to see that 2 commands have been written. 
  2. I also increased/decreased the clock.  I executed the I2C clock at 100Hz, 1kHz, 100kHz, and again, I’m only able to see that 2 commands have been written.
0 Kudos
Message 8 of 10
(3,281 Views)

Hi Amy,

 

Isn't your problem really simple then...

You are sending message n+1 before you've sent the stop for message n. This is confusing the I2C bus and returning the results which you are seeing. If you handle the messages as complete packets from send to stop, you should be fine.

(I don't think you've put your time delay in the correct place) - but I only normally do simple single packet I2C comms when needed.

This seems like it might help:

http://maxembedded.com/2014/02/inter-integrated-circuits-i2c-basics/

 

But equally you look like you've already read up and covered lots of this already so it's unlikely to be a simple mistake. Sorry if I can't help any further.

 

James

CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
0 Kudos
Message 9 of 10
(3,266 Views)

Hello, for completeness, I want to post up my solution (see attached pic) for writing multiple commands from the NI-9402 to a slave (mine was the MC3672).  I am still having trouble reading from the slave, but I'll post that in another forum. 

0 Kudos
Message 10 of 10
(3,157 Views)