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 CCS Technical Support

I2C question

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



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

I2C question
PostPosted: Fri Dec 24, 2004 7:51 am     Reply with quote

I am using a 16F628 as an I2C master to control the philips PCF8574 series of I/O expanders, using the built in I2C functions of PCM 3.148. The code that I have written so far is correct and functions as expected.

I now wish to include code that will determine whether I have a PCF8574 or a PCF8574A fitted to the hardware - the only difference being the I2C address. I am looking for hints or suggestions on how to go about this.

I cannot use the i2c_poll CCS routine, as the 16F628 does not have a hardware SSP function. I suspect that I will need to do an i2c_write and look for an 'ack' from the 8574, changing addresses if I don't receive the 'ack' - am I on the right track? Suggestions on how to do this would be welcome.

I see from the on-line help (yes, I do occasionally RTFM), that there is no time out function within the i2c routines, so again any help on this subject would also be welcome.
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Fri Dec 24, 2004 3:18 pm     Reply with quote

I think that you are on right track - the ack is returned on i2c_write() when using either hardware or software i2c.
The logic is inverted though, a 0 is returned for correct acknowledge. Test with all addressses used. If device is not present, or not working, that needs to be handled too.

Re timeout, the usual approach is is to use a 'tick' counter that counts hardware timer overflow interrupts.
There have been many of these posted on this forum, eg.
http://www.ccsinfo.com/forum/viewtopic.php?p=17814#17814
There's no need for a timeout though in this case because the ack would be there at the end of each write.
adrian



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

PostPosted: Sat Dec 25, 2004 8:55 am     Reply with quote

Woo Hoo, there is a Santa out there!

Thanks.
adrian



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

PostPosted: Tue Dec 28, 2004 2:49 pm     Reply with quote

OK, I am now starting to make progress and have got some working code.

I have discovered that I need a small delay in the code snippet below, before the code works correctly.
Code:

I2C_start();
nack = I2C_write(address);
delay_uS(500);      //critical need a delay here - but how long should it be?
I2C_stop();

Anybody got any ideas what on what I need to look at, to work out how long this delay should be? Is it dependant on bus length, bus speed, processor speed?

Also, if I have the (software) #use I2C(), but don't specify whether I use the fast or slow I2C specification, what does the compiler use? I dont have a 'scope to look at the waveforms here.
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Tue Dec 28, 2004 7:58 pm     Reply with quote

The delay shouldn't be necessary. I tried it on a 16F876 with hardware and software i2c and it worked fine without the delay. Not sure what is happening in your setup.

Re bus speed, it defaults to the standard 100kHz.
FAST has to added to the #use i2c directive to get 400kHz, but in practice the speed is a lot lower for software i2c.

Edited: Worth also mentioning that the maximum bus speed spec. for the PCF8574 is 100kHz.
adrian



Joined: 08 Sep 2003
Posts: 92
Location: Glasgow, UK

View user's profile Send private message

PostPosted: Wed Dec 29, 2004 5:32 am     Reply with quote

Kenny wrote:
The delay shouldn't be necessary. I tried it on a 16F876 with hardware and software i2c and it worked fine without the delay. Not sure what is happening in your setup.


I'm afraid my setup is a mess at the moment - which is proberably the root cause of this problem. I have a 16F628 on a PCB and the PCF8574A on a 'breadboard' linked by wires. This was just to get me going to see what I could come up with, before I commit to a new PCB.

Thanks for the input.
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

Re: I2C question
PostPosted: Thu Dec 30, 2004 12:00 pm     Reply with quote

adrian wrote:
I now wish to include code that will determine whether I have a PCF8574 or a PCF8574A fitted to the hardware - the only difference being the I2C address. I am looking for hints or suggestions on how to go about this.


see my function "i2c_device_exists(int device_type, int device_addr)" about halfway down in the following link:
http://www.ccsinfo.com/forum/viewtopic.php?t=19526

note also the two defines located a few dozen lines before that function definition:
Code:

#define PCF8574_ID   0b01000000
#define PCF8574A_ID  0b01110000


jds-pic
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