IIC读写寄存器时序图
中断方式的实现流程
中断方式的实现代码(主)
//直接读:发完器件从地址后直接读取数据
//直接写:发完器件从地址后直接读取数据
//子地址读:先发送器件从地址,再发寄存器地址,然后直接读
//子地址写:先发送器件从地址,再发寄存器地址,然后直接写
if(i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_SBSEND)){
/* send slave address */
if(子地址读) {
i2c_master_addressing_dir(I2C1, I2C_sla & 0xFE);
}else {
i2c_master_addressing_dir(I2C1, I2C_sla);
}
}else if(i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_RBNE)) {
if(I2C_num > 0) {
if(3 == I2C_num) {
/*wait until the second last data byte is received into the shift register*/
while(!i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_BTC));
/* send a NACK for the last data byte */
i2c_ack_config(I2C1, I2C_ACK_DISABLE);
}
/* read a data byte from I2C_DATA*/
*I2C_buf++ = i2c_data_receive(I2C1);
I2C_num--;
if(0 == I2C_num) {
//读写完成
i2c_stop_on_bus(I2C1);/* send a stop condition */
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
i2c_ackpos_config(I2C1, I2C_ACKPOS_CURRENT);
}
}
}else if(i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_TBE)) {
if(直接读) {
if(I2C_num > 0) {
i2c_data_transmit(I2C1, *I2C_buf++);
I2C_num--;
}else {
//读写成功
i2c_stop_on_bus(I2C1);
}
}else if(子地址读) {
if(子地址未发送) {
i2c_data_transmit(I2C1, 寄存器地址);//发送子地址
}else {
//转为直接读写
i2c_start_on_bus(I2C1);
}
}else {
i2c_data_transmit(I2C1, 寄存器地址);
//转为直接读写
}
}