IIC地址配置与通信

I2C设备的通信地址通常由硬件决定,并且通过设备上的引脚配置。在许多I2C设备上,有一些引脚(通常标记为A0, A1, A2等)用于设置设备的I2C地址。这些引脚可以通过连接到VCC(高电平)或GND(低电平)来设置不同的地址组合。

设置I2C设备地址

具体步骤如下:

  1. 查阅数据手册:首先,查看你的I2C设备的数据手册,了解如何设置设备的I2C地址。通常,数据手册会提供地址引脚的详细说明。

  2. 连接地址引脚:根据数据手册,将地址引脚(如A0, A1,)连接到VCC或GND,形成一个特定的二进制地址。例如,某个设备的默认地址可能是0x20,如果A0, A1分别连接到GND, GND,,则设备的地址可能变为0x20。

示例

假设你有一个I2C设备,其地址由A0和A1引脚设置。设备的默认地址为0x20,并且根据A0和A1的连接情况可以改变为以下地址:

  • A0=GND, A1=GND: 0x20
  • A0=VCC, A1=GND: 0x21
  • A0=GND, A1=VCC: 0x22
  • A0=VCC, A1=VCC: 0x23

注意点:

1、IIC的读写操作需要等待上一个操作完了之后再去进行下一个操作,一般是用中断去判断操作是否完成。

2、还有一般写和读是8位数据,要是8位以上的需要转一下。

3、写入和读取的地址有的芯片只支持7位数据,这个时候读和写就都是用同一个地址。

/* TWI instance ID. */
#define TWI_INSTANCE_ID_0     0

/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done0 = false;

/* TWI instance. */
static const nrf_drv_twi_t m_twi0 = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID_0);

/* Buffer for samples read from temperature sensor. */
// static uint8_t m_sample;

/**
 * @brief i2c events handler.
 */
static void LIS2DH12_i2c_handler0(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    m_xfer_done0 = true;
    //NRF_LOG_INFO("I2C transfer completed.");
}


/**
 * @brief i2c initialization.
 */
void nrf_i2c_init2(void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = LIS2DH12_I2C_SCL_PIN2,
       .sda                = LIS2DH12_I2C_SDA_PIN2,
       .frequency          = NRF_DRV_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi0, &twi_config, LIS2DH12_i2c_handler0, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi0);
}

uint8_t nrf_i2c_write(uint8_t device_id, const uint8_t write_buffer[], uint16_t length)
{
    ret_code_t err_code = NRF_SUCCESS;
    uint16_t offset = 0;
    uint16_t iic_id = device_id >> 1;
    while (offset < length)
    {
        uint16_t chunk_length = (length - offset) > 255 ? 255 : (length - offset);
        m_xfer_done0 = false;

        err_code = nrf_drv_twi_tx(&m_twi0, iic_id, &write_buffer[offset], chunk_length, false);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        while (!m_xfer_done0)
        {
            __WFE(); // 等待事件完成
        }

        offset += chunk_length;
    }

    return NRF_SUCCESS;
}

uint8_t nrf_i2c_read(uint8_t device_id, const uint8_t write_buffer[], uint16_t write_length, uint8_t read_buffer[], uint16_t read_length)
{
    ret_code_t err_code;
    uint16_t offset = 0;
    uint16_t iic_id = device_id >> 1;
    // 发送写入数据(寄存器地址等)
    while (offset < write_length)
    {
        uint16_t chunk_length = (write_length - offset) > 255 ? 255 : (write_length - offset);
        m_xfer_done0 = false;

        err_code = nrf_drv_twi_tx(&m_twi0, iic_id, &write_buffer[offset], chunk_length, true);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        while (!m_xfer_done0)
        {
            __WFE(); // 等待事件完成
        }

        offset += chunk_length;
    }

    offset = 0;
    // 接收读数据
    while (offset < read_length)
    {
        uint16_t chunk_length = (read_length - offset) > 255 ? 255 : (read_length - offset);
        m_xfer_done0 = false;

        err_code = nrf_drv_twi_rx(&m_twi0, iic_id, &read_buffer[offset], chunk_length);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        while (!m_xfer_done0)
        {
            __WFE(); // 等待事件完成
        }

        offset += chunk_length;
    }

    return NRF_SUCCESS;
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮若于心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值