Jump to content
43oh

32 I/O Port Expander Booster Pack ( MCP23S17 and MCP23S18 )


Recommended Posts

Excellent!  I will just need to install long pinned sockets, possibly a right angle connector for the 36 pin,  and it should sandwich perfectly between the launchpad and the iso-dmx board.  Looking to do 12-16 inputs and 12-16 outputs, so this is perfect.

Link to post
Share on other sites

Example code:

#include "msp430g2553.h"
#include "MCP23X18.h"

#define EXP_CLK_PIN BIT5	// P1.5 SPI clock
#define EXP_SOMI_PIN BIT6	// P1.6 SPI MISO
#define EXP_SIMO_PIN BIT7	// P1.7 SPI MOSI
#define EXP_CS_0_PIN BIT4	// P1.4 SPI chip 0 select
#define EXP_CS_1_PIN BIT0	// P1.0 SPI chip 1 select
#define EXP_INTA_0_PIN BIT1 // P2.1 INTA chip 0
#define EXP_INTB_0_PIN BIT0 // P2.0 INTB chip 0
#define EXP_INTA_1_PIN BIT4	// P2.4 INTA chip 1
#define EXP_INTB_1_PIN BIT5	// P2.5 INTB chip 1
#define EXP_SELECT_0 P1OUT &= ~EXP_CS_0_PIN
#define EXP_SELECT_1 P1OUT &= ~EXP_CS_1_PIN
#define EXP_DESELECT P1OUT |= EXP_CS_0_PIN | EXP_CS_1_PIN
#define EXP_DESELECT_0 P1OUT |= EXP_CS_0_PIN
#define EXP_DESELECT_1 P1OUT |= EXP_CS_1_PIN

// w & r do not select chip, use read0, read1, write0, and write1
void w(unsigned char registerAddr, unsigned char data);
unsigned char r(unsigned char registerAddr);
#define write0(registerAddr, data) EXP_SELECT_0; w(registerAddr, data)
#define write1(registerAddr, data) EXP_SELECT_1; w(registerAddr, data)
#define read0(registerAddr) EXP_SELECT_0; r(registerAddr)
#define read1(registerAddr) EXP_SELECT_1; r(registerAddr)

// used for examples only
unsigned char result = 0xFF;
unsigned char counter = 0;

void main(void) {

    WDTCTL = WDTPW + WDTHOLD;         // disable WDT
    BCSCTL1 = CALBC1_16MHZ;           // 16MHz clock
    DCOCTL = CALDCO_16MHZ;

    // configure CS pins
    P1OUT |= EXP_CS_0_PIN + EXP_CS_1_PIN;
    P1DIR |= EXP_CS_0_PIN + EXP_CS_1_PIN;
    // configure INT pins
    P2SEL &= ~(EXP_INTA_0_PIN + EXP_INTB_0_PIN + EXP_INTA_1_PIN + EXP_INTB_1_PIN);
    P2DIR &= ~(EXP_INTA_0_PIN + EXP_INTB_0_PIN + EXP_INTA_1_PIN + EXP_INTB_1_PIN);
    P2IES |= EXP_INTA_0_PIN + EXP_INTB_0_PIN + EXP_INTA_1_PIN + EXP_INTB_1_PIN;
    P2IFG &= ~(EXP_INTA_0_PIN + EXP_INTB_0_PIN + EXP_INTA_1_PIN + EXP_INTB_1_PIN);
    P2IE |= EXP_INTA_0_PIN + EXP_INTB_0_PIN + EXP_INTA_1_PIN + EXP_INTB_1_PIN;

    // configure USIB pins
    P1SEL |= EXP_CLK_PIN + EXP_SIMO_PIN + EXP_SOMI_PIN;
    P1SEL2 |= EXP_CLK_PIN + EXP_SIMO_PIN + EXP_SOMI_PIN;
    // setup USIB
    UCB0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC;  // 3-pin, 8-bit SPI master
    UCB0CTL1 |= UCSSEL_2;                     // SMCLK
    UCB0BR0 |= 0x01;                          // 1:1
    UCB0BR1 = 0;
    UCB0CTL1 &= ~UCSWRST;                     // clear SW

    // configure MCP23S18
    // chip 0
    write0(IODIRA0, 0);
    write0(IODIRB0, 0xFF); //default but it doesn't hurt
    write0(GPPUB0, 0xFF);
    write0(GPIOA0, 0x3C);
    write0(GPINTENB0, 0xFF);

    // enable interrupts
    _bis_SR_register(GIE);

    while(1) {
    	// example: copy counter to port A of chip 0 every 0.25us
    	_delay_cycles(4000000);
    	counter++;
    	write0(GPIOA0, counter);
    }
}


#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void) {
	// example: copy chip 0's port B to port A when change on port B is detected
    if(P2IFG & EXP_INTB_0_PIN) {
        result = read0(GPIOB0);
        write0(GPIOA0, result);
        P2IFG &= ~EXP_INTB_0_PIN;
    }
}

void w(unsigned char registerAddr, unsigned char data) {

    UCB0TXBUF = CONTROL_WRITE;
    while(!(IFG2 & UCB0TXIFG))
        ;
    UCB0TXBUF = registerAddr;
    while(!(IFG2 & UCB0TXIFG))
        ;
    UCB0TXBUF = data;
    while (UCB0STAT & UCBUSY);

    EXP_DESELECT;
}

unsigned char r(unsigned char registerAddr) {

    UCB0TXBUF = CONTROL_READ;
    while(!(IFG2 & UCB0TXIFG))
        ;
    UCB0TXBUF = registerAddr;
    while(!(IFG2 & UCB0TXIFG))
        ;
    UCB0TXBUF = 0;
    while (UCB0STAT & UCBUSY);

    EXP_DESELECT;

    return UCB0RXBUF;
}

MCP23X18.h

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...