The Godfather talking
You may crack software. How about me?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 24, 2024, 08:20:48 08:20


Login with username, password and session length


Pages: [1]
Print
Author Topic: MCP23s08 help ?  (Read 4842 times)
0 Members and 1 Guest are viewing this topic.
dikris
Active Member
***
Offline Offline

Posts: 200

Thank You
-Given: 290
-Receive: 69


« on: May 18, 2006, 09:49:04 09:49 »

Hi all,
 
I have CCS project with PIC18f452 and 8 mcp23s08.I want to make  4 of them inputs with pull-up and another 4 - Outputs.I use hardware addressing pin(A0,A1) . Inputs and otputs have separately CS.
Can you advice me how to init those chips.When I send command to outputs all 4 chips recive same information.Any driver idea.
 
I wrote this driver:
 
void init_SPI()
{
output_low(SPI_CLK);
 delay_us(50);
output_low(SPI_DI);
output_high(OUTPUTS_CS);
output_high(INPUTS_CS);
   delay_us(50);
}
void mcp32s08_SendByte(BYTE data)
{
   BYTE i;
 delay_us(2);
  for(i=0; i<8; ++i)  //SEND DATA
 {
   if(bit_test(data, 7))
   output_high(SPI_DI);
  else      
   output_low(SPI_DI);
     // data=data>>1;
      delay_us(60);
      output_high(SPI_CLK);
      delay_us(40);
    output_low(SPI_CLK);
    data <<= 1;
   }
}

void mcp32s08_write(BYTE address,BYTE reg,BYTE value,BYTE io)
{
  output_low(SPI_CLK);
  output_low(SPI_DI);
if(io==0)                 //outputs
output_low(OUTPUTS_CS);
else                        //inputs
output_low(INPUTS_CS);

 mcp32s08_SendByte(address);  //SEND OP CODE (ADDRESS)
 mcp32s08_SendByte(reg); //SEND REGISTER
 mcp32s08_SendByte(value);  //SEND VALUE
 init_SPI();
}

void mcp32s08_SetPulUp(BYTE address)
{
 address=address<<1;
 address=0x40+address;
mcp32s08_write(address,MCP23s08_GPPU,0xFF,1);
}
 
void mcp32s08_SetIOCON(BYTE address,BYTE dir)
{
 address=address<<1;
 address=0x40+address;
mcp32s08_write(address,MCP23s08_IOCON,0x04,dir);
}
 
void mcp32s08_Out(BYTE address,BYTE value)
{
 address=address<<1;
 address=0x40+address;
mcp32s08_write(address,MCP23s08_GPIO,value,0);
}

void mcp32s08_init(BYTE address,BYTE dir)
{
BYTE value;
 address=address<<1;
 address=0x40+address;
if(dir==1)
 value=0xFF;
else
 value=0x00;
mcp32s08_write(address,MCP23s08_IODIR,value,dir);
}
 
 
And the code of init is:
 
 for(i=0; i<4; ++i)  //INPUTS
 {
mcp32s08_SetIOCON(i,1);
mcp32s08_SetPulUp(i);  //Pull Up - Internal
mcp32s08_init(i,1);  
 }
  for(i=0; i<4; ++i)  //OUTS
 {
  mcp32s08_SetIOCON(i,0);
  mcp32s08_init(i,0);
  mcp32s08_Out(i,0x00);   //All Outs '0'
 }
 
  mcp32s08_Out(0,0x01); //Here is the problem

After this bit 0 of all outs is 1, not 1 on first chip only.
 
Sorry of my English.
Logged
bbarney
Moderator
Hero Member
*****
Offline Offline

Posts: 2430

Thank You
-Given: 405
-Receive: 545


Uhm? where did pickit put my mute button


« Reply #1 on: May 18, 2006, 11:17:46 11:17 »

maybe this will help
Logged

Ever wonder why Kamikaze pilot's wore helmet's ?
dikris
Active Member
***
Offline Offline

Posts: 200

Thank You
-Given: 290
-Receive: 69


« Reply #2 on: May 18, 2006, 11:58:05 11:58 »

The book describe I2C. MCP23s08 -> SPI, MCP23008 -> I2C.
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #3 on: May 18, 2006, 03:23:57 15:23 »

I can't find any significant error in Yr. code, but if I were You I should fiddle a little bit with the delay routines(timining can be critical when talking to external modules). I assume You have connected the hardware and pullup resistors OK.

Good Luck!:)
Logged

Code Warrior
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #4 on: May 18, 2006, 03:29:55 15:29 »

You wrote:
mcp32s08_write(address,MCP23s08_IOCON,0x04,dir);
 
The rest of the code constitute "dir" as "BYTE dir". By the way, when You send us code, don't be sparse with comments and line numbers.
Logged

Code Warrior
dikris
Active Member
***
Offline Offline

Posts: 200

Thank You
-Given: 290
-Receive: 69


« Reply #5 on: May 18, 2006, 04:46:09 16:46 »

Thanks a lot.
 
void mcp32s08_SetIOCON(BYTE address,BYTE dir) //dir mean INPUTS-1 and OUTPUT - 0
{
address=address<<1;
address=0x40+address;
//mcp32s08_write(address,MCP23s08_IOCON,0x04,dir); //WRONG VALUE - 0x04
 
 // HAEN(hardware address enable) is bit 3  ->  0x08 from IOCON Register
                                                                   
mcp32s08_write(address,MCP23s08_IOCON,0x08,dir); //CORRECT VALUE
}
Logged
dikris
Active Member
***
Offline Offline

Posts: 200

Thank You
-Given: 290
-Receive: 69


« Reply #6 on: May 18, 2006, 05:17:59 17:17 »

All registers from one of the MCP23s08(inputs) via RS232

IODIR=FF
IPOL=00
GPINTEN=00
DEFVAL=00
INTCON=00
IOCON=08
GPPU=00
INTF=00
INTCAP=00
GPIO=FF
OLAT=00

Logged
bbarney
Moderator
Hero Member
*****
Offline Offline

Posts: 2430

Thank You
-Given: 405
-Receive: 545


Uhm? where did pickit put my mute button


« Reply #7 on: May 18, 2006, 10:28:20 22:28 »

sorry about that i just woke up when i read your post
Logged

Ever wonder why Kamikaze pilot's wore helmet's ?
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #8 on: May 19, 2006, 09:17:58 21:17 »

Hi Dikris!
Glad You made it...
Logged

Code Warrior
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC