打印
[AT32F405]

【AT-START-F405测评】-1- I2C扫描设备

[复制链接]
1352|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
YangTwo|  楼主 | 2024-5-15 20:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
通过本程序来获取连接到I2C总线上的外围设备,实验使用的是地址为0X78的OLED。

首先初始化串口与I2C。

  /* initial system clock */
  system_clock_config();

  /* config nvic priority group */
  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

  /* at board initial */
  at32_board_init();
        uart_print_init(115200);

  hi2cx.i2cx = I2Cx_PORT;

  /* i2c config */
  i2c_config(&hi2cx);
        printf("i2c scan example: aim to find the i2c device on the bus\r\n");

接下来是核心代码:
/* Scans for I2C client devices on the bus - that have an address within the 
*specified range [addr_min, addr_max]
*/
void main_i2c_scan(uint8_t addr_min, uint8_t addr_max)
{
    uint8_t client_address;
    uint8_t client_addr_str[5]={0};
    static uint8_t IIC_Client_Num = 0;
    printf("\r\n I2C Scan started from 0x%02X to 0x%02X:", addr_min, addr_max);
    //LCD_ShowString(0,35,"I2C Client:",RED,WHITE,12,0);
    for (client_address = addr_min; client_address <= addr_max; client_address++)
    {
        printf("\r\n Scanning client address = 0x%02X", (int)client_address);       

                            /* start the request reception process */

                                //I2C_Send7bitAddress(I2C1, client_address, I2C_Direction_Transmitter);
                                if((i2c_status = i2c_master_transmit(&hi2cx, client_address, &client_address, 1, I2C_TIMEOUT)) == I2C_OK )
                                {
            printf("\r\n---- --> client (0x%02X) ACKED <-- ------ ", (int)client_address);
            client_addr_str[0] = '0';
            client_addr_str[1] = 'x';
            if(client_address/16 >= 10)
                client_addr_str[2] = client_address/16 -10 + 'A';
            else
                client_addr_str[2] = client_address/16 + '0';
            if(client_address%16 >= 10)
                client_addr_str[3] = client_address%16 -10 + 'A';
            else
                client_addr_str[3] = client_address%16 + '0';
            client_addr_str[4] = '\0';
            IIC_Client_Num++;
                       
                                                printf("Yes, I2C Device found. Device Addr: %s",client_addr_str);
                                }else{
                                        continue;
                                }
        
        delay_ms(50);
    }//<--for--loop>
    printf("\r\n ----------------- I2C Scan ended -----------------");
}

串口输出:
i2c scan example: aim to find the i2c device on the bus

I2C Scan started from 0x00 to 0x7F:
Scanning client address = 0x00
Scanning client address = 0x01
Scanning client address = 0x02
Scanning client address = 0x03
Scanning client address = 0x04
Scanning client address = 0x05
Scanning client address = 0x06
Scanning client address = 0x07
Scanning client address = 0x08
Scanning client address = 0x09
Scanning client address = 0x0A
Scanning client address = 0x0B
Scanning client address = 0x0C
Scanning client address = 0x0D
Scanning client address = 0x0E
Scanning client address = 0x0F
Scanning client address = 0x10
Scanning client address = 0x11
Scanning client address = 0x12
Scanning client address = 0x13
Scanning client address = 0x14
Scanning client address = 0x15
Scanning client address = 0x16
Scanning client address = 0x17
Scanning client address = 0x18
Scanning client address = 0x19
Scanning client address = 0x1A
Scanning client address = 0x1B
Scanning client address = 0x1C
Scanning client address = 0x1D
Scanning client address = 0x1E
Scanning client address = 0x1F
Scanning client address = 0x20
Scanning client address = 0x21
Scanning client address = 0x22
Scanning client address = 0x23
Scanning client address = 0x24
Scanning client address = 0x25
Scanning client address = 0x26
Scanning client address = 0x27
Scanning client address = 0x28
Scanning client address = 0x29
Scanning client address = 0x2A
Scanning client address = 0x2B
Scanning client address = 0x2C
Scanning client address = 0x2D
Scanning client address = 0x2E
Scanning client address = 0x2F
Scanning client address = 0x30
Scanning client address = 0x31
Scanning client address = 0x32
Scanning client address = 0x33
Scanning client address = 0x34
Scanning client address = 0x35
Scanning client address = 0x36
Scanning client address = 0x37
Scanning client address = 0x38
Scanning client address = 0x39
Scanning client address = 0x3A
Scanning client address = 0x3B
Scanning client address = 0x3C
Scanning client address = 0x3D
Scanning client address = 0x3E
Scanning client address = 0x3F
Scanning client address = 0x40
Scanning client address = 0x41
Scanning client address = 0x42
Scanning client address = 0x43
Scanning client address = 0x44
Scanning client address = 0x45
Scanning client address = 0x46
Scanning client address = 0x47
Scanning client address = 0x48
Scanning client address = 0x49
Scanning client address = 0x4A
Scanning client address = 0x4B
Scanning client address = 0x4C
Scanning client address = 0x4D
Scanning client address = 0x4E
Scanning client address = 0x4F
Scanning client address = 0x50
Scanning client address = 0x51
Scanning client address = 0x52
Scanning client address = 0x53
Scanning client address = 0x54
Scanning client address = 0x55
Scanning client address = 0x56
Scanning client address = 0x57
Scanning client address = 0x58
Scanning client address = 0x59
Scanning client address = 0x5A
Scanning client address = 0x5B
Scanning client address = 0x5C
Scanning client address = 0x5D
Scanning client address = 0x5E
Scanning client address = 0x5F
Scanning client address = 0x60
Scanning client address = 0x61
Scanning client address = 0x62
Scanning client address = 0x63
Scanning client address = 0x64
Scanning client address = 0x65
Scanning client address = 0x66
Scanning client address = 0x67
Scanning client address = 0x68
Scanning client address = 0x69
Scanning client address = 0x6A
Scanning client address = 0x6B
Scanning client address = 0x6C
Scanning client address = 0x6D
Scanning client address = 0x6E
Scanning client address = 0x6F
Scanning client address = 0x70
Scanning client address = 0x71
Scanning client address = 0x72
Scanning client address = 0x73
Scanning client address = 0x74
Scanning client address = 0x75
Scanning client address = 0x76
Scanning client address = 0x77
Scanning client address = 0x78
---- --> client (0x78) ACKED <-- ------ Yes, I2C Device found. Device Addr: 0x78
Scanning client address = 0x79
---- --> client (0x79) ACKED <-- ------ Yes, I2C Device found. Device Addr: 0x79
Scanning client address = 0x7A
Scanning client address = 0x7B
Scanning client address = 0x7C
Scanning client address = 0x7D
Scanning client address = 0x7E
Scanning client address = 0x7F
----------------- I2C Scan ended -----------------

结果分析:
0x78与0x79实际上指向的是同一个设备(7 bit Address =0x3C 或者 0b'011-1100),因为每个I2C设备都有两个地址,一个用于写入,一个用于读取。


使用特权

评论回复
沙发
gangong| | 2024-10-31 07:01 | 只看该作者
写的很美

使用特权

评论回复
板凳
可怜的小弗朗士| | 2024-11-9 18:54 | 只看该作者
这个扫描的原理是啥

使用特权

评论回复
地板
YangTwo|  楼主 | 2024-11-9 19:02 | 只看该作者

依次发送不同的地址到总线上,看有没有收到ACK。收到ACK,就表示这个地址是某个器件的地址,因为它回复了ACK。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

23

主题

101

帖子

1

粉丝