貼錯了,應該在這2個副程式
/**
* @brief Specify two bytes register address and Write a byte to Slave
*
* @param[in] *i2c Point to I2C peripheral
* @param[in] u8SlaveAddr Access Slave address(7-bit)
* @param[in] u16DataAddr Specify a address (2 byte) of data write to
* @param[in] data Write a byte data to Slave
*
* @retval 0 Write data success
* @retval 1 Write data fail, or bus occurs error events
*
* @details The function is used for I2C Master specify two bytes address that data write to in Slave.
*
*/
uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, const uint8_t data)
{
uint8_t u8Xfering = 1, u8Err = 0, u8Addr = 1, u8Ctrl = 0;
uint32_t u32txLen = 0;
I2C_START(i2c); /* Send START */
while(u8Xfering && (u8Err == 0))
{
I2C_WAIT_READY(i2c);
switch(I2C_GET_STATUS(i2c))
{
case 0x08:
I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */
u8Ctrl = I2C_CTL_SI; /* Clear SI */
break;
case 0x18: /* Slave Address ACK */
I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF00) >> 8); /* Write Hi byte address of register */
break;
case 0x20: /* Slave Address NACK */
case 0x30: /* Master transmit data NACK */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Err = 1;
break;
case 0x28:
if(u8Addr)
{
I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */
u8Addr = 0;
}
else if((u32txLen < 1) && (u8Addr == 0))
{
I2C_SET_DATA(i2c, data);
u32txLen++;
}
else
{
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Xfering = 0;
}
break;
case 0x38: /* Arbitration Lost */
default: /* Unknow status */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Err = 1;
break;
}
I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
}
return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
}
/**
* @brief Specify two bytes register address and read a byte from Slave
*
* @param[in] *i2c Point to I2C peripheral
* @param[in] u8SlaveAddr Access Slave address(7-bit)
* @param[in] u16DataAddr Specify a address(2 byte) of data read from
*
* @return Read a byte data from Slave
*
* @details The function is used for I2C Master specify two bytes address that a data byte read from Slave.
*
*
*/
uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
{
uint8_t u8Xfering = 1, u8Err = 0, rdata = 0, u8Addr = 1, u8Ctrl = 0;
I2C_START(i2c); /* Send START */
while(u8Xfering && (u8Err == 0))
{
I2C_WAIT_READY(i2c);
switch(I2C_GET_STATUS(i2c))
{
case 0x08:
I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */
u8Ctrl = I2C_CTL_SI; /* Clear SI */
break;
case 0x18: /* Slave Address ACK */
I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF00) >> 8); /* Write Hi byte address of register */
break;
case 0x20: /* Slave Address NACK */
case 0x30: /* Master transmit data NACK */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Err = 1;
break;
case 0x28:
if(u8Addr)
{
I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */
u8Addr = 0;
}
else
u8Ctrl = I2C_CTL_STA_SI; /* Clear SI and send repeat START */
break;
case 0x10:
I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */
u8Ctrl = I2C_CTL_SI; /* Clear SI */
break;
case 0x40: /* Slave Address ACK */
u8Ctrl = I2C_CTL_SI; /* Clear SI */
break;
case 0x48: /* Slave Address NACK */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Err = 1;
break;
case 0x58:
rdata = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Xfering = 0;
break;
case 0x38: /* Arbitration Lost */
default: /* Unknow status */
u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
u8Err = 1;
break;
}
I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
}
if(u8Err)
rdata = 0; /* If occurs error, return 0 */
return rdata; /* Return read data */
}
|