First modify the project_cfg.h file to define the hardware-defined i2c pins, where 13 and 14 represent IO13 and IO14, as follows:
const fpioa_cfg_t g_fpioa_cfg =
{
.version = PIN_CFG_VERSION,
.functions_count = 2,
.functions =
{
{14, FUNC_I2C0_SCLK},
{13, FUNC_I2C0_SDA},
}
};
Then in the main function is as follows:
//打开i2c0
handle_t i2c = io_open ("/dev/i2c0");
//获取设备地址
handle_t dev0 = i2c_get_device (i2c , 0x21 , 7);
//设置频率
i2c_dev_set_clock_rate (dev0 , 200000);
uint8_t reg = 0x26;
uint8_t data_buf [2] = { 0x26 ,0x02 };
data_buf [0] = reg;
//寄存器0x26写入值为0x02
io_write (dev0 , data_buf , 2);
//从0x26中读出一个字节并保存到data_buf里
i2c_dev_transfer_sequential (dev0 , ® , 1, data_buf , 1);
//对比两个值是否相同
printf("%x %x\r\n",data_buf[0],data_buf[1]);