xlsw_3dp_LDC1612/src/LDC1612.c
2024-12-07 12:14:09 +08:00

185 lines
5.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by dell on 24-12-3.
//
#include "LDC1612.h"
void I2C_config(void) {
rcu_periph_clock_enable(RCU_IR_GPIO);
rcu_periph_clock_enable(RCU_I2C);
gpio_af_set(I2C_SCL_PORT, I2C_GPIO_AF, I2C_SCL_PIN);
gpio_af_set(I2C_SDA_PORT, I2C_GPIO_AF, I2C_SDA_PIN);
gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN);
gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2);
// i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR);
i2c_enable(I2C0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
void LDC1612_Init(void) {
uint8_t RCOUNT0_ALL[3]={SET_CONVERSION_TIME_REG_START,0x05,0x36};//csdn
uint8_t SETTLECOUNT0_ALL[3]={SET_LC_STABILIZE_REG_START,0x00,0x0a};
uint8_t CLOCK_DIVIDERS0_ALL[3]={SET_FREQ_REG_START,0x10,0x02};
uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_REG,0x00,0x00};
uint8_t MUX_CONFIG_ALL[3]={MUL_CONFIG_REG,0x82,0x0c};
uint8_t DRIVE_CURRENT0_ALL[3]={SET_DRIVER_CURRENT_REG,0x90,0x00};
uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn
}
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(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
timeout++;
if (timeout >= I2C_TIME_OUT) {
continue; // 超时,跳过该地址
}
i2c_start_on_bus(I2C0);
timeout = 0;
// 等待起始条件发送完成
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
timeout++;
if (timeout >= I2C_TIME_OUT) {
continue; // 超时,跳过该地址
}
i2c_master_addressing(I2C0, (address << 1), I2C_TRANSMITTER);
timeout = 0;
// 等待地址发送完成
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
timeout++;
if (timeout < I2C_TIME_OUT) {
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
printf("Found device at 0x%02X\r\n", address);
found_devices++;
}
// 生成停止条件
i2c_stop_on_bus(I2C0);
timeout = 0;
while (i2c_flag_get(I2C0, 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);
}
}
/**
* @brief 读取16位I2C数据带有超时机制
* @param reg: 要读取的寄存器地址
* @param value: 存储读取数据的指针
* @retval 0: 成功1: 超时
*/
uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) {
uint32_t timeout = 0;
// 发送启动信号
i2c_start_on_bus(I2C0);
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
}
// 发送从设备地址和写信号
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
if ((timeout++) > I2C_TIME_OUT) return 2; // 超时
}
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
// 发送寄存器地址
i2c_data_transmit(I2C0, reg);
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE)) {
if ((timeout++) > I2C_TIME_OUT) return 3; // 超时
}
// 重新启动信号
i2c_start_on_bus(I2C0);
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
if ((timeout++) > I2C_TIME_OUT) return 4; // 超时
}
// 发送从设备地址和读信号
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
if ((timeout++) > I2C_TIME_OUT) return 5; // 超时
}
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
// 读取数据
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
if ((timeout++) > I2C_TIME_OUT) return 6; // 超时
}
*value = i2c_data_receive(I2C0) << 8; // 读取高8位
timeout = 0;
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
if ((timeout++) > I2C_TIME_OUT) return 7; // 超时
}
*value |= i2c_data_receive(I2C0); // 读取低8位
// 发送停止信号
i2c_stop_on_bus(I2C0);
timeout = 0;
return 0; // 成功
}
void LDC1612_read_sensor_infomation(void)
{
uint16_t device_id;
uint16_t manufacturer_id;
uint8_t result;
// 读取设备ID
result = IIC_read_16bit(READ_DEVICE_ID, &device_id);
if (result == 0) {
printf("Device ID: 0x%04X\n", device_id);
} else {
printf("Failed to read Device ID\n");
printf("result: %d\n", result);
}
result = IIC_read_16bit(READ_MANUFACTURER_ID, &manufacturer_id);
if (result == 0) {
printf("manufacturer ID: 0x%04X\n", manufacturer_id);
} else {
printf("Failed to read manufacturer ID\n");
printf("result: %d\n", result);
}
}