View previous topic :: View next topic |
Author |
Message |
rret
Joined: 01 Dec 2009 Posts: 9 Location: sherman CT
|
i2c write never returns - RTC chip |
Posted: Thu Dec 10, 2009 3:39 pm |
|
|
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
|
|
Posted: Thu Dec 10, 2009 3:53 pm |
|
|
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
|
did indeed need the pull-up resistors |
Posted: Thu Dec 10, 2009 4:48 pm |
|
|
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. |
|
|
|