1.硬件IIC初始化
//硬件IIC相关函数
void IIC_HardInit(void)
{
i2c_config_t config;
// enable the clk
rcc_enable_peripheral_clk(RCC_PERIPHERAL_I2C0, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOA, true);
// set iomux
gpio_set_iomux(GPIOA, GPIO_PIN_14, 3);
gpio_set_iomux(GPIOA, GPIO_PIN_15, 3);
// init
i2c_config_init(&config);
config.settings.master.speed = I2C_CR_BUS_MODE_FAST;
i2c_init(I2C0, &config);
i2c_cmd(I2C0, true);
}
2.写寄存器
//硬件IIC往寄存器里写数据
void KX126_WriteReg_Hardware(u8 reg,u8 dat)
{
//起始信号+从机地址+写
i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//写寄存器
i2c_send_data(I2C0, reg);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//写数据
i2c_send_data(I2C0, dat);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//停止信号
i2c_master_send_stop(I2C0);
}
3.读寄存器
//硬件IIC从寄存器里读数据
u8 KX126_ReadReg_Hardware(u8 reg)
{
u8 dat=0;
//起始信号+从机地址+写
i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//写寄存器
i2c_send_data(I2C0, reg);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//起始信号+从机地址+读
i2c_master_send_start(I2C0, KX126_ADDR, I2C_READ);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//读数据
i2c_set_receive_mode(I2C0, I2C_NAK);
while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);
dat= i2c_receive_data(I2C0);
i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
//停止信号
i2c_master_send_stop(I2C0);
return dat;
}
4.连续写寄存器
//硬件IIC往寄存器写入连续数据
void KX126_WriteRegBuf_Hardware(u8 reg,u8 *buf,u16 len)
{
u8 i;
//起始信号+从机地址+写
i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//写寄存器
i2c_send_data(I2C0, reg);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//发送数据
for(i=0;i<len;i++)
{
//写数据
i2c_send_data(I2C0, buf[i]);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
}
//IIC_Stop();
i2c_master_send_stop(I2C0);
}
5.连续读寄存器
//硬件IIC从寄存器里读取连续数据
void KX126_ReadRegBuf_Hardware(u8 reg,u8 *buf,u16 len)
{
//起始信号+从机地址+写
i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//写寄存器
i2c_send_data(I2C0, reg);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//起始信号+从机地址+读
i2c_master_send_start(I2C0, KX126_ADDR, I2C_READ);
i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
//读取数据
while(len)
{
if(len==1)
{
i2c_set_receive_mode(I2C0, I2C_NAK);
while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);//有数据进来时退出
*buf= i2c_receive_data(I2C0);
i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
}
else
{
i2c_set_receive_mode(I2C0, I2C_ACK);
while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);
*buf= i2c_receive_data(I2C0);
i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
}
len--;
buf++;
}
//IIC_Stop();
i2c_master_send_stop(I2C0);
}