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

I²C and Streams

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



Joined: 14 Nov 2006
Posts: 5

View user's profile Send private message

I²C and Streams
PostPosted: Thu Nov 16, 2006 12:17 pm     Reply with quote

I have 2 Smart Batteries in my system and I need to communicate with them through I²C. Because they have the same address I need to have separate pins for each battery. I did this on another project a couple of years ago and I had to write 2 copies of all my functions.

They started out like this:
Code:
#use i2c( Master, sda=PIN_B2, scl=PIN_B1 )
#separate
unsigned long ReadLongRegister_B( int &BatteryRegister ) {
unsigned int   LowByte, HighByte;
const      int   SMBusBatteryWrite   = 0x16;
const      int   SMBusBatteryRead   = 0x17;

   i2c_start();
   i2c_write(SMBusBatteryWrite);
   i2c_write(BatteryRegister);
   i2c_start();

Followed by the next one:
Code:
#use i2c( Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW )
#separate
unsigned long ReadLongRegister_A( int &BatteryRegister ) {
unsigned int   LowByte, HighByte;
const      int   SMBusBatteryWrite   = 0x16;
const      int   SMBusBatteryRead   = 0x17;

   i2c_start();
   i2c_write(SMBusBatteryWrite);
   i2c_write(BatteryRegister);
   i2c_start();

I would really like to use the StreamID option and simplify things but I can't seem to get it right. I am rusty, it's been over 2 years since I did any serious programming and I'm just now getting spun back up.

Here's how I tried using streams:
Code:
unsigned long ReadLongSMBusRegister( const int MyStreamID, int &BatteryRegister ) {
unsigned int   LowByte, HighByte;
const      int   SMBusBatteryWrite   = 0x16;
const      int   SMBusBatteryRead   = 0x17;

   i2c_start(MyStreamID);
   i2c_write(MyStreamID, SMBusBatteryWrite);
   i2c_write(MyStreamID, BatteryRegister);
   i2c_start(MyStreamID);

but the compiler doesn't like me passing a constant.

I think I'm stuck with writing 2 sets of functions I just don't have to include the #use for each one, otherwise it's the exact same thing.

Comments? Help? Flames?

David
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 16, 2006 1:35 pm     Reply with quote

What is your compiler version ?
David_K



Joined: 14 Nov 2006
Posts: 5

View user's profile Send private message

PostPosted: Thu Nov 16, 2006 2:04 pm     Reply with quote

I'm using 4.014. The part is a 18F4685. I'm using software I²C.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Nov 16, 2006 3:06 pm     Reply with quote

I've had to use multiple I2C devices, that had the same address, on the same bus before. I used a FET MUX switch (SN74CBT3253) and had two pins, from the PIC, select which pair of fets fed the I2C signals through. If you try this make sure you have pull-ups on each of the output side for each I2C device, along with pull-ups on the PIC side, or the device will see an extra pulse when that channel is de-selected. Just have your PIC select which MUX pins to use before each command sequence.

Ronald
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