generated from hulk/gd32e23x_template
Complete data reading
This commit is contained in:
parent
529f1b3628
commit
c88a383c29
@ -31,14 +31,6 @@
|
|||||||
#define SENSOR_RESET_REG 0X1C
|
#define SENSOR_RESET_REG 0X1C
|
||||||
#define SET_DRIVER_CURRENT_REG 0X1E
|
#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_MANUFACTURER_ID 0X7E
|
||||||
#define READ_DEVICE_ID 0X7F
|
#define READ_DEVICE_ID 0X7F
|
||||||
|
|
||||||
@ -59,7 +51,7 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define COIL_RP_KOM 15.727
|
#define COIL_RP_KOM 15.727
|
||||||
#define COIL_L_UH 18.147
|
#define COIL_L_UH 33
|
||||||
#define COIL_C_PF 150
|
#define COIL_C_PF 150
|
||||||
#define COIL_Q_FACTOR 35.97
|
#define COIL_Q_FACTOR 35.97
|
||||||
|
|
||||||
@ -85,29 +77,46 @@ typedef enum {
|
|||||||
#define I2C_SDA_PIN GPIO_PIN_0
|
#define I2C_SDA_PIN GPIO_PIN_0
|
||||||
#define I2C_GPIO_AF GPIO_AF_1
|
#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_config(void);
|
||||||
|
|
||||||
void i2c_bus_reset(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_time(uint8_t channel, uint16_t result);
|
||||||
|
|
||||||
void ldc1612_set_conversion_offset(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_LC_stabilize_time(uint8_t channel, uint16_t result);
|
||||||
|
|
||||||
void ldc1612_set_freq_divide(uint8_t channel);
|
void ldc1612_set_freq_divide(uint8_t channel);
|
||||||
|
|
||||||
void ldc1612_set_error_config(uint16_t value);
|
void ldc1612_set_error_config(uint16_t value);
|
||||||
|
|
||||||
void ldc1612_set_mux_config(uint16_t value);
|
void ldc1612_set_mux_config(uint16_t value);
|
||||||
|
|
||||||
void ldc1612_reset_sensor(void);
|
void ldc1612_reset_sensor(void);
|
||||||
|
|
||||||
void ldc1612_set_drive_current(uint8_t channel, uint16_t value);
|
void ldc1612_set_drive_current(uint8_t channel, uint16_t value);
|
||||||
|
|
||||||
void ldc1612_set_sensor_config(uint16_t value);
|
void ldc1612_set_sensor_config(uint16_t value);
|
||||||
|
|
||||||
void ldc1612_single_ch0_config(void);
|
void ldc1612_single_ch0_config(void);
|
||||||
|
|
||||||
void ldc1612_iic_get_sensor_infomation(void);
|
void ldc1612_iic_get_sensor_infomation(void);
|
||||||
|
|
||||||
uint16_t ldc1612_get_manufacturer_id(void);
|
uint16_t ldc1612_get_manufacturer_id(void);
|
||||||
|
|
||||||
uint16_t ldc1612_get_deveice_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);
|
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
|
#endif //LDC1612_H
|
||||||
|
@ -4,33 +4,13 @@
|
|||||||
|
|
||||||
#include "LDC1612.h"
|
#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
|
\brief configure the GPIO ports
|
||||||
\param[in] none
|
\param[in] none
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void gpio_config(void) {
|
void i2c_gpio_config(void) {
|
||||||
/* enable IIC GPIO clock */
|
/* enable IIC GPIO clock */
|
||||||
rcu_periph_clock_enable(RCU_GPIO_I2C);
|
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);
|
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) {
|
if (fin_div * 4 < 40) {
|
||||||
freq_div = 2;
|
freq_div = 2;
|
||||||
@ -241,7 +221,7 @@ void ldc1612_set_mux_config(uint16_t value) {
|
|||||||
data[0] = (value >> 8) & 0xFF;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
|
|
||||||
ldc1612_iic_write_16bits(MUX_CONFIG_REG, data);
|
ldc1612_iic_write_16bits(MUL_CONFIG_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief reset sensor.
|
/** @brief reset sensor.
|
||||||
@ -276,8 +256,7 @@ void ldc1612_set_sensor_config(uint16_t value) {
|
|||||||
ldc1612_iic_write_16bits(SENSOR_CONFIG_REG, data);
|
ldc1612_iic_write_16bits(SENSOR_CONFIG_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ldc1612_single_ch0_config(void)
|
void ldc1612_single_ch0_config(void) {
|
||||||
{
|
|
||||||
ldc1612_set_freq_divide(CHANNEL_0); //0x14 --0x1002
|
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
|
||||||
@ -291,7 +270,6 @@ void ldc1612_single_ch0_config(void)
|
|||||||
ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C
|
ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C
|
||||||
|
|
||||||
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
|
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ldc1612_iic_get_sensor_infomation(void) {
|
void ldc1612_iic_get_sensor_infomation(void) {
|
||||||
@ -314,6 +292,58 @@ uint16_t ldc1612_get_deveice_id(void) {
|
|||||||
return (data[0] << 8) | data[1];
|
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 ldc1612_iic_read_16bits(uint8_t reg_addr, uint8_t *data) {
|
||||||
uint8_t state = I2C_START;
|
uint8_t state = I2C_START;
|
||||||
uint8_t read_cycle = 0;
|
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;
|
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;
|
uint8_t state = I2C_START;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
uint8_t i2c_timeout_flag = 0;
|
uint8_t i2c_timeout_flag = 0;
|
||||||
|
16
src/main.c
16
src/main.c
@ -39,9 +39,6 @@ void led_config(void) {
|
|||||||
nvic_irq_enable(LED_IRQ, 2);
|
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
|
\brief main function
|
||||||
\param[in] none
|
\param[in] none
|
||||||
@ -53,21 +50,20 @@ int main(void) {
|
|||||||
systick_config();
|
systick_config();
|
||||||
RS485_config();
|
RS485_config();
|
||||||
led_config();
|
led_config();
|
||||||
I2C_config();
|
i2c_gpio_config();
|
||||||
|
i2c_config();
|
||||||
|
|
||||||
printf("\r\n");
|
uint32_t raw_value = 0;
|
||||||
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
|
||||||
printf("\r\n");
|
|
||||||
|
|
||||||
delay_ms(500);
|
delay_ms(500);
|
||||||
ldc1612_iic_get_sensor_infomation();
|
ldc1612_iic_get_sensor_infomation();
|
||||||
|
|
||||||
ldc1612_single_ch0_config();
|
ldc1612_single_ch0_config();
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// delay_ms(1000);
|
delay_ms(500);
|
||||||
// printf("OK!!!\r\n");
|
raw_value = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||||
|
printf("data:%ld\r\n",raw_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user