Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Mcp23s17 (SPI) with NP2+


  • Please log in to reply
4 replies to this topic

#1 Xtrophic

Xtrophic

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationSweden

Posted 31 January 2013 - 12:07 AM

Hi,

 

I am trying to get an Mcp23s17 interfaced with my NP2 to eventually drive an LCD screen.

 

I am not having much success getting the Mcp23s17 to work. The first simple scenario I was thinking was to just light up a simple LED using one of the GPIOs on the Mcp. 

 

I have updated to the latest firmware in hopes that there were some issues there.

 

I am attaching a pic of how it's wired, in case something is wrong there (but I hope not).

 

I have modified the MFToolbox code for Mcp23017 to basicly use SPI instead, and I have tried manually just sending the commands to the device via SPI write. But so far no luck what so ever.

 

When using SPI directly:

            spiConfig = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, false, true, 1000, SPI_Devices.SPI1);            Microsoft.SPOT.Hardware.SPI spi = new SPI(spiConfig);            spi.Write(new byte[] { 0x40 });     // Adress            spi.Write(new byte[] { 0x00, 0x00 });   // Set bank A to output            spi.Write(new byte[] { 0x01, 0x00 });   // Set bank B to output            spi.Write(new byte[] { 0x12, 0xff });   // Set bank A outputs to high            spi.Write(new byte[] { 0x13, 0xff });   // Set bank B outputs to high            spi.Dispose();

 

Or when using the quickly modified mcp23017 code:

            spiConfig = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, false, true, 1000, SPI_Devices.SPI1);            var mux = new Toolbox.NETMF.Hardware.Mcp23s17(spiConfig);            for (int i = 0; i < 16; i++) {                mux.Pins[i].Write(true);            }

 

 

Any help or suggestion on the matter would be appreciated :)

Attached Files



#2 Xtrophic

Xtrophic

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationSweden

Posted 31 January 2013 - 08:02 PM

It was missing a 10k ohm pullup on the reset pin, plus the code was a bit off. But now I got a simple command working so I will continue to tune the class to get it complete :) Once it's complete I'll share the code.



#3 Xtrophic

Xtrophic

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationSweden

Posted 01 February 2013 - 07:49 PM

So one step done, in that I can sucessfully set pin outputs to values. But when I attempt to query the device for information I do not get anything back.

 

Here is the code I am using: 

            spiConfig = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, false, true, 10000, SPI_Devices.SPI1);            Microsoft.SPOT.Hardware.SPI spi = new SPI(spiConfig);            spi.Write(new byte[] { 0x40, 0x00, 0x00 });   // Set bank A to output            spi.Write(new byte[] { 0x40, 0x01, 0x00 });   // Set bank B to output            spi.Write(new byte[] { 0x40, 0x12, 0x0f });   // Set bank A outputs to high            spi.Write(new byte[] { 0x40, 0x13, 0xff });   // Set bank B outputs to high            byte[] readBuffer = new byte[1];            byte[] readBuffer2 = new byte[1];            spi.WriteRead(new byte[] { 0x41, 0x14 }, readBuffer, 1);    // Attempt to query OLATA            spi.WriteRead(new byte[] { 0x41, 0x12 }, readBuffer2, 1);   // Attempt to query GPIOA            // buffer & buffer2 are still 0 or if I run it at 1MHz it's 255 ...            Debug.Print("");            spi.Dispose();

 

Am I doing something wrong in the code, or I am still missing a pullup somewhere? Any suggestions on how to proceed would be appreciated! :)



#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 02 July 2013 - 02:01 PM

Hi, did you get this driver working eventually?



#5 k_madsen

k_madsen

    Member

  • Members
  • PipPip
  • 22 posts
  • LocationEsbjerg Denmark

Posted 30 July 2014 - 08:13 AM

Hi

This simple code works with no interrupt and porta as out and port b as in adresse is 64 write and 65 read (all low)

 

setRegister(register , value);

byte readRegister(register);

 

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
//using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
namespace addonkm
{
    class MCP23S17
    {
        const byte IODIRA = 0x00; //iodirektion register 1 = in  0 = out default = 1
        const byte IODIRB = 0x10;
        const byte IPOLA = 0x01; // invert pin om port   1 = invert  default = 0
        const byte IPOLB = 0x11;
        const byte GPINTNA = 0x02; //int on change 1 = aktiv  default = 0
        const byte GPINTNB = 0x12;
        const byte DEFVALA = 0x03; //def comp reg on chage ved int on def change der genereres et int hvis 1 bit ikke har samme polaritet som dette register hvis enablet(intcon) 
        const byte DEFVALB = 0x13;
        const byte INTCONA = 0x04; // int control register  1 = compare with DEFVALA 0 = compare with last pin value
        const byte INTCONB = 0x14;
        const byte IOCON = 0x05;  // io expander config register bit 7 = bank mirror seqop disslw haen odr intpol 0
        const byte GPPUA = 0x06;    // pullup ressistors
        const byte GPPUB = 0x16;
        const byte INTFA = 0x07;    //int flag register 1 = pin that coursed the interrupt
        const byte INTFB = 0x17;
        const byte INTCAPA = 0x08; // interrupt captured value
        const byte INTCAPB = 0x18;
        const byte GPIOA = 0x09;  //Port register read port value read 
        const byte GPIOB = 0x19;
        const byte OLATA = 0x0a;    // output latch register   write
        const byte OLATB = 0x1a;
 
        private byte adresse;
        private byte adrread;
 
        static SPI.Configuration spiNormal = new SPI.Configuration(Pins.GPIO_PIN_D7,
                                                                    false,
                                                                    0,
                                                                    0,
                                                                    false,
                                                                    true,
                                                                    10000,
                                                                    SPI_Devices.SPI1
                                                                    );
 
        SPI SPIBus = new SPI(spiNormal);
 
        public void setRegister(byte register, byte value)
        {
            byte[] buff = new byte[3];
            buff[0] = adresse;
            buff[1] = register;
            buff[2] = value;
            SPIBus.Write(buff);
        }
 
        public byte readRegister(byte register)
        {
            byte[] buff = new byte[3];
            byte[] readbuff = new byte[3];
 
            buff[0] = adrread;
            buff[1] = register;
            buff[2] = 0; //spi dummy
            SPIBus.WriteRead(buff, readbuff);
            return readbuff[2];
        }
 
        private MCP23S17()
        {
        }
 
        public MCP23S17(byte adress)
        {
            adresse = (byte)(adress << 1);
            adrread = (byte)(adresse | 1);
 
            //saet port a til out og port b ind
            setRegister(IODIRA, 0); // alle bit out
            setRegister(GPPUA , 0xff); // alle pull op trans on
            setRegister(GPPUB, 0xff);
            setRegister(IOCON, 0xe2);
        }
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.