<pre name="code" class="html">int main (void)
{
i2c_module_write_test(0x51,0x00,0x00);
i2c_module_write_test(0x51,0x00,0x02);//如图1所示
</pre>}// i2c 测试函数<pre>
void i2c_module_write_test(uint16_t dev_address, uint8_t address, uint8_t data)
{
SetWord16(I2C_ENABLE_REG, 0x0); // Disable the I2C controller
SetWord16(I2C_CON_REG, I2C_MASTER_MODE | I2C_SLAVE_DISABLE | I2C_RESTART_EN); // Slave is disabled
SetBits16(I2C_CON_REG, I2C_SPEED, 2); // Set speed
SetBits16(I2C_CON_REG, I2C_10BITADDR_MASTER, 0); // Set addressing mode
SetWord16(I2C_TAR_REG, dev_address & 0x3FF); // Set Slave device address
SetWord16(I2C_ENABLE_REG, 0x1); // Enable the I2C controller
while( (GetWord16(I2C_STATUS_REG) & 0x20) != 0 ); // Wait for I2C master FSM to be IDLE
// write the dev address (w)
// write the mem address (w)
<span style="white-space:pre"> </span>SEND_I2C_COMMAND(address & 0xFF);
WAIT_WHILE_I2C_FIFO_IS_FULL(); // Wait if I2c Tx FIFO is full
SEND_I2C_COMMAND(data & 0xFF); // Send write data
WAIT_UNTIL_NO_MASTER_ACTIVITY(); // wait until no master activity
}
图1