diff --git a/inc/LDC1612.h b/inc/LDC1612.h index ee6aa61..615e13f 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -31,14 +31,6 @@ #define SENSOR_RESET_REG 0X1C #define SET_DRIVER_CURRENT_REG 0X1E -#define RCOUNT_CH0_REG 0x08 -#define SETTLECOUNT_CH0_REG 0x10 -#define CLOCK_DIVIDERS_CH0_REG 0x14 -#define ERROR_CONFIG_REG 0X19 -#define MUX_CONFIG_REG 0x1B -#define DRIVE_CURRENT_CH0_REG 0x1E -#define SENSOR_CONFIG_REG 0X1A - #define READ_MANUFACTURER_ID 0X7E #define READ_DEVICE_ID 0X7F @@ -59,7 +51,7 @@ /******************************************************************************/ #define COIL_RP_KOM 15.727 -#define COIL_L_UH 18.147 +#define COIL_L_UH 33 #define COIL_C_PF 150 #define COIL_Q_FACTOR 35.97 @@ -85,29 +77,46 @@ typedef enum { #define I2C_SDA_PIN GPIO_PIN_0 #define I2C_GPIO_AF GPIO_AF_1 +void i2c_gpio_config(void); - -void I2C_config(void); -void gpio_config(void); void i2c_config(void); + void i2c_bus_reset(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); void ldc1612_iic_get_sensor_infomation(void); + uint16_t ldc1612_get_manufacturer_id(void); + uint16_t ldc1612_get_deveice_id(void); + +uint32_t ldc1612_get_raw_channel_result(uint8_t channel); + +uint32_t ldc1612_parse_raw_result(uint32_t raw_result); + uint8_t ldc1612_iic_read_16bits(uint8_t reg_addr, uint8_t *data); -int ldc1612_iic_write_16bits(uint8_t reg_addr, uint8_t data[2]); + +uint8_t ldc1612_iic_write_16bits(uint8_t reg_addr, uint8_t data[2]); #endif //LDC1612_H diff --git a/src/LDC1612.c b/src/LDC1612.c index 2ddbd8f..66c2282 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -4,33 +4,13 @@ #include "LDC1612.h" -void I2C_config(void) { - rcu_periph_clock_enable(RCU_GPIO_I2C); - 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); -} - /*! \brief configure the GPIO ports \param[in] none \param[out] none \retval none */ -void gpio_config(void) { +void i2c_gpio_config(void) { /* enable IIC GPIO clock */ rcu_periph_clock_enable(RCU_GPIO_I2C); @@ -202,7 +182,7 @@ void ldc1612_set_freq_divide(uint8_t channel) { sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6); - fin_div = (uint16_t)(sensor_freq / 8.75 + 1); + fin_div = (uint16_t) (sensor_freq / 8.75 + 1); if (fin_div * 4 < 40) { freq_div = 2; @@ -241,7 +221,7 @@ void ldc1612_set_mux_config(uint16_t value) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; - ldc1612_iic_write_16bits(MUX_CONFIG_REG, data); + ldc1612_iic_write_16bits(MUL_CONFIG_REG, data); } /** @brief reset sensor. @@ -276,22 +256,20 @@ void ldc1612_set_sensor_config(uint16_t value) { ldc1612_iic_write_16bits(SENSOR_CONFIG_REG, data); } -void ldc1612_single_ch0_config(void) -{ - ldc1612_set_freq_divide(CHANNEL_0); //0x14 --0x1002 +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_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_conversion_time(CHANNEL_0, LDC1612_CONVERSION_TIME_CH0); //0x08 --0x0546 - ldc1612_set_error_config(LDC1612_ERROR_CONFIG); //0x19 --0x0000) + ldc1612_set_error_config(LDC1612_ERROR_CONFIG); //0x19 --0x0000) - ldc1612_set_drive_current(CHANNEL_0, LDC1612_DRIVE_CURRENT); //0x1E --0x9000 + 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 + ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C + ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601 } void ldc1612_iic_get_sensor_infomation(void) { @@ -314,6 +292,58 @@ uint16_t ldc1612_get_deveice_id(void) { return (data[0] << 8) | data[1]; } +/** @brief read the raw channel result from register. + @param channel LDC1612 has total two channels. + @param result raw data + * */ +uint32_t ldc1612_get_raw_channel_result(uint8_t channel) { + uint32_t raw_value = 0; + uint8_t value[2] = {0}; + + ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); + raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16; + ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel + 1, value); + raw_value |= (uint32_t) ((value[0] << 8) | value[1]); + return ldc1612_parse_raw_result(raw_value); +} + +/** @brief parse the data which read from data register. + @param channel LDC1612 has total two channels. + @param raw_result the raw data which read from data register,it contains error codes and sensor value; + * */ +uint32_t ldc1612_parse_raw_result(uint32_t raw_result) { + uint32_t calibration_value = 0; + uint8_t error_code = 0; + + calibration_value = raw_result & 0x0FFFFFFF; + if (0xFFFFFFF == calibration_value) { + return 0xF0000000; + // ERR_NC-No coil detected!!! + } + + error_code = raw_result >> 24; + + if (error_code & 0x80) { + return 0x80000000; + // ERR_UR-Under range error!!! + } + if (error_code & 0x40) { + return 0x40000000; + // ERR_OR-Over range error!!! + } + if (error_code & 0x20) { + return 0x20000000; + // ERR_WD-Watch dog timeout error!!! + } + if (error_code & 0x10) { + return 0x10000000; + // ERR_AE-error!!! + } + + return raw_result; +} + + uint8_t ldc1612_iic_read_16bits(uint8_t reg_addr, uint8_t *data) { uint8_t state = I2C_START; uint8_t read_cycle = 0; @@ -478,7 +508,7 @@ uint8_t ldc1612_iic_read_16bits(uint8_t reg_addr, uint8_t *data) { return I2C_END; } -int ldc1612_iic_write_16bits(uint8_t reg_addr, uint8_t data[2]) { +uint8_t ldc1612_iic_write_16bits(uint8_t reg_addr, uint8_t data[2]) { uint8_t state = I2C_START; uint16_t timeout = 0; uint8_t i2c_timeout_flag = 0; diff --git a/src/main.c b/src/main.c index fd76f3e..472d74f 100644 --- a/src/main.c +++ b/src/main.c @@ -39,9 +39,6 @@ void led_config(void) { nvic_irq_enable(LED_IRQ, 2); } -uint8_t RCOUNT_ALL[3] = {SET_CONVERSION_TIME_REG_START, 0x05, 0x36}; -uint8_t SETTLECOUNT0_ALL[3] = {SET_CONVERSION_TIME_REG_START, 0x05, 0x36}; - /*! \brief main function \param[in] none @@ -53,21 +50,20 @@ int main(void) { systick_config(); RS485_config(); led_config(); - I2C_config(); + i2c_gpio_config(); + i2c_config(); - printf("\r\n"); - printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); - printf("\r\n"); + uint32_t raw_value = 0; delay_ms(500); ldc1612_iic_get_sensor_infomation(); ldc1612_single_ch0_config(); - while (1) { - // delay_ms(1000); - // printf("OK!!!\r\n"); + delay_ms(500); + raw_value = ldc1612_get_raw_channel_result(CHANNEL_0); + printf("data:%ld\r\n",raw_value); } }