|
|
View previous topic :: View next topic |
Author |
Message |
ailatech
Joined: 24 Jul 2007 Posts: 7 Location: Jordan
|
Connecting 2 I2C devices |
Posted: Tue Sep 25, 2007 3:21 am |
|
|
hello
I need to connect two I2C devices with my PIC. One is DS1307 RTC, and the other is 2416 16Kbit I2C eeprom. How can I use both.
their libraries are:
DS1307:
Code: | #define DS1307_SDA PIN_C1
#define DS1307_SCL PIN_C0
#use i2c(Master, sda=DS1307_SDA, scl=DS1307_SCL)
//==========================
// initial DS1307
//==========================
void init_DS1307()
{
output_float(DS1307_SCL);
output_float(DS1307_SDA);
}
//==========================
// write data one byte to
// DS1307
//==========================
void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
//==========================
// read data one byte from DS1307
//==========================
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}
|
2416 EEPROM:
Code: | ///////////////////////////////////////////////////////////////////////////
//// Library for a MicroChip 24LC16B ////
//// ////
//// init_ext_eeprom(); Call before the other functions are used ////
//// ////
//// write_ext_eeprom(a, d); Write the byte d to the address a ////
//// ////
//// d = read_ext_eeprom(a); Read the byte d from the address a ////
//// ////
//// b = ext_eeprom_ready(); Returns TRUE if the eeprom is ready ////
//// to receive opcodes ////
//// ////
//// The main program may define EEPROM_SDA ////
//// and EEPROM_SCL to override the defaults below. ////
//// ////
//// Pin Layout ////
//// ----------------------------------------------------------- ////
//// | | ////
//// | 1: NC Not Connected | 8: VCC +5V | ////
//// | | | ////
//// | 2: NC Not Connected | 7: WP GND | ////
//// | | | ////
//// | 3: NC Not Connected | 6: SCL EEPROM_SCL and Pull-Up | ////
//// | | | ////
//// | 4: VSS GND | 5: SDA EEPROM_SDA and Pull-Up | ////
//// ----------------------------------------------------------- ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996, 2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////
#ifndef EEPROM_SDA
#define EEPROM_SDA PIN_C4
#define EEPROM_SCL PIN_C3
#endif
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 2048
void init_ext_eeprom() {
output_float(EEPROM_SCL);
output_float(EEPROM_SDA);
}
BOOLEAN ext_eeprom_ready() {
int1 ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(0xa0); // then the device is ready.
i2c_stop();
return !ack;
}
void write_ext_eeprom(long int address, BYTE data) {
while(!ext_eeprom_ready());
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
BYTE read_ext_eeprom(long int address) {
BYTE data;
while(!ext_eeprom_ready());
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
i2c_write(address);
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))|1);
data=i2c_read(0);
i2c_stop();
return(data);
}
|
_________________ Eng. Haytham Shyouk
B.A. Electronics Engineering |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Wed Sep 26, 2007 4:13 am |
|
|
Hello,
You can connect some I2C devices on the same bus. Each devices must have a different device 'address' called by Microchip 'Control code'.
The first byte transmitted by the master is the address of the target slave.
This address is composed with two parts. The first 4 bits are for the family code or 'Control code' and the 3 next are for the 'Block Select Bits'. Some 'family' have no 'Block Select Bits' ( as the DS1307).
The DS1307 'Control code' is 1101 (0xD0) and no 'Block Select Bits'. Only one DS1307 can be connected in a bus.
The 2416 'Control code' is 1010 (0xA0). The 'Block Select Bits' can select between eight 2416 connected in the bus.
Here are modified function for accessing 2416 with 'Block Select Bits'
Code: |
#ifndef I2C_SDA
#define I2C_SDA PIN_C4
#define I2C_SCL PIN_C3
#endif
#use i2c(master, sda=I2C_SDA, scl=I2C_SCL)
#ifndef hi(x)
#define hi(x) (*(&x+1))
#endif
BOOLEAN ext_eeprom_ready_SD(int SD) { // avec Select Device
int1 ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(0xa0 | (SD<<1) ); // then the device is ready.
i2c_stop();
return !ack;
}
void write_ext_eeprom_SD(long int address, BYTE data, int SD) {
while(!ext_eeprom_ready());
i2c_start();
i2c_write(0xa0 | (SD<<1) );
i2c_write(hi(address));
i2c_write(address);
i2c_write(data);
i2c_stop();
}
BYTE read_ext_eeprom_SD(long int address, int SD) {
BYTE data;
while(!ext_eeprom_ready());
i2c_start();
i2c_write(0xa0 | (SD<<1) );
i2c_write(hi(address));
i2c_write(address);
i2c_start();
i2c_write(0xa1 | (SD<<1) );
data=i2c_read(0);
i2c_stop();
return(data);
}
| The int SD parameter is the 'Block Select Bits'.
Then remove SCL ans SDA define and INIT from DS1307 driver. you need only one init procedure, as
Code: | //==========================
// initial I2C
//==========================
void init_I2C() {
output_float(I2C_SCL);
output_float(I2C_SDA);
} |
At least take care to assign a different 'Block Select Bits' to each 2416 by grounding the A0 A1 and A2 as you want. for example the first 2416 can keep A0 A1 and A2 not grounded, the second 2416 can have A0 and A1 not grounded and A2 grounded, the third can have A0 not grounded A1 grounded and A2 not grounded, etc...
dro _________________ in médio virtus |
|
|
|
|
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
|