generated from hulk/gd32e23x_template
add iic scan
This commit is contained in:
parent
dde90dc162
commit
16767ae172
@ -49,4 +49,6 @@ void LDC1612_Init(void);
|
||||
int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length);
|
||||
int i2c_write_reg(uint8_t device_address, uint8_t *data);
|
||||
|
||||
void I2C_Scan(void);
|
||||
|
||||
#endif //LDC1612_H
|
||||
|
@ -3,8 +3,6 @@
|
||||
//
|
||||
|
||||
#include "LDC1612.h"
|
||||
#include "i2c.h"
|
||||
|
||||
|
||||
void led_config(void)
|
||||
{
|
||||
@ -264,4 +262,54 @@ i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); // 重新启用ACK
|
||||
return 0;
|
||||
}
|
||||
|
||||
void I2C_Scan(void) {
|
||||
uint32_t timeout;
|
||||
uint8_t address;
|
||||
int found_devices = 0;
|
||||
|
||||
printf("Scanning I2C bus...\r\n");
|
||||
|
||||
for (address = 1; address < 127; address++) {
|
||||
timeout = 0;
|
||||
|
||||
// 生成起始条件
|
||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
continue; // 超时,跳过该地址
|
||||
}
|
||||
i2c_start_on_bus(LDC_I2C);
|
||||
timeout = 0;
|
||||
|
||||
// 等待起始条件发送完成
|
||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
continue; // 超时,跳过该地址
|
||||
}
|
||||
i2c_master_addressing(LDC_I2C, address << 1, I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
|
||||
// 等待地址发送完成
|
||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
||||
printf("Found device at 0x%02X\r\n", address);
|
||||
found_devices++;
|
||||
}
|
||||
|
||||
// 生成停止条件
|
||||
i2c_stop_on_bus(LDC_I2C);
|
||||
timeout = 0;
|
||||
|
||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
}
|
||||
|
||||
if (found_devices == 0) {
|
||||
printf("No I2C devices found.\r\n");
|
||||
} else {
|
||||
printf("Total %d I2C devices found.\r\n", found_devices);
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,9 @@ int main(void)
|
||||
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
||||
printf("\r\n");
|
||||
|
||||
LDC1612_Init();
|
||||
LDC1612_I2CConfig();
|
||||
// LDC1612_Init();
|
||||
I2C_Scan();
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user