|
|
View previous topic :: View next topic |
Author |
Message |
David_K
Joined: 14 Nov 2006 Posts: 5
|
I²C and Streams |
Posted: Thu Nov 16, 2006 12:17 pm |
|
|
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
|
|
Posted: Thu Nov 16, 2006 1:35 pm |
|
|
What is your compiler version ? |
|
|
David_K
Joined: 14 Nov 2006 Posts: 5
|
|
Posted: Thu Nov 16, 2006 2:04 pm |
|
|
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
|
|
Posted: Thu Nov 16, 2006 3:06 pm |
|
|
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 |
|
|
|
|
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
|