سلام
من یه هدر دارم که توسط تابع زیر ارسال و دریافت میکنه و توضیحات تابع هم در زیر هست :
کد:
/************************************************************************
* Function Name: LDByteWriteI2C *
* Parameters: EE memory ControlByte, address and data *
* Description: Writes data one byte at a time to I2C EE *
* device. This routine can be used for any I2C *
* EE memory device, which only uses 1 byte of *
* address data as in the 24LC01B/02B/04B/08B/16B. *
* *
************************************************************************/
unsigned char LDByteWriteI2C( unsigned char ControlByte, unsigned char LowAdd, unsigned char data )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
WriteI2C( ControlByte ); // write 1 byte - R/W bit should be 0
IdleI2C(); // ensure module is idle
WriteI2C( LowAdd ); // write address byte to EEPROM
IdleI2C(); // ensure module is idle
WriteI2C ( data ); // Write data byte to EEPROM
IdleI2C(); // ensure module is idle
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
while (EEAckPolling(ControlByte)); //Wait for write cycle to complete
return ( 0 ); // return with no error
}
/********************************************************************
* Function Name: LDByteReadI2C *
* Parameters: EE memory ControlByte, address, pointer and *
* length bytes. *
* Description: Reads data string from I2C EE memory *
* device. This routine can be used for any I2C*
* EE memory device, which only uses 1 byte of *
* address data as in the 24LC01B/02B/04B/08B. *
* *
********************************************************************/
unsigned char LDByteReadI2C( unsigned char ControlByte, unsigned char address, unsigned char *data, unsigned char length )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
WriteI2C( ControlByte ); // write 1 byte
IdleI2C(); // ensure module is idle
WriteI2C( address ); // WRITE word address to EEPROM
IdleI2C(); // ensure module is idle
RestartI2C(); // generate I2C bus restart condition
while ( SSPCON2bits.RSEN ); // wait until re-start condition is over
WriteI2C( ControlByte | 0x01 ); // WRITE 1 byte - R/W bit should be 1 for read
IdleI2C(); // ensure module is idle
getsI2C( data, length ); // read in multiple bytes
NotAckI2C(); // send not ACK condition
while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
return ( 0 ); // return with no error
}
میخام ارسال و دریافتی که توسط تابع بالا انجام میشه رو توسط تابع زیر انجام بدم :
کد:
/*
*********************************************************************************************************
** Function name: I2C_WriteNByte ()
** Function: to have the child write N bytes of data the device address
** Entry parameter: sla device from the address
** Suba_type subaddress structure 1 - single-byte address 3-8 + X structure 2-- byte address
** Suba internal device physical address
** * S will be a pointer to data to be written
** Num the number of data to be written
** Output Parameters: 1 operation is successful
** 0 operation failed
*********************************************************************************************************
*/
uint8_t I2C_WriteNByte(uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num)
{
if (num > 0) /* If you read the number is 0, return error */
{
if (suba_type == 1)
{
I2C_sla = sla; /* Read from the device address */
I2C_suba = suba; /* Device subaddress */
I2C_suba_num = 1; /* 1 byte device subaddress */
}
if (suba_type == 2)
{
I2C_sla = sla; /* Read from the device address */
I2C_suba = suba; /* Device subaddress */
I2C_suba_num = 2; /* 1 byte device subaddress */
}
if (suba_type == 3)
{
I2C_sla = sla + ((suba >> 7 )& 0x0e); /* Read from the device address */
I2C_suba = suba; /* Device subaddress */
I2C_suba_num = 1; /* 1 byte device subaddress */
}
I2C_buf = s; /* Data */
I2C_num = num; /* The number of data */
I2C_suba_en = 2; /* A sub-address, write */
I2C_end = 0;
LPC_I2C0->I2CONCLR = (1 << 2)|
(1 << 3)|
(1 << 5); /* Clear SI flag */
LPC_I2C0->I2CONSET = (1 << 5)|
(1 << 6); /* Set Start flag */
while(I2C_end == 0);
if(I2C_end)
{
Delay(20);
return 1;
}
else
{
Delay(20);
return 0;
}
}
Delay(20);
return 0;
}
/*
*********************************************************************************************************
** Function name: I2C_ReadNByte ()
** Function: sub-address from a device to read N bytes starting at any address data
** Entry parameter: sla device from the address
** Suba_type subaddress structure 1 - single-byte address 2-8 + X structure 2-- byte address
** Suba device subaddress
** S data receive buffer pointer
** Num the number of read
** Output Parameters: 1 operation is successful
** 0 operation failed
*********************************************************************************************************
*/
uint8_t I2C_ReadNByte (uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num)
{
if (num > 0)
{
if (suba_type == 1)
{
I2C_sla = sla + 1; /* Read from the device address~{#,~}R=1 */
I2C_suba = suba; /* Device subaddress */
I2C_suba_num = 1; /*1 byte device subaddress */
}
if (suba_type == 2)
{
I2C_sla = sla + 1; /* Read from the device address~{#,~}R=1 */
I2C_suba = suba; /* Device subaddress */
I2C_suba_num = 2; /* 1 byte device subaddress */
}
if (suba_type == 3)
{
I2C_sla = sla + ((suba >> 7 )& 0x0e) + 1; /* Read from the device address~{#,~}R=1 */
I2C_suba = suba & 0x0ff; /* Device subaddress */
I2C_suba_num = 1; /* 1 byte device subaddress */
}
I2C_buf = s; /* Data receive buffer pointer */
I2C_num = num; /* To read the number of */
I2C_suba_en = 1; /* A sub-address read */
I2C_end = 0;
LPC_I2C0->I2CONCLR = (1 << 2)|
(1 << 3)|
(1 << 5); /* Clear SI flag */
LPC_I2C0->I2CONSET = (1 << 5)|
(1 << 6); /* Set Start flag */
while(I2C_end == 0);
if(I2C_end)
{
Delay(20);
return 1;
}
else
{
Delay(20);
return 0;
}
}
Delay(20);
return 0;
}
باید توی تابع LDByteWriteI2C پارامتر مثلا LowAdd رو با چه پارامتری در تابع I2C_WriteNByte تعویض کنم ؟ بقیه پارامتر ها چطور ؟
کد:
LDByteWriteI2C( unsigned char ControlByte, unsigned char LowAdd, unsigned char data )
کد:
I2C_WriteNByte(uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num
)