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 write never returns - RTC chip

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



Joined: 01 Dec 2009
Posts: 9
Location: sherman CT

View user's profile Send private message

i2c write never returns - RTC chip
PostPosted: Thu Dec 10, 2009 3:39 pm     Reply with quote

I am using the following circuit. The RTC is hooked up to a 16F877A.



RC2 is !RESET
RC3 is SCL
RC4 is SDA

The following code (from PCM_PROGRAMMER) was modified by me. The 1st write never returns. Please, any ideas why?
Code:

#ifndef PCF8593_SDA
#define PCF8593_SDA        PIN_C4   //data
#define PCF8593_SCL        PIN_C3   //clock
#define PCF8593_NOTRESET    PIN_C2
#endif

#use i2c(master, sda=PCF8593_SDA, scl=PCF8593_SCL)

#ifndef PCF8593_WRITE_ADDRESS
#define PCF8593_WRITE_ADDRESS 0xA2
#define PCF8593_READ_ADDRESS  0xA3
#endif

// Register addresses
#define PCF8593_CTRL_STATUS_REG    0x00

// Commands for the Control/Status register.
#define PCF8593_START_COUNTING     0x00   //bit7 = 0   p.6
#define PCF8593_STOP_COUNTING      0x80   //bit7 = 1   p.6

//----------------------------------------------
void PCF8593_init(void)
{
//pulse NOTRESET-
  output_low(PCF8593_NOTRESET);  //as per 8593 DS
  //delay_ms(20);
  output_high(PCF8593_NOTRESET);
 
PCF8593_write_byte(PCF8593_CTRL_STATUS_REG,
                              PCF8593_START_COUNTING);
}   

//----------------------------------------------
void PCF8593_write_byte(int8 address, int8 data)
{
disable_interrupts(GLOBAL);
i2c_start();
i2c_write(PCF8593_WRITE_ADDRESS);  // <======== NEVER RETURNS
i2c_write(address);
i2c_write(data);
i2c_stop();
enable_interrupts(GLOBAL);
}   

void main()
{
   PCF8593_init();
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 10, 2009 3:53 pm     Reply with quote

Quote:
1st write never returns

If it locks up on the i2c_write(), it's because that function checks the
SCL line for "clock stretching" by the slave device. If the SCL line
is held low permanently, i2c_write() will stay in an endless loop
while waiting for SCL to go high. It will lock up.

One way this could happen is if you left off the pull-up resistors
on the SCL (and SDA) lines. These pull-ups are a requirement of
the i2c bus. You can use 4.7K ohm resistors for the pull-ups.


Quote:

void PCF8593_write_byte(int8 address, int8 data)
{
disable_interrupts(GLOBAL);
i2c_start();
i2c_write(PCF8593_WRITE_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
enable_interrupts(GLOBAL);
}

Also, if you're not using interrupts in your program, then delete those
two lines.
rret



Joined: 01 Dec 2009
Posts: 9
Location: sherman CT

View user's profile Send private message

did indeed need the pull-up resistors
PostPosted: Thu Dec 10, 2009 4:48 pm     Reply with quote

Thanks PCM_PROGRAMMER.

My circuit did indeed need the pull-up resistors.
I threw a couple of 4.6 k ohms in there and bingo! No more stuck at the write call.

Thanks again.
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