generated from hulk/gd32e23x_template
Porting the seeed open source library
This commit is contained in:
parent
e2c2e81247
commit
5877465628
@ -12,6 +12,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#define LDC1612_ADDR (0x2B << 1)
|
#define LDC1612_ADDR (0x2B << 1)
|
||||||
|
|
||||||
@ -48,9 +49,19 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define CONVERSION_TIME_CH0 0X0546 //0536
|
#define LDC1612_CONVERSION_TIME_CH0 0X0546 //0536
|
||||||
#define CONVERSION_OFFSET_CH0 0X0000
|
#define LDC1612_DRIVE_CURRENT 0X9000 //A000
|
||||||
#define LC_STABILIZE_TIME_CH0 0X001E //30
|
#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 {
|
typedef enum {
|
||||||
I2C_START = 0,
|
I2C_START = 0,
|
||||||
@ -80,9 +91,19 @@ void I2C_config(void);
|
|||||||
void gpio_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_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);
|
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);
|
||||||
|
186
src/LDC1612.c
186
src/LDC1612.c
@ -104,7 +104,7 @@ void i2c_bus_reset(void) {
|
|||||||
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||||
* 最后会打印出找到的设备总数。
|
* 最后会打印出找到的设备总数。
|
||||||
*/
|
*/
|
||||||
void I2C_scan(void) {
|
void i2c_scan(void) {
|
||||||
uint32_t timeout;
|
uint32_t timeout;
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
int found_devices = 0;
|
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);
|
ldc1612_iic_write_16bits(SET_LC_STABILIZE_REG_START + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data) {
|
/** @brief set input frequency divide and fref divide.
|
||||||
uint16_t timeout = 0;
|
@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总线是否忙,发送起始信号
|
fin_div = (uint16_t)(sensor_freq / 8.75 + 1);
|
||||||
timeout++;
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (fin_div * 4 < 40) {
|
||||||
i2c_start_on_bus(I2C0);
|
freq_div = 2;
|
||||||
timeout = 0;
|
|
||||||
} else {
|
} else {
|
||||||
printf("err\r\n");
|
freq_div = 4;
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写
|
value = fin_div << 12;
|
||||||
timeout++;
|
value |= freq_div;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
// printf("\tvalue: 0x%x\r\n", value);
|
||||||
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
return -2; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
uint8_t data[2] = {0};
|
||||||
timeout++;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
data[1] = value & 0xFF;
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
return -3; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址
|
ldc1612_iic_write_16bits(SET_FREQ_REG_START + channel, data);
|
||||||
timeout++;
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_data_transmit(I2C0, reg);
|
|
||||||
timeout = 0;
|
|
||||||
// i2c_start_on_bus(I2C0);
|
|
||||||
} else {
|
|
||||||
return -4; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
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)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
|
ldc1612_iic_write_16bits(ERROR_CONFIG_REG, data);
|
||||||
timeout++;
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
return -5; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
|
/** @brief mux config.
|
||||||
timeout++;
|
@param result The value to be set.
|
||||||
}
|
* */
|
||||||
if (timeout < I2C_TIME_OUT) {
|
void ldc1612_set_mux_config(uint16_t value) {
|
||||||
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
uint8_t data[2] = {0};
|
||||||
timeout = 0;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
} else {
|
data[1] = value & 0xFF;
|
||||||
return -6; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
ldc1612_iic_write_16bits(MUX_CONFIG_REG, data);
|
||||||
timeout++;
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
return -7; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取第一个字节的数据
|
/** @brief reset sensor.
|
||||||
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; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
// 读取第二个字节的数据
|
/** @brief set drive current of sensor.
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
@param result The value to be set.
|
||||||
timeout++;
|
* */
|
||||||
}
|
void ldc1612_set_drive_current(uint8_t channel, uint16_t value) {
|
||||||
if (timeout < I2C_TIME_OUT) {
|
uint8_t data[2] = {0};
|
||||||
*data |= i2c_data_receive(I2C0);
|
data[0] = (value >> 8) & 0xFF;
|
||||||
timeout = 0;
|
data[1] = value & 0xFF;
|
||||||
} else {
|
|
||||||
return -9; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
void ldc1612_iic_get_sensor_infomation(void) {
|
||||||
uint16_t data = 0;
|
uint8_t data[2] = {0};
|
||||||
LDC1612_IIC_read_16bits(READ_MANUFACTURER_ID, &data);
|
ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data);
|
||||||
printf("\tManufacturer: 0x%x\r\n", data);
|
printf("\tManufacturer: 0x%x\r\n", (data[0] << 8) | data[1]);
|
||||||
LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data);
|
ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
|
||||||
printf("\tDevice: 0x%x\r\n", data);
|
printf("\tDevice: 0x%x\r\n", (data[0] << 8) | data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
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 ldc1612_get_deveice_id(void) {
|
||||||
// uint16_t data = 0;
|
|
||||||
// LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data);
|
|
||||||
// return data;
|
|
||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
|
ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
|
||||||
return (data[0] << 8) | data[1];
|
return (data[0] << 8) | data[1];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user