Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 16th June 2008, 08:54 PM   (permalink)
Default Can i use two I2C device in 16F877A?

Hi,

i want to use DS1307(RTC) and DS1820(Temprature Sensor) in my project. my micro is 16F877A and compiler is MikroC. this micro has one I2C bus but i have 2 I2C device.

can i use this 2 device in my project?
Hesam Kamalan is offline  
Reply With Quote
Old 16th June 2008, 09:01 PM   (permalink)
Default

You can use quite a few devices on the same I2C bus as long as they have different addresses.
But the DS18S20 is not an I2C device, it's a 1-wire device and not I2C bus compatible.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Reply With Quote
Old 16th June 2008, 09:19 PM   (permalink)
Default

Thank you blueroomelectronics,

you are right.
Hesam Kamalan is offline  
Reply With Quote
Old 17th June 2008, 01:13 AM   (permalink)
Default

If it wasn't a one-wire and you still wanted 2 I2C controls you could also do so by using the mikroC Soft_I2C library. This allows you to specify the SDA, SCL pins to be whatever you like. So you could have one using the default SDA/SCL pins RC4 and RC3 and then you could wire up two others as well e.g. RB4 and RB5.

Last edited by richacm; 17th June 2008 at 01:27 AM.
richacm is offline  
Reply With Quote
Old 17th June 2008, 08:24 AM   (permalink)
Default

Quote:
Originally Posted by richacm View Post
If it wasn't a one-wire and you still wanted 2 I2C controls you could also do so by using the mikroC Soft_I2C library. This allows you to specify the SDA, SCL pins to be whatever you like. So you could have one using the default SDA/SCL pins RC4 and RC3 and then you could wire up two others as well e.g. RB4 and RB5.
But that would rather defeat the object of I2C, which is that you only need a single bus, and all devices have their own address on that bus. I'm not saying that under extremely rare circumstances it might be useful, but it would be rare.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now  
Reply With Quote
Old 17th June 2008, 11:02 AM   (permalink)
Default

Thank you very much.

i was worked with one I2C bus but i did not work with two I2C device.

i have a question.
when i assign one address on my bus, witch device take first address?
Hesam Kamalan is offline  
Reply With Quote
Old 17th June 2008, 11:28 AM   (permalink)
Default

You don't 'assign addresses', each device has it's own address assigned when the chip is made - although many have external pins you can use to select one of a number of addresses. Eight pin EEPROM's are a prime example.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now  
Reply With Quote
Old 17th June 2008, 12:05 PM   (permalink)
Default

thanks Nigel,

imagin that i want to use one DS1307. i use this code for writeing data:
//----------------------------------------------------------------
void write_DS1307(unsigned short address, unsigned short data)
{
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Wr(data);
I2C_Stop();
}
//----------------------------------------------------------------

i wrote my own address in code. now, if i had two DS1307, how can i assign address?
witch Device take first address?
Hesam Kamalan is offline  
Reply With Quote
Old 17th June 2008, 12:54 PM   (permalink)
Default

Quote:
Originally Posted by Hesam Kamalan View Post
thanks Nigel,

imagin that i want to use one DS1307. i use this code for writeing data:
//----------------------------------------------------------------
void write_DS1307(unsigned short address, unsigned short data)
{
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Wr(data);
I2C_Stop();
}
//----------------------------------------------------------------

i wrote my own address in code. now, if i had two DS1307, how can i assign address?
witch Device take first address?
Are there any other printed numbers on the DS1307's..?
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/
ericgibbs is online now  
Reply With Quote
Old 17th June 2008, 01:12 PM   (permalink)
Default

The DS1307 has a fixed I2C address of 0b1101000x (0xD0, 0xD1)
The Philips I2C guide should help.
http://www.nxp.com/acrobat_download/.../AN10216_1.pdf
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Reply With Quote
Old 17th June 2008, 01:15 PM   (permalink)
Default

