Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/15/05 08:33
Read: times


 
#94991 - I2C Sequential access
I wrote a single master I2C routine for 8051 family controller for storing char type variables in 24cxx serial EEPROMS. "This program worked fine". Now, I modified this program to store int type variables. To test the program I am writing 0x55AA to a certiain memory location and then reading it back. But I am not getting back 0x55AA. Where I am, I have no means to check the actual data that is being exchanged between the EEPROM and the controller, and I have only 2 LEDs in my target board that I can use for troubleshooting. So, I am posting the modified program here hoping that someone here may be able to recognize the problem area.

/* Code begins here */
void main(void)
{
...
I2CPut(0x55AA, 0x20, 0xA0);
...
OutputWord = I2CGet(0x20, 0xA0);
...
}

// Claim the I2C bus
void I2CStart(void)
{
SCL = TRUE; // Make sure that lines are active
SDA = TRUE;
DLY_CLK();
DLY_CLK();
SDA = FALSE; // Change SDA when SCL is high
DLY_CLK();
SCL = FALSE;
DLY_CLK();
}

// Release the I2C bus
void I2CStop(void)
{
DLY_CLK();
SDA= FALSE;
DLY_CLK();
SCL = TRUE;
DLY_CLK();
SDA = TRUE; // Raise SDA when SCL is high
DLY_CLK();
DLY_CLK();
}

// Clock out the data bit
void CLK_SCL(void)
{
SCL = TRUE;
DLY_CLK();
SCL = FALSE;
DLY_CLK();
}

void DLY_CLK(void)
{
_asm
NOP
NOP
NOP
NOP
_endasm;
}

void TxDataByte(unsigned char Value)
{
char Handle = 0x08;

while(Handle) // Send each bit.
{
SDA = (Value & 0x80) ? TRUE:FALSE;
CLK_SCL();
Value = Value <<1;
Handle--;
}
SDA = TRUE; // Acknowledge bit
CLK_SCL();
// Insert code for checking for acknowledgement if required.
}

char RxDataByte(void)
{
char Handle, OutputChar;

OutputChar = 0;
Handle = 0x08;

SDA = TRUE; // SDA held high to receive

while(Handle) // Get byte one bit at a time.
{
SCL = TRUE;
DLY_CLK();
OutputChar = OutputChar <<1;
if(SDA)
OutputChar |= 0x01;
DLY_CLK();
SCL = FALSE;
DLY_CLK();
Handle--;
}

return(OutputChar);
}

void I2CPut(unsigned int Value, unsigned char WordAddr, unsigned char DeviceAddr)
{
char Handle, ScratchpadVar;

I2CStart();
TxDataByte(DeviceAddr);
TxDataByte(WordAddr);
TxDataByte(Value & 0x00FF);

Value = Value >> 8;
TxDataByte(Value);

I2CStop();

// 5ms delay to allow EEPROM to complete it's write operation.
// Inline delay added here.

}

unsigned int I2CGet(unsigned char WordAddr, unsigned char DeviceAddr)
{
unsigned int ReturnValue = 0;

I2CStart();

TxDataByte(DeviceAddr); // Device address and '0' for write operation.
TxDataByte(WordAddr);
I2CStart();

TxDataByte(DeviceAddr | 0x01); // Device address and '1' for Read operation.
ReturnValue = RxDataByte();
// Acknowledge, to receive next byte
SDA = FALSE;
CLK_SCL();

ReturnValue |= (unsigned int)RxDataByte() << 8;
// Not Acknowledge
SDA = TRUE;
CLK_SCL();

I2CStop();

return(ReturnValue);
}
/* End of Code */


Regards.

List of 10 messages in thread
TopicAuthorDate
I2C Sequential access            01/01/70 00:00      
   write cycle time            01/01/70 00:00      
      Nothing wrong here.            01/01/70 00:00      
         Testing I2C Driver Code            01/01/70 00:00      
         it is from my datasheet!            01/01/70 00:00      
         Many Manufacturers            01/01/70 00:00      
   two things            01/01/70 00:00      
   What are you getting back?            01/01/70 00:00      
      Problem is solved!            01/01/70 00:00      
         programming end polling            01/01/70 00:00      

Back to Subject List