Compare commits

..

No commits in common. "529f1b3628ce16db6b50f46185c6ff63289ef7eb" and "e2c2e81247f370eecd01d3dee54d6f61f78d65b2" have entirely different histories.

3 changed files with 113 additions and 119 deletions

View File

@ -12,7 +12,6 @@
#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)
@ -49,20 +48,10 @@
/******************************************************************************/ /******************************************************************************/
#define LDC1612_CONVERSION_TIME_CH0 0X0546 //0536 #define CONVERSION_TIME_CH0 0X0546 //0536
#define LDC1612_DRIVE_CURRENT 0X9000 //A000 #define CONVERSION_OFFSET_CH0 0X0000
#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 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,
I2C_SEND_ADDRESS, I2C_SEND_ADDRESS,
@ -91,19 +80,9 @@ 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);

View File

@ -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,116 +190,115 @@ 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);
} }
/** @brief set input frequency divide and fref divide. int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data) {
@param channel LDC1612 has total two channels. uint16_t timeout = 0;
@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;
sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
fin_div = (uint16_t)(sensor_freq / 8.75 + 1); while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) //判断IIC总线是否忙发送起始信号
timeout++;
if (fin_div * 4 < 40) { if (timeout < I2C_TIME_OUT) {
freq_div = 2; i2c_start_on_bus(I2C0);
timeout = 0;
} else { } else {
freq_div = 4; printf("err\r\n");
return -1; // 超时返回错误
} }
value = fin_div << 12; while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送设置sensor地址并设置为写
value |= freq_div; timeout++;
// printf("\tvalue: 0x%x\r\n", value); if (timeout < I2C_TIME_OUT) {
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
timeout = 0;
} else {
return -2; // 超时返回错误
}
uint8_t data[2] = {0}; while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
data[0] = (value >> 8) & 0xFF; timeout++;
data[1] = value & 0xFF; if (timeout < I2C_TIME_OUT) {
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div); i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
timeout = 0;
} else {
return -3; // 超时返回错误
}
ldc1612_iic_write_16bits(SET_FREQ_REG_START + channel, data); 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; // 超时返回错误
}
/** @brief Error output config. delay_us(10);
@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;
ldc1612_iic_write_16bits(ERROR_CONFIG_REG, data); 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; // 超时返回错误
}
/** @brief mux config. while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
@param result The value to be set. timeout++;
* */ }
void ldc1612_set_mux_config(uint16_t value) { if (timeout < I2C_TIME_OUT) {
uint8_t data[2] = {0}; i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
data[0] = (value >> 8) & 0xFF; timeout = 0;
data[1] = value & 0xFF; } else {
return -6; // 超时返回错误
}
ldc1612_iic_write_16bits(MUX_CONFIG_REG, data); 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; // 超时返回错误
}
/** @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. // 读取第二个字节的数据
@param result The value to be set. while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
* */ timeout++;
void ldc1612_set_drive_current(uint8_t channel, uint16_t value) { }
uint8_t data[2] = {0}; if (timeout < I2C_TIME_OUT) {
data[0] = (value >> 8) & 0xFF; *data |= i2c_data_receive(I2C0);
data[1] = value & 0xFF; timeout = 0;
} else {
return -9; // 超时返回错误
}
ldc1612_iic_write_16bits(SET_DRIVER_CURRENT_REG + channel, data); i2c_stop_on_bus(I2C0);
}
/** @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) {
uint8_t data[2] = {0}; uint16_t data = 0;
ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data); LDC1612_IIC_read_16bits(READ_MANUFACTURER_ID, &data);
printf("\tManufacturer: 0x%x\r\n", (data[0] << 8) | data[1]); printf("\tManufacturer: 0x%x\r\n", data);
ldc1612_iic_read_16bits(READ_DEVICE_ID, data); LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data);
printf("\tDevice: 0x%x\r\n", (data[0] << 8) | data[1]); printf("\tDevice: 0x%x\r\n", data);
} }
uint16_t ldc1612_get_manufacturer_id(void) { uint16_t ldc1612_get_manufacturer_id(void) {
@ -309,6 +308,9 @@ 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];

View File

@ -62,8 +62,21 @@ int main(void) {
delay_ms(500); delay_ms(500);
ldc1612_iic_get_sensor_infomation(); ldc1612_iic_get_sensor_infomation();
ldc1612_single_ch0_config(); // uint16_t device_id = 0;
// device_id = ldc1612_get_deveice_id();
// printf("device id = 0x%x\r\n", device_id);
//
// delay_ms(100);
//
// device_id = 0;
// device_id = ldc1612_get_manufacturer_id();
// printf("device id = 0x%x\r\n", device_id);
uint16_t result = {0x0546};
uint8_t data[2] = {0};
data[0] = (result >> 8) & 0xFF;
data[1] = result & 0xFF;
printf("data[0] = 0x%x, data[1] = 0x%x\r\n", data[0], data[1]);
while (1) { while (1) {
// delay_ms(1000); // delay_ms(1000);