View previous topic :: View next topic |
Author |
Message |
coblet
Joined: 04 Mar 2007 Posts: 3 Location: SAN SEBASTIAN
|
Help with MCP23S17 |
Posted: Sun Mar 04, 2007 3:19 pm |
|
|
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
|
|
Posted: Sun Mar 04, 2007 5:55 pm |
|
|
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
|
MCP23S17 is now ok |
Posted: Tue Mar 06, 2007 12:37 pm |
|
|
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
|
|
Posted: Tue Mar 06, 2007 12:42 pm |
|
|
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
|
variable i_GPIOA_MCP1 |
Posted: Tue Mar 06, 2007 1:03 pm |
|
|
I send this variables with RS232 protocol to my PC (in the PC I have one application with C++). |
|
|
|