You can only have 1 DS1307 on the I²C bus as it doesn't have any address pins but why would you want two? You can put 8 serial EEPROMS, a DS1307, an MCP9800 temperature sensor, a battery monitor etc. on the same bus. Send the correct address and the device assigned that address will answer.

Mike.
Pommie is online now  
Reply With Quote
Old 17th June 2008, 05:35 PM   (permalink)
Thumbs up Can i use two I2C device in 16F877A?

Hi Heasm,
yes, you can use two I2C devices,but a little tricky. You must spend one pin of your controller. Use a digital switch like 74HC157. This has 4 switches with 2 inputs (A, B) and one output (Q) each. It can be triggered through Select A/B. It would be sufficient to switch the CLK only, but you have 4 switches, so you can switch CLK and Data Line of the I2C bus.

Some remarks to your code example.

You must first configure the MSSP as I2C operation. It ist done with the following code sequence, I call it ConfigI2C:

void ConfigI2C(void)
{
INTCON2 = 0x80; // Disable port B pullups
PIR1bits.SSPIF = 0; // Clear MSSP Interrupt Flag bit
PIR2bits.BCLIF = 0; // Clear Bus Collision Interrupt Flag bit
DDRBbits.RB1 = 1; // Configure SCL as input
DDRBbits.RB0 = 1; // Configure SDA as input
OpenI2C(MASTER, SLEW_OFF);
SSPADD = I2C_BaudRate; // I2C transfer rate 100 kc
}

Don't forget the external pullups for RB0 and RB1. I use 4.7 kOhms for 100 kHz bus speed.

Next I miss the IDLE calls in your example. This is a working routine. Reading one byte from the device. You can use it as a template.

unsigned char PCF8574_inp(unsigned char subaddr)
{
unsigned char cw;

StartI2C();
while(SSPCON2bits.SEN);
WriteI2C(PCF8574_addr+subaddr*2+Read);
IdleI2C();
cw = ReadI2C();
NotAckI2C();
while(SSPCON2bits.ACKEN);
StopI2C();
while(SSPCON2bits.PEN);
CloseI2C();
return(cw);
}

With greetings
ERhard

Last edited by ESchemainda; 17th June 2008 at 05:38 PM.
ESchemainda is offline  
Reply With Quote
Old 17th June 2008, 08:25 PM   (permalink)
Thumbs down Can i use two I2C device in 16F877A?

Sorry, I made an error.
The device for switching must be bi-directional. So one cannot use the 74HC157. I don't know whether a bidirectional device exists, so the circuit would become some difficult. May be it will work by only switching the CLK line, without clock stretching, but I'm not sure. Sorry again.
Erhard
ESchemainda is offline  
Reply With Quote
Old 17th June 2008, 09:11 PM   (permalink)
Default

thank you so much . . . all
Hesam Kamalan is offline  
Reply With Quote
Old 18th June 2008, 06:22 AM   (permalink)
Default

Quote:
Originally Posted by ESchemainda View Post
Sorry, I made an error.
The device for switching must be bi-directional. So one cannot use the 74HC157. I don't know whether a bidirectional device exists, so the circuit would become some difficult. May be it will work by only switching the CLK line, without clock stretching, but I'm not sure. Sorry again.
Erhard
hi,
It would be possible to use an analog bidirectional gate, if it was required for selection.
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/
ericgibbs is online now  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
16f877A watzmann Micro Controllers 1 9th April 2008 09:41 PM
Two VSS & Two VDD in PIC 16F877A. Suraj143 Micro Controllers 15 22nd November 2007 08:22 AM
RTC with pic 16f877a fernandoagf General Electronics Chat 1 14th October 2007 11:12 PM
pwm and 16F877A williB Micro Controllers 15 12th September 2006 08:08 AM
16F877A help TKS Micro Controllers 9 17th March 2004 12:12 PM



All times are GMT. The time now is 01:27 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.