CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

MCP23S17 16 Bit SPI IO Expander Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

MCP23S17 16 Bit SPI IO Expander Driver
PostPosted: Wed Oct 02, 2013 10:13 am     Reply with quote

MCP23S17.h
Code:

#use SPI (MASTER, DO = PIN_B1, DI = PIN_B3, CLK = PIN_B0, BITS = 8, Mode = 0)


#define IO_DEVICE_ADDRESS_WRITE   0x40
#define IO_DEVICE_ADDRESS_READ    0x41

#define IODIRA        0x00      //Data Direction Register for PORTA   
#define IODIRB        0x01      //Data Direction Register for PORTB
#define IPOLA         0x02      //Input Polarity Register for PORTA
#define IPOLB         0x03      //Input Polarity Register for PORTB
#define GPINTENA      0x04      //Interrupt-on-change enable Register for PORTA
#define GPINTENB      0x05      //Interrupt-on-change enable Register for PORTB
#define DEFVALA       0x06      //Default Value Register for PORTA   
#define DEFVALB       0x07      //Default Value Register for PORTB     
#define INTCONA       0x08      //Interrupt-on-change control Register for PORTA 
#define INTCONB       0x09      //Interrupt-on-change control Register for PORTB     
#define IOCON         0x0A      //Configuration register for device                     
#define GPPUA         0x0C      //100kOhm pullup resistor register for PORTA (sets pin to input when set)   
#define GPPUB         0x0D      //100kOhm pullup resistor register for PORTB (sets pin to input when set)     
#define INTFA         0x0E      //Interrupt flag Register for PORTA       
#define INTFB         0x0F      //Interrupt flag Register for PORTB   
#define INTCAPA       0x10      //Interrupt captured value Register for PORTA 
#define INTCAPB       0x11      //Interrupt captured value Register for PORTB   
#define GPIOA         0x12      //General purpose I/O Register for PORTA 
#define GPIOB         0x13      //General purpose I/O Register for PORTB
#define OLATA         0x14      //Output latch Register for PORTA
#define OLATB         0x15      //Output latch Register for PORTB 

#define CS           pin_B2

                                                                         
void setup_MCP23S17();                           
void MCP23S17_write(unsigned char address, unsigned char value);         
unsigned char MCP23S17_read(unsigned char address); 


MCP23S17.c

Code:

#include "MCP23S17.h" 


void setup_MCP23S17()
{                                                 
   MCP23S17_write(IOCON, 0x18);
   MCP23S17_write(IODIRA, 0x00); 
   MCP23S17_write(IODIRB, 0xFF); 
   MCP23S17_write(GPPUA, 0x00); 
   MCP23S17_write(GPPUB, 0xFF);
   MCP23S17_write(IPOLA, 0xFF);   
   MCP23S17_write(IPOLB, 0x00);
}                                                                   
                                                       
                                                 
void MCP23S17_write(unsigned char address, unsigned char value)
{                                                 
    output_low(CS);
    spi_xfer(IO_DEVICE_ADDRESS_WRITE);
    spi_xfer(address);                           
    spi_xfer(value);
    output_high(CS);                 
}                                                 
                                                 
                                                 
unsigned char MCP23S17_read(unsigned char address)                 
{                                                 
    unsigned char received_data = 0;
    output_low(CS);
    spi_xfer(IO_DEVICE_ADDRESS_READ);           
    spi_xfer(address);                           
    received_data = spi_xfer(0); 
    output_high(CS);         
    return received_data;                                 
}


Example
Code:

#include <10F200.h>                                     
#fuses PROTECT, NOWDT, NOMCLR         
#use delay (clock = 4MHz)                                                 
                                                   
#include "MCP23S17.c"
                                                 
void setup();                                                 
                                                                             
                                                 
void main()                                       
{                                                 
    unsigned char in = 0;
    setup();
           
    while(TRUE)                                   
    {                 
      in = MCP23S17_read(GPIOB);
      MCP23S17_write(OLATA, in);
    }                                                                   
}                                                 
                                                 
                                                 
void setup()                                     
{                                                                           
    setup_MCP23S17();                             
}                                                 
       

_________________
https://www.facebook.com/MicroArena

SShahryiar
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

Demo
PostPosted: Wed Oct 02, 2013 11:19 am     Reply with quote

https://www.facebook.com/photo.php?fbid=414425551990900&set=pb.113572212076237.-2207520000.1380734269.&type=3&theater
_________________
https://www.facebook.com/MicroArena

SShahryiar
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group