Nordic--nrf52832--TWI/IIC

参考博客:https://blog.csdn.net/wulazula/article/details/80818651
 TWI总线。

  • 兼容I2C协议

  • 时钟频率有100k,250k,400k可以选择

  • 支持时钟扩展

  • 支持EasyDMA

  • SDK 15.0

IIC/TWI初始化

#define IIC_SCL_PIN             27    // SCL signal pin
#define IIC_SDA_PIN             26    // SDA signal pin

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                data_handler(m_sample);
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}

/**
 * @brief TWI initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = IIC_SCL_PIN,           // 引脚配置
       .sda                = IIC_SDA_PIN,           // 引脚配置
       .frequency          = NRF_DRV_TWI_FREQ_100K, // 传输速率
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH, // IIC中断优先级
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

/**
 * @brief TWI uninitialization.
 */
void twi_uninit (void)
{
    nrf_drv_twi_uninit(&m_twi);
}

两个函数: nrf_drv_twi_tx 和 nrf_drv_twi_rx

ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,  // 实例
                          uint8_t               address,     // 地址
                          uint8_t const *       p_data,      // 数据 
                          uint8_t               length,      // 数据长度
                          bool                  no_stop)    // 设置停止位
ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,  // 实例
                          uint8_t               address,     // 地址
                          uint8_t *             p_data,      // 数据 
                          uint8_t               length)      // 数据长度

你可能感兴趣的:(Nordic--nrf52832--TWI/IIC)