![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
code stuck at I2C_WRITE() ?? |
Posted: Tue Dec 13, 2005 6:35 am |
|
|
Hi all,
The following code is a pretty simple EEPROM test code. It works on one of my test board but doesn't on another one. Can anybody think of any problems in software or hardware for this one?
Code: | #include<18f8722.h>
#fuses HS, LVP, NOWDT
#use delay(clock=24000000)
#use i2c( Master, fast, SDA = PIN_C1, SCL = PIN_C0)
#define EEPROM_WRITE 0b10100000 // bit 7 - 4 are the control bits where '1010' means either WRITE or READ operation
#define EEPROM_READ 0b10100001 // bit 3, bit 2 and bit 1 together represent the device address which is 000 here
void I2C_Init( void )
{
output_float( PIN_C0 );
output_float( PIN_C1 );
}
int1 I2C_Device_Ready( void )
{
int1 ACK;
i2c_start();
ACK = i2c_write( EEPROM_WRITE);
i2c_stop();
return !ACK;
}
void I2C_Device_Write( signed int *EEPROM_Buffer, int EEPROM_Address_MSB, int EEPROM_Address_LSB, int Buffer_Length)
{
int i;
while( !I2C_Device_Ready() ); // Wait for ready
i2c_start();
i2c_write( EEPROM_WRITE ); // Send control byte
i2c_write( EEPROM_Address_MSB ); // Send most significant address byte
i2c_write( EEPROM_Address_LSB ); // Send least significant address byte
for( i=0; i<Buffer_Length; i++)
i2c_write( *(EEPROM_Buffer + i) );
i2c_stop();
}
void I2C_Device_Read( signed int *EEPROM_Buffer, int EEPROM_Address_MSB, int EEPROM_Address_LSB, int Buffer_Length)
{
int i;
while( !I2C_Device_Ready() ); // Wait for ready
i2c_start();
i2c_write( EEPROM_WRITE);
i2c_write( EEPROM_Address_MSB ); // Send most significant address byte
i2c_write( EEPROM_Address_LSB ); // Send least significant address byte
i2c_start();
i2c_write( EEPROM_READ);
for( i=0; i< Buffer_Length - 1 ; i++)
*(EEPROM_Buffer + i) = i2c_read();
*(EEPROM_Buffer + (Buffer_Length - 1) ) = i2c_read(0); // no ACK for the last byte
i2c_stop();
}
void main()
{
I2C_Init();
while( !I2C_Device_Ready() )
;
while(1)
{
output_high(pin_f5);
delay_ms(500);
output_low(pin_f5);
delay_ms(500);
}
}
|
I realise on the second board, the code couldn't run through function I2C_Device_Ready() and actually got stuck at I2C_WRITE() (I did some simple test on it and sure about this thing). The most weird thing is, I couldn't even see any signal coming out of the PIC at PIN A1 or A0 on oscillope. So I think this problem has nothing to do with external circuitry because it's the PIC that fails to send signal out on SDA. But why??? |
|
![](templates/subSilver/images/spacer.gif) |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
|
Posted: Tue Dec 13, 2005 7:19 am |
|
|
actually i figured out the problem.
forgot to connect pull up resistors... ![Embarassed](images/smiles/icon_redface.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Dec 13, 2005 7:28 am |
|
|
Obvious question. Have you got resistive pull-ups on the lines?. For I2C, the PIC only pulls the signal down. It is up to the external circuit to pull the lines up. Without the pull ups, there will be no activity on the lines, and the code will not work.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|