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

MCP23016 Babbles addresses other than 001

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



Joined: 25 Apr 2007
Posts: 23

View user's profile Send private message

MCP23016 Babbles addresses other than 001
PostPosted: Wed Dec 05, 2007 3:24 pm     Reply with quote

Hello:

This one has kept me up a week of nights.

Using a PIC18F452 with three MCP23016 port expanders. Using hardware I2C ports SCLK (RC3) and SDA (RC4). Pull-ups on both line to +5V. Only three MCP23016 on bus, nothing else. Have three MCP23016 hardwired so that their addresses are 000b 001b and 010b.

I only write to the expanders no read requirement

Using v3.249 PCWH to compile and ICS-40U debugger

Code is shown below

Addressing device 000b works fine. However, addressing either 001b or 010b devices results in no initialization and, of course, no output. The outputs of the expanders are floating around 3.5 volts after writing 0x00 to them.

I examine the I2C bus to reveal a considerable amount of babbling going on as I try and initialize either the 001b or 010b device. I have some nice logic analyser pictures if anyone is interested. Both clock and SDA lines are toggling wildly as I attempt to initialize either the 001b or 010b device.

I'm at a loss. Any help would be much appreciated!

Regards

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS,stream=PC)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)

//For I2C Port Expanders

// COMMAND BYTE
#define GP0 0x00
#define GP1 0x01
#define OLAT0 0x02
#define OLAT1 0x03
#define IPOL0 0x04
#define IPOL1 0x05
#define IODIR0 0x06
#define IODIR1 0x07
#define INTCAP0 0x08
#define INTCAP1 0x09
#define IOCON0 0x0A
#define IOCON1 0x0B


//MCP23016 I2C Routines

void write_to_MCP(unsigned char WriteAddress, unsigned char cmdByte,
unsigned char Data)
{
i2c_start();
delay_us(20);
i2c_write(WriteAddress);
delay_us(20);
i2c_write(cmdByte);
delay_us(20);
i2c_write(Data);
delay_us(20);
i2c_stop();
delay_us(50);
}



void init_mcp23016(unsigned char MCP23016_Device_Address)
{

// Set-up selected I/O expander unit

write_to_MCP(MCP23016_Device_Address, IPOL0, 0x00);
write_to_MCP(MCP23016_Device_Address, IPOL1, 0x00);

write_to_MCP(MCP23016_Device_Address, OLAT0, 0x00);
write_to_MCP(MCP23016_Device_Address, OLAT1, 0x00);

write_to_MCP(MCP23016_Device_Address, IODIR0, 0x00);
write_to_MCP(MCP23016_Device_Address, IODIR1, 0x00);
}
//==============================================================================

//=====================CALLING ROUTINE TO INITIALIZE
Init_All_Outputs()
{
ADR0_GP0= 0X00;
ADR0_GP1= 0X00;

init_mcp23016(0x40);
write_to_MCP(0x40, OLAT0, ADR0_GP0);
write_to_MCP(0x40, OLAT1, ADR0_GP1);
Delay_ms(10);

init_mcp23016(0x42);
write_to_MCP(0x42, OLAT0, ADR1_GP0);
write_to_MCP(0x42, OLAT1, ADR1_GP1);
Delay_ms(10);
}

[/img]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 05, 2007 3:35 pm     Reply with quote

All examples in the data sheet show two data bytes. The order is:
Quote:
Slave address
Command byte
Data byte 1
Data byte 2

Your write routine, write_to_MCP(), only writes one data byte.
I suggest you change it to write two bytes.
Jerry R



Joined: 25 Apr 2007
Posts: 23

View user's profile Send private message

SOLVED!
PostPosted: Wed Dec 05, 2007 7:39 pm     Reply with quote

PCM Programmer: Thanks for your reply. A wake-up call regarding using other's code without fully understanding it. I took this afternoon/evening to re-write the entire routines, understanding each step and PRESTO!

Thanks for the slap!
Regards
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