I2C主从机



  • 请问能否提供一个I2C主从机使用的demo,可以测试用的?



  • examples\none_evm\ble_drivers_demo\proj 下#include "driver_system.h"
    #include "driver_iomux.h"

    #include "driver_iic.h"
    static const uint8_t LSM6DS33_ADDR = (0x6B<<1);
    void demo_i2c(void)
    {
    uint8_t i2c_test_no = 0;
    uint8_t buff1[6];
    uint8_t buff2[6];
    co_printf("IIC demo\r\n");

    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_6, PORTD6_FUNC_I2C1_CLK);//Pc6
    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_7, PORTD7_FUNC_I2C1_DAT);//Pc7
    system_set_port_pull( (GPIO_PD6|GPIO_PD7), false);
    

    //IIC1, 1M hz. slave addr: 0xd6
    iic_init(IIC_CHANNEL_1,1000,LSM6DS33_ADDR);
    iic_write_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x15,0x00);
    iic_write_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x16,0x00);
    iic_write_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x10,0x80);
    iic_write_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x11,0x80);
    iic_write_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x12,0x04);

    while(1)
    {
        for(uint8_t i=0; i<6; i++)
        {
            iic_read_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x28+i,&buff1[i]);
            iic_read_byte(IIC_CHANNEL_1,LSM6DS33_ADDR,0x22+i,&buff2[i]);
        }
        co_printf("X:%d |Y:%d |Z:%d\r\n",*(uint16_t *)buff1,*(uint16_t *)(buff1+2),*(uint16_t *)(buff1+4));
        co_printf("AccX:%d|Acc_Y:%d|Acc_Z:%d\r\n",*(uint16_t *)buff2,*(uint16_t *)(buff2+2),*(uint16_t *)(buff2+4));
        i2c_test_no++;
        if(i2c_test_no>3)
            break;
    }
    

    }