diff --git a/inc/LDC1612.h b/inc/LDC1612.h index 1cb1865..d705296 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -13,12 +13,25 @@ #include #include -#define LED_PORT GPIOA -#define LED_PIN GPIO_PIN_7 -#define LED_RCU RCU_GPIOA -#define LED_TIMER_RCU RCU_TIMER16 -#define LED_TIMER TIMER16 -#define LED_IRQ TIMER16_IRQn +/*Register Rddr*/ +/***************************************************************************/ +#define CONVERTION_RESULT_REG_START 0X00 +#define SET_CONVERSION_TIME_REG_START 0X08 +#define SET_CONVERSION_OFFSET_REG_START 0X0C +#define SET_LC_STABILIZE_REG_START 0X10 +#define SET_FREQ_REG_START 0X14 + +#define SENSOR_STATUS_REG 0X18 +#define ERROR_CONFIG_REG 0X19 +#define SENSOR_CONFIG_REG 0X1A +#define MUL_CONFIG_REG 0X1B +#define SENSOR_RESET_REG 0X1C +#define SET_DRIVER_CURRENT_REG 0X1E + +#define READ_MANUFACTURER_ID 0X7E +#define READ_DEVICE_ID 0X7F + +/******************************************************************************/ #define I2C_TIME_OUT (uint16_t)(10000) #define LDC_I2C I2C0 @@ -55,7 +68,10 @@ typedef enum { #define DRIVE_CURRENT0_ADDR 0x1E #define CONFIG_ADDR 0x1A -void led_config(void); + + +void LDC1612_read_sensor_infomation(void); + void LDC1612_I2CConfig(void); void LDC1612_Init(void); int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length); @@ -65,6 +81,8 @@ uint8_t eeprom_byte_write_timeout(uint8_t device_address, uint8_t *data); void I2C_Scan(void); +uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value); + int LDC_getID(void); #endif //LDC1612_H diff --git a/inc/main.h b/inc/main.h index dd037de..aa626bc 100644 --- a/inc/main.h +++ b/inc/main.h @@ -35,4 +35,13 @@ OF SUCH DAMAGE. #ifndef MAIN_H #define MAIN_H +#define LED_PORT GPIOA +#define LED_PIN GPIO_PIN_7 +#define LED_RCU RCU_GPIOA +#define LED_TIMER_RCU RCU_TIMER16 +#define LED_TIMER TIMER16 +#define LED_IRQ TIMER16_IRQn + +void led_config(void); + #endif /* MAIN_H */ diff --git a/src/LDC1612.c b/src/LDC1612.c index 8073600..728afbf 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -4,36 +4,27 @@ #include "LDC1612.h" -void led_config(void) + +void LDC1612_read_sensor_infomation(void) { - rcu_periph_clock_enable(LED_RCU); + uint16_t device_id; + uint8_t result; - gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); - gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); - gpio_bit_write(LED_PORT, LED_PIN, SET); + // 读取设备ID + result = IIC_read_16bit(READ_DEVICE_ID, &device_id); - rcu_periph_clock_enable(LED_TIMER_RCU); - timer_deinit(LED_TIMER); + if (result == 0) { + printf("Device ID: 0x%04X\n", device_id); + } else { + printf("Failed to read Device ID\n"); + } - timer_parameter_struct timer_initpara; - timer_struct_para_init(&timer_initpara); - timer_initpara.prescaler = 799; - timer_initpara.alignedmode = TIMER_COUNTER_EDGE; - timer_initpara.counterdirection = TIMER_COUNTER_UP; - timer_initpara.period = 999; - timer_initpara.clockdivision = TIMER_CKDIV_DIV1; - timer_init(LED_TIMER, &timer_initpara); - - timer_auto_reload_shadow_enable(LED_TIMER); - timer_interrupt_enable(LED_TIMER, TIMER_INT_UP); - - timer_enable(LED_TIMER); - - nvic_irq_enable(LED_IRQ, 2); } + + /** * @brief This function configure the I2C peripheral & GPIO * @param[in] none @@ -60,6 +51,7 @@ void LDC1612_I2CConfig(void) { i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); } + /** * @brief 向I2C设备寄存器写入数据 * @param device_address: 设备地址 @@ -455,6 +447,77 @@ void I2C_Scan(void) { } + +/** + * @brief 读取16位I2C数据,带有超时机制 + * @param reg: 要读取的寄存器地址 + * @param value: 存储读取数据的指针 + * @retval 0: 成功,1: 超时 + */ +uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) { + uint32_t timeout = 0; + + // 发送启动信号 + i2c_start_on_bus(I2C0); + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + + // 发送从设备地址和写信号 + i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER); + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); + + // 发送寄存器地址 + i2c_data_transmit(I2C0, reg); + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_TBE)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + + // 重新启动信号 + i2c_start_on_bus(I2C0); + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + + // 发送从设备地址和读信号 + i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER); + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); + + // 读取数据 + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + *value = i2c_data_receive(I2C0) << 8; // 读取高8位 + + timeout = 0; + while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + *value |= i2c_data_receive(I2C0); // 读取低8位 + + // 发送停止信号 + i2c_stop_on_bus(I2C0); + timeout = 0; + while (I2C_CTL0(I2C0) & I2C_CTL0_STOP) { + if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + } + + return 0; // 成功 +} + + int LDC_getID(void) { uint8_t data[2] = {0}; uint16_t timeout = 0; diff --git a/src/main.c b/src/main.c index ba3a2c3..91783cf 100644 --- a/src/main.c +++ b/src/main.c @@ -9,12 +9,37 @@ #include "gd32e23x.h" #include "systick.h" #include "gd32e23x_libopt.h" - -#include "peripheral.h" - #include "RS485.h" #include "LDC1612.h" +void led_config(void) +{ + rcu_periph_clock_enable(LED_RCU); + + gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); + gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); + gpio_bit_write(LED_PORT, LED_PIN, SET); + + rcu_periph_clock_enable(LED_TIMER_RCU); + timer_deinit(LED_TIMER); + + timer_parameter_struct timer_initpara; + timer_struct_para_init(&timer_initpara); + timer_initpara.prescaler = 799; + timer_initpara.alignedmode = TIMER_COUNTER_EDGE; + timer_initpara.counterdirection = TIMER_COUNTER_UP; + timer_initpara.period = 999; + timer_initpara.clockdivision = TIMER_CKDIV_DIV1; + timer_init(LED_TIMER, &timer_initpara); + + timer_auto_reload_shadow_enable(LED_TIMER); + timer_interrupt_enable(LED_TIMER, TIMER_INT_UP); + + timer_enable(LED_TIMER); + + nvic_irq_enable(LED_IRQ, 2); +} + /*! \brief main function \param[in] none @@ -28,7 +53,7 @@ int main(void) RS485_config(); led_config(); - delay_ms(5000); + // delay_ms(5000); printf("\r\n"); printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); printf("\r\n"); @@ -36,13 +61,11 @@ int main(void) LDC1612_I2CConfig(); // LDC1612_Init(); // I2C_Scan(); -int ret = 0; - ret = LDC_getID(); - printf("ret:%d\r\n",ret); + LDC1612_read_sensor_infomation(); + + - uint8_t data[2] = {0}; - uint32_t value = 0; while(1){ // LDC1612_read_reg(LDC1612_ADDR, 0x7f, data, 2);