View previous topic :: View next topic |
Author |
Message |
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
Basic I2C question |
Posted: Mon Mar 06, 2006 8:00 am |
|
|
I have a basic question of how to interface with I2C components. I am trying to interface with a real time clock (RTC) where I have to be able to read/write to specific registers. My question is this: what is the code to read a specific register or write to a specific register. I have something like this, but it doesnt work:
i2c_start();
i2c_write(0xD1); //Read command for RTC address
i2c_write(0x00); //Read register 0
data1 = i2c_read();
i2c_stop();
This doesnt seem to work. I know this is probably a very basic question, but a short example routine would be greatly appreciated. I guess, I dont know how the slave device knows that I am looking at a specific register, I would think the second 'write' command would be taken as an enable command for a different address, no?
Anyways, if there is some code to make it clear how to write to a register or read to a register, I would greatly appreciate it. I already understand that the last bit of the address needs to change for a read/write function. Also, should I wait any time between the start and the write, or any of the writes and the read commands?
Thanks! |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Mar 06, 2006 9:05 am |
|
|
What is the part number of the component you are trying to talk to? Many of them have different sequences that need to be followed in order to establish the proper communications with them.
Ronald |
|
|
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
|
Posted: Mon Mar 06, 2006 9:29 am |
|
|
I am looking at the Dallas Semiconductor DS1307 64x8 RTC.
Thanks! |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 06, 2006 10:48 am |
|
|
The 'key', is that the chip does not allow you to send a 'register address', for the read command. It only allows the register address to be selected for a 'write' (quite a lot of I2C chips are like this).
To set the register address for a read, you have to use a different sequence:
Code: |
I2C_START();
I2C_WRITE(0xD0); //Note _write_ command
I2C_WRITE(0); //Send the address
I2C_START(); //Send a 'repeated start'
I2C_WRITE(0xD1); //Now select to read
val=I2C_READ();
//Repeat the read for as many registers as required
I2C_STOP();
|
It is slightly non-intuitive, but is common to a lot of I2C devices. The 'address' has to be selected before the 'read' command is started.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 06, 2006 11:02 am |
|
|
And the last read operation does a "NACK" (a 0 parameter):
|
|
|
jseidmann
Joined: 04 Nov 2004 Posts: 67
|
|
Posted: Mon Mar 06, 2006 11:12 am |
|
|
Thanks everyone! I will not be able to test this out for a few days, but I appreciate all your input and help!
Thanks! |
|
|
|