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 support@ccsinfo.com

Help with MCP23S17

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
coblet



Joined: 04 Mar 2007
Posts: 3
Location: SAN SEBASTIAN

View user's profile Send private message

Help with MCP23S17
PostPosted: Sun Mar 04, 2007 3:19 pm     Reply with quote

Hello,

I am trying to use the MCP23S17 to add 16 input to my PIC 18F4580

The value read is always 0, not change. I donīt know what to do. Anybody could help me.
This is my code:

Code:

#include <18F4580.h>             
#fuses HS, NOPROTECT, NOLVP, NOWDT
#use delay(clock=20000000)     

#define CS_23S17_1                 PIN_C1
#define RESET_23S17_1            PIN_D0

void main()

   int8 data0,data1;
   output_low(RESET_23S17_1 );
   delay_us(10);
   output_high(RESET_23S17_1 );

    setup_spi(SPI_MASTER |SPI_H_TO_L| SPI_CLK_DIV_4);

   output_low(CS_23S17_1);
   spi_write(0x40); /// Opcode write mode
   spi_write(0x00); // WRITE IOCON   //IODIRA
   spi_write(0xff);// // set port A as inputs
   spi_write(0xff);// // set port B as inputs
   output_high(CS_23S17_1);  // disable I/O
   

   while(TRUE)
     {

   output_low(CS_23S17_1); //enable I/O expander
   spi_write(0x40); /// Opcode write mode
   spi_write(0x12); // SELECT GPIOA
   data0=spi_read(0);// read GPIOA
   data1=spi_read(0);// read GPIOB
   output_high(CS_23S17_1);  // disable I/O
     
   }

} // main






Thanks in advance
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Mar 04, 2007 5:55 pm     Reply with quote

I donīt have the MCP23S17 but I wrote this code if you want to test it.

Code:

#define IODIRA 0x00
#define IODIRB 0x01
#define GPIOA  0x12
#define GPIOB  0x13
   
   output_low(CS_23S17_1);
   spi_write(0x40);
   spi_write(IODIRA); // WRITE IOCON   
   spi_write(0xFF);     //
   output_high(CS_23S17_1);
   
   delay_ms(10);
   
   output_low(CS_23S17_1);
   spi_write(0x40);
   spi_write(IODIRB); // WRITE IOCON   
   spi_write(0xFF);     //
   output_high(CS_23S17_1);
 
   while(1)
     {
       output_low(CS_23S17_1);
       spi_write(0x41);    // READ OPCODE
       data0=spi_read(GPIOA); // read GPIOA
       data1=spi_read(GPIOB); // read GPIOB
       output_high(CS_23S17_1);
   
       printf("PA %x   PB %x\r\n", data0, data1);
       delay_ms(500);
     }


Humberto
coblet



Joined: 04 Mar 2007
Posts: 3
Location: SAN SEBASTIAN

View user's profile Send private message

MCP23S17 is now ok
PostPosted: Tue Mar 06, 2007 12:37 pm     Reply with quote

Thanks for your help,
Now the comunications with my MCP23S17 are ok.
The problem was the spi configuration.

With this program I can read 16 input from MCP23S17.

Code:


//---------------------------------------------------------------------------
// Includes

#include <18F4580.h>           

#fuses HS, NOPROTECT, NOLVP, NOWDT
//#fuses EC_IO, NOPROTECT, NOLVP,WDT16   fuses 18F458
#use delay(clock=20000000)
 

#define CS_23S17_1               PIN_C1

#define IODIRA 0x00
#define IODIRB 0x01
#define GPIOA  0x12
#define GPIOB  0x13

void lectura_IO_EXPANDER_1(void);

int8 i_GPIOA_MCP1;
int8 i_GPIOB_MCP1;


void main()

   output_high(CS_23S17_1);

 setup_spi(SPI_MASTER |0X4000| SPI_CLK_DIV_4);//16
  bit_set(*0xFC6, 5); // SSPCON1.SSPEN = 1
  bit_set(*0xFC7, 6); // SSPSTAT.CKE = 1   

   output_low(CS_23S17_1);
   spi_write(0x40);
   spi_write(IODIRA); // WRITE IOCON   
   spi_write(0xFF);     //
   output_high(CS_23S17_1);   
   delay_us(10);   

   output_low(CS_23S17_1);
   spi_write(0x40);
   spi_write(IODIRB); // WRITE IOCON   
   spi_write(0xFF);     //
   output_high(CS_23S17_1);

  while(TRUE)
     {

  lectura_IO_EXPANDER_1();

      delay_us(100);
   }

} // main


void lectura_IO_EXPANDER_1(void)
{
    output_low(CS_23S17_1);
    spi_write(0x41);    // READ OPCODE
    spi_write(0x12); // GPIOA SELECT GPIOA   
    i_GPIOA_MCP1=spi_read(0); // read GPIOA
    i_GPIOB_MCP1=spi_read(0); // read GPIOB
    output_high(CS_23S17_1);
}


Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Mar 06, 2007 12:42 pm     Reply with quote

Just curious, in your code I donīt see any code to watch the variables i_GPIOA_MCP1.
How do you "see" the inputs values ?


Humberto
coblet



Joined: 04 Mar 2007
Posts: 3
Location: SAN SEBASTIAN

View user's profile Send private message

variable i_GPIOA_MCP1
PostPosted: Tue Mar 06, 2007 1:03 pm     Reply with quote

I send this variables with RS232 protocol to my PC (in the PC I have one application with C++).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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