diff --git a/inc/LDC1612.h b/inc/LDC1612.h index 02a83b1..ee6aa61 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -12,6 +12,7 @@ #include #include #include +#include #define LDC1612_ADDR (0x2B << 1) @@ -48,9 +49,19 @@ /******************************************************************************/ -#define CONVERSION_TIME_CH0 0X0546 //0536 -#define CONVERSION_OFFSET_CH0 0X0000 -#define LC_STABILIZE_TIME_CH0 0X001E //30 +#define LDC1612_CONVERSION_TIME_CH0 0X0546 //0536 +#define LDC1612_DRIVE_CURRENT 0X9000 //A000 +#define LDC1612_MUX_CONFIG 0X020C // no auto scan and filter bandwidth 3.3MHz +#define LDC1612_SENSOR_CONFIG 0X1601 +#define LDC1612_ERROR_CONFIG 0x0000 +#define LC_STABILIZE_TIME_CH0 0X001E //30 + +/******************************************************************************/ + +#define COIL_RP_KOM 15.727 +#define COIL_L_UH 18.147 +#define COIL_C_PF 150 +#define COIL_Q_FACTOR 35.97 typedef enum { I2C_START = 0, @@ -80,9 +91,19 @@ void I2C_config(void); void gpio_config(void); void i2c_config(void); void i2c_bus_reset(void); -void I2C_scan(void); +void i2c_scan(void); + +void ldc1612_set_conversion_time(uint8_t channel, uint16_t result); +void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result); +void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result); +void ldc1612_set_freq_divide(uint8_t channel); +void ldc1612_set_error_config(uint16_t value); +void ldc1612_set_mux_config(uint16_t value); +void ldc1612_reset_sensor(void); +void ldc1612_set_drive_current(uint8_t channel, uint16_t value); +void ldc1612_set_sensor_config(uint16_t value); +void ldc1612_single_ch0_config(void); -int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data); void ldc1612_iic_get_sensor_infomation(void); uint16_t ldc1612_get_manufacturer_id(void); uint16_t ldc1612_get_deveice_id(void); diff --git a/src/LDC1612.c b/src/LDC1612.c index 227d723..2ddbd8f 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -104,7 +104,7 @@ void i2c_bus_reset(void) { * 如果在某个地址上发现了设备,则会打印出该设备的地址。 * 最后会打印出找到的设备总数。 */ -void I2C_scan(void) { +void i2c_scan(void) { uint32_t timeout; uint8_t address; int found_devices = 0; @@ -190,115 +190,116 @@ void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) { ldc1612_iic_write_16bits(SET_LC_STABILIZE_REG_START + channel, data); } -int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data) { - uint16_t timeout = 0; +/** @brief set input frequency divide and fref divide. + @param channel LDC1612 has total two channels. + @param FIN_DIV FIN input divide + @param FREF_DIV fref,reference frequency of sensor. + * */ +void ldc1612_set_freq_divide(uint8_t channel) { + uint16_t value; + uint16_t fin_div, freq_div; + float sensor_freq; - i2c_ack_config(I2C0, I2C_ACK_ENABLE); + sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6); - while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) //判断IIC总线是否忙,发送起始信号 - timeout++; - if (timeout < I2C_TIME_OUT) { - i2c_start_on_bus(I2C0); - timeout = 0; + fin_div = (uint16_t)(sensor_freq / 8.75 + 1); + + if (fin_div * 4 < 40) { + freq_div = 2; } else { - printf("err\r\n"); - return -1; // 超时返回错误 + freq_div = 4; } - while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写 - timeout++; - if (timeout < I2C_TIME_OUT) { - i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER); - timeout = 0; - } else { - return -2; // 超时返回错误 - } + value = fin_div << 12; + value |= freq_div; + // printf("\tvalue: 0x%x\r\n", value); - 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); - timeout = 0; - } else { - return -3; // 超时返回错误 - } + uint8_t data[2] = {0}; + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; + // printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div); - while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址 - timeout++; - if (timeout < I2C_TIME_OUT) { - i2c_data_transmit(I2C0, reg); - timeout = 0; - // i2c_start_on_bus(I2C0); - } else { - return -4; // 超时返回错误 - } + ldc1612_iic_write_16bits(SET_FREQ_REG_START + channel, data); +} - delay_us(10); +/** @brief Error output config. + @param result The value to be set. + * */ +void ldc1612_set_error_config(uint16_t value) { + uint8_t data[2] = {0}; + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; - while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号 - timeout++; - if (timeout < I2C_TIME_OUT) { - i2c_start_on_bus(I2C0); - timeout = 0; - } else { - return -5; // 超时返回错误 - } + ldc1612_iic_write_16bits(ERROR_CONFIG_REG, data); +} - while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) { - timeout++; - } - if (timeout < I2C_TIME_OUT) { - i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER); - timeout = 0; - } else { - return -6; // 超时返回错误 - } +/** @brief mux config. + @param result The value to be set. + * */ +void ldc1612_set_mux_config(uint16_t value) { + uint8_t data[2] = {0}; + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; - 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); - timeout = 0; - } else { - return -7; // 超时返回错误 - } + ldc1612_iic_write_16bits(MUX_CONFIG_REG, data); +} - // 读取第一个字节的数据 - while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) { - timeout++; - } - if (timeout < I2C_TIME_OUT) { - *data = i2c_data_receive(I2C0) << 8; - timeout = 0; - } else { - return -8; // 超时返回错误 - } +/** @brief reset sensor. - i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK + * */ +void ldc1612_reset_sensor(void) { + uint8_t data[2] = {0}; + data[0] = 0x80; + data[1] = 0x00; + ldc1612_iic_write_16bits(SENSOR_RESET_REG, data); +} - // 读取第二个字节的数据 - while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) { - timeout++; - } - if (timeout < I2C_TIME_OUT) { - *data |= i2c_data_receive(I2C0); - timeout = 0; - } else { - return -9; // 超时返回错误 - } +/** @brief set drive current of sensor. + @param result The value to be set. + * */ +void ldc1612_set_drive_current(uint8_t channel, uint16_t value) { + uint8_t data[2] = {0}; + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; - i2c_stop_on_bus(I2C0); + ldc1612_iic_write_16bits(SET_DRIVER_CURRENT_REG + channel, data); +} + +/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable .. + @param result The value to be set. + * */ +void ldc1612_set_sensor_config(uint16_t value) { + uint8_t data[2] = {0}; + data[0] = (value >> 8) & 0xFF; + data[1] = value & 0xFF; + + ldc1612_iic_write_16bits(SENSOR_CONFIG_REG, data); +} + +void ldc1612_single_ch0_config(void) +{ + ldc1612_set_freq_divide(CHANNEL_0); //0x14 --0x1002 + + ldc1612_set_LC_stabilize_time(CHANNEL_0, LC_STABILIZE_TIME_CH0); //0x10 --0x001E + + ldc1612_set_conversion_time(CHANNEL_0, LDC1612_CONVERSION_TIME_CH0); //0x08 --0x0546 + + ldc1612_set_error_config(LDC1612_ERROR_CONFIG); //0x19 --0x0000) + + ldc1612_set_drive_current(CHANNEL_0, LDC1612_DRIVE_CURRENT); //0x1E --0x9000 + + ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C + + ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601 - // printf("device id = %x\r\n", (data[0] <<8 | data[1])); - return 0; } void ldc1612_iic_get_sensor_infomation(void) { - uint16_t data = 0; - LDC1612_IIC_read_16bits(READ_MANUFACTURER_ID, &data); - printf("\tManufacturer: 0x%x\r\n", data); - LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data); - printf("\tDevice: 0x%x\r\n", data); + uint8_t data[2] = {0}; + ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data); + printf("\tManufacturer: 0x%x\r\n", (data[0] << 8) | data[1]); + ldc1612_iic_read_16bits(READ_DEVICE_ID, data); + printf("\tDevice: 0x%x\r\n", (data[0] << 8) | data[1]); } uint16_t ldc1612_get_manufacturer_id(void) { @@ -308,9 +309,6 @@ uint16_t ldc1612_get_manufacturer_id(void) { } uint16_t ldc1612_get_deveice_id(void) { - // uint16_t data = 0; - // LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data); - // return data; uint8_t data[2] = {0}; ldc1612_iic_read_16bits(READ_DEVICE_ID, data); return (data[0] << 8) | data[1];