LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

MCP23017, I2C LabView HELP PLEASE.

Solved!
Go to solution
Highlighted

Hello,

 

So at work the goal is to send I2C commands to a Port Expander (PN:MCP23017) but I am completely lost also semi-new to LabView anyways. I understand at a high level what needs to happen in order to write to the Port Expander. Datasheet says I need to first write to it and "Define I/O Direction" then after this is defined I can write to send the actual data.

 

I downloaded the NI-845x drivers and I have all the I2C vi's available. I'm just so lost on how to actually code this and write anything. I feel like I'm missing an extra write step for defining the I/O direction but I don't know. I keep getting the Error -301742 which says "The slave did not acknowledge an address+direction byte transmitted by the I2C master."

 

 

The following is my attempt so far....

Vi.PNG

 

 
 

 

 

Download All
0 Kudos
Message 1 of 20
(378 Views)
Highlighted

Too bad you're not using a SPI chip.  I got introduced to SPI and the 8452 about eight months ago, even used a Port Expander chip (MCP23S17, same as yours but SPI-enabled).  Knew nothing about SPI at the time, fortunately had a colleague who "knew more than I" (not that difficult!) who'd made a first pass and got a crude (messy) version of code to work.  I basically "cleaned up" his code, asked some Engineers to explain how the Chip worked, and went to town.  [Hint -- set up Bank 0 addressing, makes life so much simpler].

 

     For what it is worth, here's how I configured the Chip:

  • Set up the Chip with two commands:
    • IOCON, I/O control, Bank 0, multi-byte I/O  (0x40, 0x0A, 0x00)
    • IODIR, I/O direction (R/W), all write (0x40, 0x00, 0x00, 0x00)
  • To set the Register, it differs if you are setting it with 16 bits (word write) or only 8 (byte write)
  • To set a Word, write both bytes to OLATA (0x40, 0x14, 0xnn (Lo), 0xnn (Hi)
  • To set a Byte, write to OLATA or OLATB, as needed (0x40, 0x14 or 0x15, 0xnn)

You set up the Chip once, then just use Word or Byte writes, as required.  Note that you need to tell the 8452 which SPI chip you are trying to talk to, i.e. you need code to do the Chip Select, but you probably already have that figure out.

 

For what it is worth, I'm now doing all the SPI stuff in an FPGA (also very new to me) -- very interesting, and a lot more fun.

 

Bob Schor

 

P.S. -- I just looked at the I2C examples that ship with LabVIEW.  It looks like (if you are only doing Data Out, i.e. setting the Port Expander bits, not using it for input) that General I2C Write has both the (one-time, usually) "initialization" code (where you use all of the blocks) and the later "Write Data to the Register" (where you only use part of the last major block, assuming you've not closed the Reference to the SPI Master and its Slave (the first two blocks).

Message 2 of 20
(290 Views)
Highlighted

@GeneralDynamic wrote:

The following is my attempt so far....


It looks like you are on the right track.  As you said, you need to set the port to be an output (defaults to input).  Here's what I put together really quick.

NOTE: I barely skimmed the data sheet and have not used this chip.  So no promises it works.

 

Something you might want to consider is making a VI to write to a Register.  It can put the register and value into an array and write the I2C message.  I have something similar hiding in my libraries.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Download All
Message 3 of 20
(269 Views)
Highlighted

Oh so you think I might have to use 2 write blocks: 1 for defining I/O direction and the other for data.

 

How come you used different control types for each write?

0 Kudos
Message 4 of 20
(238 Views)
Highlighted

What do you mean the I2C example that ships with LabVIEW? How can I get this?

 

 

0 Kudos
Message 5 of 20
(237 Views)
Highlighted

@GeneralDynamic wrote:

How come you used different control types for each write?


For the first one, I hardcoded the direction to be output.  For the second one, I was writing a desired value.  So basically it was two different situations, so the way I built up the array was different.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
0 Kudos
Message 6 of 20
(227 Views)
Highlighted

I just keep getting an error, I feel like I'm so close. Thanks for the help though, I know its hard to troubleshoot this with me when you cant physically see my setup.

 

Capture.PNG

 

Captujre.PNG

0 Kudos
Message 7 of 20
(224 Views)
Highlighted

@GeneralDynamic wrote:

I just keep getting an error, I feel like I'm so close. Thanks for the help though, I know its hard to troubleshoot this with me when you cant physically see my setup.


The error claims in invalid setting.  What values values are you using for the device address, address size, and clock rate?


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
0 Kudos
Message 8 of 20
(222 Views)
Highlighted

Maybe it's easier if you just open up my code? If not let me know.

0 Kudos
Message 9 of 20
(217 Views)
Highlighted

Clock Rate is default to 0.  That is likely your problem.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
0 Kudos
Message 10 of 20
(211 Views)