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

Read the input of MCP23S17 - Pic24f

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Read the input of MCP23S17 - Pic24f
PostPosted: Wed Sep 12, 2012 12:56 pm     Reply with quote

HI,

I'm working on a library for my project and i have a problem...
I'm trying to set the 16 I/O in input mode, and read them but nothing (Mode output no problem).

CCS : 4.128 / Pic 24Fj64GA104


Register and pinout :
Code:

//-- MCP23S17 --
#pin_select SCK1OUT   = PIN_C5    // Clock
#pin_select SDI1    = PIN_C3    // Data input
#pin_select SDO1      = PIN_c4   // Data output
#define      mcp_clk   pin_c5
#define      mcp_sdi   pin_c3
#define      mcp_sdo   pin_c4
#define      mcp_cs   pin_a4


#define write_mode   0x40
#define   read_mode   0x41

#define IOCON_reg   0x0A
#define   IODIR_reg   0x00   // Direction register          [ 1=input / 0=output ]
#define   IPOL_reg   0x02   // Input polarity            [ 0= same / 1=opposite ]
#define   GPPU_reg   0x0c   // Pull up resistor register   [ 1=pullup enable / 0=pullup disable ]
#define   GPIO_reg   0x12   // General purpose IO register
#define   OLAT_reg   0x14   // Output latch register      [ 1=logic_high / 0=logic_low ]
#define GPINTEN_reg   0x04    // Interrupt register         [ 1=enable / 0=disable]



My init function
Code:

void mcp_init()
{
   setup_spi(SPI_MASTER |SPI_L_TO_H | SPI_CLK_DIV_6); // Spi n°1 - Max 10Mhz

      mcp_write(IOCON_reg,0x40,0x40);    // config registre mode de communiquation

   // PortA & PortB en output
         //mcp_write(IODIR_reg,0x00,0x00);      
      //mcp_write(OLAT_reg,0x00,0x00);   
      //mcp_write(GPPU_reg,0x00,0x00);      
      //mcp_write(GPIO_reg,0xff,0xff);

   // PortA & PortB en input
      mcp_write(IODIR_reg,0xFF,0xFF);      // PortA et PortB en entrees
      mcp_write(IPOL_reg,0x00,0x00);      // valeur identique a l'etat de l'input
      mcp_write(GPINTEN_reg,0x00,0x00);   // Pas d'interruption
   
}



My Write / read function
Code:

//---------------------------------------------
void mcp_write(int8 adresse,int8 data_1,int8 data_2)
{
   output_low(mcp_cs);
      spi_write(write_mode);
      spi_write(adresse);
      spi_write(data_1);
      spi_write(data_2);   
   output_high(mcp_cs);
}
//----------------------------------------------
void mcp_read(int8 adresse)
{
   int8 data1;
   int8 data2;

   output_low(mcp_cs);
      spi_write(read_mode);
      spi_write(adresse);
      data1=spi_read(0);
      data2=spi_read(0);
   output_high(mcp_cs);
}
//----------------------------------------------


For read my input, I call the the function mcp_read() with GPIO_reg parameter.

If someone sees a terrible mistake !?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 12, 2012 4:37 pm     Reply with quote

Quote:
setup_spi(SPI_MASTER |SPI_L_TO_H | SPI_CLK_DIV_6); // Spi n°1 - Max 10Mhz


Look at the SPI mode #define statements for the PIC24FJ-series by FvM
at the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=38491&start=34

Look at Figure 2-5 in the MCP23S17 data sheet, on page 32:
http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
It shows that SPI mode 0,0 or mode 1,1 can be used with this chip.

I don't know what mode you have in your setup_spi() statement. But
you need to fix it, according to the two links I gave above.
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