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

Basic I2C question

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



Joined: 04 Nov 2004
Posts: 67

View user's profile Send private message Send e-mail

Basic I2C question
PostPosted: Mon Mar 06, 2006 8:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 9:05 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 06, 2006 9:29 am     Reply with quote

I am looking at the Dallas Semiconductor DS1307 64x8 RTC.

Thanks!
Ttelmah
Guest







PostPosted: Mon Mar 06, 2006 10:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Mar 06, 2006 11:02 am     Reply with quote

And the last read operation does a "NACK" (a 0 parameter):
Code:
val=I2C_READ(0);
jseidmann



Joined: 04 Nov 2004
Posts: 67

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 06, 2006 11:12 am     Reply with quote

Thanks everyone! I will not be able to test this out for a few days, but I appreciate all your input and help!

Thanks!
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