|
|
View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
Read the input of MCP23S17 - Pic24f |
Posted: Wed Sep 12, 2012 12:56 pm |
|
|
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
|
|
|
|
|
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
|