generated from hulk/gd32e23x_template
Compare commits
8 Commits
890287a706
...
main
Author | SHA1 | Date | |
---|---|---|---|
d6f3ea33b1 | |||
de94a72f72 | |||
44cf0426e5 | |||
c90ecf05f9 | |||
38849856af | |||
f508a7cd2d | |||
e0b9aab103 | |||
1d1c0fce21 |
@@ -94,9 +94,12 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
|
|||||||
#set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
#set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
||||||
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
||||||
#set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
#set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
|
||||||
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
|
||||||
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
|
||||||
|
#set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
||||||
|
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
||||||
|
#set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
|
||||||
|
|
||||||
# RELEASE
|
# RELEASE
|
||||||
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O3") # -flto
|
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O3") # -flto
|
||||||
|
@@ -13,6 +13,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define LDC1612_ADDR (0x2B << 1)
|
||||||
|
|
||||||
/*Register Rddr*/
|
/*Register Rddr*/
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
#define CONVERTION_RESULT_REG_START 0X00
|
#define CONVERTION_RESULT_REG_START 0X00
|
||||||
@@ -33,9 +35,6 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_TIME_OUT (uint16_t)(10000)
|
|
||||||
#define LDC_I2C I2C0
|
|
||||||
#define I2C_SPEED 400000
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
I2C_START = 0,
|
I2C_START = 0,
|
||||||
@@ -45,10 +44,7 @@ typedef enum {
|
|||||||
I2C_STOP
|
I2C_STOP
|
||||||
} i2c_process_enum;
|
} i2c_process_enum;
|
||||||
|
|
||||||
#define I2C_OK 1
|
#define I2C_SPEED 100000
|
||||||
#define I2C_FAIL 0
|
|
||||||
#define I2C_END 1
|
|
||||||
|
|
||||||
#define RCU_IR_GPIO RCU_GPIOF
|
#define RCU_IR_GPIO RCU_GPIOF
|
||||||
#define RCU_I2C RCU_I2C0
|
#define RCU_I2C RCU_I2C0
|
||||||
#define I2C_SCL_PORT GPIOF
|
#define I2C_SCL_PORT GPIOF
|
||||||
@@ -57,32 +53,13 @@ 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
|
||||||
|
|
||||||
#define LDC1612_ADDR (0x2B << 1)
|
#define I2C_TIME_OUT (uint16_t)(10000)
|
||||||
|
|
||||||
// LDC1612 寄存器地址
|
void I2C_config(void);
|
||||||
#define RCOUNT0_ADDR 0x08
|
void I2C_scan(void);
|
||||||
#define SETTLECOUNT0_ADDR 0x10
|
|
||||||
#define CLOCK_DIVIDERS0_ADDR 0x14
|
int LDC1612_IIC_read_16bits(void);
|
||||||
#define ERROR_CONFIG_ADDR 0x19
|
|
||||||
#define MUX_CONFIG_ADDR 0x1B
|
|
||||||
#define DRIVE_CURRENT0_ADDR 0x1E
|
|
||||||
#define CONFIG_ADDR 0x1A
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
int i2c_write_reg(uint8_t device_address, uint8_t *data);
|
|
||||||
|
|
||||||
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
|
#endif //LDC1612_H
|
||||||
|
@@ -43,5 +43,7 @@ OF SUCH DAMAGE.
|
|||||||
#define LED_IRQ TIMER16_IRQn
|
#define LED_IRQ TIMER16_IRQn
|
||||||
|
|
||||||
void led_config(void);
|
void led_config(void);
|
||||||
|
void iicSendData(void);
|
||||||
|
void iicReceiveData(void);
|
||||||
|
|
||||||
#endif /* MAIN_H */
|
#endif /* MAIN_H */
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
# 连接cmsis-dap(喝粥)
|
# 连接cmsis-dap(喝粥)
|
||||||
; interface cmsis-dap
|
interface cmsis-dap
|
||||||
source [find interface/cmsis-dap.cfg]
|
source [find interface/cmsis-dap.cfg]
|
||||||
|
|
||||||
# 选择SWD
|
# 选择SWD
|
||||||
@@ -7,3 +7,8 @@ transport select swd
|
|||||||
|
|
||||||
# 加载gd32e23x.cfg
|
# 加载gd32e23x.cfg
|
||||||
source [find target/gd32e23x.cfg]
|
source [find target/gd32e23x.cfg]
|
||||||
|
|
||||||
|
; set CHIPNAME gd32e23x
|
||||||
|
;
|
||||||
|
; reset_config srst_only
|
||||||
|
adapter speed 500
|
||||||
|
516
src/LDC1612.c
516
src/LDC1612.c
@@ -5,33 +5,8 @@
|
|||||||
#include "LDC1612.h"
|
#include "LDC1612.h"
|
||||||
|
|
||||||
|
|
||||||
void LDC1612_read_sensor_infomation(void)
|
|
||||||
{
|
|
||||||
uint16_t device_id;
|
|
||||||
uint8_t result;
|
|
||||||
|
|
||||||
// 读取设备ID
|
void I2C_config(void) {
|
||||||
result = IIC_read_16bit(READ_DEVICE_ID, &device_id);
|
|
||||||
|
|
||||||
if (result == 0) {
|
|
||||||
printf("Device ID: 0x%04X\n", device_id);
|
|
||||||
} else {
|
|
||||||
printf("Failed to read Device ID\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief This function configure the I2C peripheral & GPIO
|
|
||||||
* @param[in] none
|
|
||||||
* @param[out] none
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
void LDC1612_I2CConfig(void) {
|
|
||||||
rcu_periph_clock_enable(RCU_IR_GPIO);
|
rcu_periph_clock_enable(RCU_IR_GPIO);
|
||||||
rcu_periph_clock_enable(RCU_I2C);
|
rcu_periph_clock_enable(RCU_I2C);
|
||||||
|
|
||||||
@@ -44,357 +19,32 @@ void LDC1612_I2CConfig(void) {
|
|||||||
gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_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);
|
gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
|
||||||
|
|
||||||
i2c_clock_config(LDC_I2C, I2C_SPEED, I2C_DTCY_2);
|
i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2);
|
||||||
|
|
||||||
i2c_mode_addr_config(LDC_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR);
|
// i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR);
|
||||||
i2c_enable(LDC_I2C);
|
i2c_enable(I2C0);
|
||||||
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// void LDC1612_Init(void) {
|
||||||
|
// uint8_t RCOUNT0_ALL[3]={SET_CONVERSION_TIME_REG_START,0x05,0x36};//csdn
|
||||||
|
// uint8_t SETTLECOUNT0_ALL[3]={SET_LC_STABILIZE_REG_START,0x00,0x0a};
|
||||||
|
// uint8_t CLOCK_DIVIDERS0_ALL[3]={SET_FREQ_REG_START,0x10,0x02};
|
||||||
|
// uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_REG,0x00,0x00};
|
||||||
|
// uint8_t MUX_CONFIG_ALL[3]={MUL_CONFIG_REG,0x82,0x0c};
|
||||||
|
// uint8_t DRIVE_CURRENT0_ALL[3]={SET_DRIVER_CURRENT_REG,0x90,0x00};
|
||||||
|
// uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 向I2C设备寄存器写入数据
|
* @brief 扫描I2C总线,查找连接的设备
|
||||||
* @param device_address: 设备地址
|
*
|
||||||
* @param data: 要写入的数据
|
* 该函数会扫描I2C总线上的所有地址(1到126),并尝试与每个地址进行通信。
|
||||||
* @retval int: 0表示成功,-1表示超时
|
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||||
|
* 最后会打印出找到的设备总数。
|
||||||
*/
|
*/
|
||||||
int i2c_write_reg(uint8_t device_address, uint8_t *data)
|
void I2C_scan(void) {
|
||||||
{
|
|
||||||
uint32_t timeout = 0;
|
|
||||||
printf("debug tag- 0\r\n");
|
|
||||||
// 确保 I2C 总线空闲
|
|
||||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
printf("debug tag- 1-\r\n");
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_start_on_bus(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
printf("debug tag- 1\r\n");
|
|
||||||
|
|
||||||
// 等待起始条件发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER);
|
|
||||||
timeout = 0;
|
|
||||||
printf("debug tag- 2\r\n");
|
|
||||||
|
|
||||||
// 等待地址发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
printf("debug tag- 3-\r\n");
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
printf("debug tag- 3\r\n");
|
|
||||||
|
|
||||||
// 发送寄存器地址和数据
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_data_transmit(LDC_I2C, data[i]);
|
|
||||||
printf("iic_value:%x\r\n", data[i]);
|
|
||||||
timeout = 0;
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 生成停止条件
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_stop_on_bus(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 确保停止条件已发送完成
|
|
||||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("test info\r\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t eeprom_byte_write_timeout(uint8_t device_address, uint8_t *data)
|
|
||||||
{
|
|
||||||
uint8_t state = I2C_START;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
uint8_t i2c_timeout_flag = 0;
|
|
||||||
|
|
||||||
/* enable acknowledge */
|
|
||||||
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE);
|
|
||||||
while(!(i2c_timeout_flag)) {
|
|
||||||
switch(state) {
|
|
||||||
case I2C_START:
|
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
|
||||||
while(i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_start_on_bus(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c bus is busy in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_SEND_ADDRESS:
|
|
||||||
/* i2c master sends START signal successfully */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
|
||||||
/* address flag set means i2c slave sends ACK */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_TRANSMIT_DATA;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_TRANSMIT_DATA:
|
|
||||||
/* wait until the transmit data buffer is empty */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
/* send the EEPROM's internal address to write to : only one byte address */
|
|
||||||
i2c_data_transmit(LDC_I2C, data[0]);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
/* send the EEPROM's internal address to write to : only one byte address */
|
|
||||||
i2c_data_transmit(LDC_I2C, data[1]);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends data timeout in WRITE 1 BYTE!\n");
|
|
||||||
}
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
/* send the EEPROM's internal address to write to : only one byte address */
|
|
||||||
i2c_data_transmit(LDC_I2C, data[2]);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends data timeout in WRITE 2 BYTE!\n");
|
|
||||||
}
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
state = I2C_STOP;
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_STOP:
|
|
||||||
/* send a stop condition to I2C bus */
|
|
||||||
i2c_stop_on_bus(LDC_I2C);
|
|
||||||
/* i2c master sends STOP signal successfully */
|
|
||||||
while((I2C_CTL0(LDC_I2C) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if(timeout < I2C_TIME_OUT) {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_END;
|
|
||||||
i2c_timeout_flag = I2C_OK;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
state = I2C_START;
|
|
||||||
i2c_timeout_flag = I2C_OK;
|
|
||||||
timeout = 0;
|
|
||||||
printf("i2c master sends start signal in WRITE BYTE.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return I2C_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LDC1612_Init(void) {
|
|
||||||
uint8_t RCOUNT0_ALL[3]={RCOUNT0_ADDR,0x05,0x36};//csdn
|
|
||||||
uint8_t SETTLECOUNT0_ALL[3]={SETTLECOUNT0_ADDR,0x00,0x0a};
|
|
||||||
uint8_t CLOCK_DIVIDERS0_ALL[3]={CLOCK_DIVIDERS0_ADDR,0x10,0x02};
|
|
||||||
uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_ADDR,0x00,0x00};
|
|
||||||
uint8_t MUX_CONFIG_ALL[3]={MUX_CONFIG_ADDR,0x82,0x0c};
|
|
||||||
uint8_t DRIVE_CURRENT0_ALL[3]={DRIVE_CURRENT0_ADDR,0x90,0x00};
|
|
||||||
uint8_t CONFIG_ALL[3]={CONFIG_ADDR,0x14,0x01};//csdn
|
|
||||||
|
|
||||||
// LDC1612_I2CConfig();
|
|
||||||
|
|
||||||
// int ret = i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL);
|
|
||||||
//
|
|
||||||
// printf("ret:%d\r\n",ret);
|
|
||||||
i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL);
|
|
||||||
// eeprom_byte_write_timeout(LDC1612_ADDR, SETTLECOUNT0_ALL);
|
|
||||||
delay_ms(100);
|
|
||||||
i2c_write_reg(LDC1612_ADDR, SETTLECOUNT0_ALL);
|
|
||||||
delay_ms(100);
|
|
||||||
// i2c_write_reg(LDC1612_ADDR, CLOCK_DIVIDERS0_ALL);
|
|
||||||
// delay_ms(100);
|
|
||||||
// i2c_write_reg(LDC1612_ADDR, ERROR_CONFIG_ALL);
|
|
||||||
// delay_ms(100);
|
|
||||||
// i2c_write_reg(LDC1612_ADDR, MUX_CONFIG_ALL);
|
|
||||||
// delay_ms(100);
|
|
||||||
// i2c_write_reg(LDC1612_ADDR, DRIVE_CURRENT0_ALL);
|
|
||||||
// delay_ms(100);
|
|
||||||
// i2c_write_reg(LDC1612_ADDR, CONFIG_ALL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从I2C设备寄存器读取数据
|
|
||||||
* @param device_address: 设备地址
|
|
||||||
* @param reg_address: 寄存器地址
|
|
||||||
* @param data: 存储读取数据的缓冲区
|
|
||||||
* @param length: 要读取的数据长度
|
|
||||||
* @retval int: 0表示成功,-1表示超时
|
|
||||||
*/
|
|
||||||
int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length)
|
|
||||||
{
|
|
||||||
uint32_t timeout = 0;
|
|
||||||
|
|
||||||
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE);
|
|
||||||
|
|
||||||
// 判断总线是否忙
|
|
||||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_start_on_bus(LDC_I2C); //发送开始信号
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 等待起始条件发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER); //设置为发送模式
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 等待地址发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 开始接收数据
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_data_transmit(LDC_I2C, reg_address); //发送寄存器地址
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 生成重复起始条件
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_start_on_bus(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 等待重复起始条件发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_master_addressing(LDC_I2C, device_address, I2C_RECEIVER);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 等待地址发送完成
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// 读取数据
|
|
||||||
for (uint16_t i = 0; i < length; i++) {
|
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT))
|
|
||||||
timeout++;
|
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
|
||||||
return -1; // 超时返回错误
|
|
||||||
}
|
|
||||||
if (i == length - 1) {
|
|
||||||
i2c_ack_config(LDC_I2C, I2C_ACK_DISABLE); // 禁用ACK
|
|
||||||
i2c_stop_on_bus(LDC_I2C); // 生成停止条件
|
|
||||||
}
|
|
||||||
data[i] = i2c_data_receive(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
}
|
|
||||||
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); // 重新启用ACK
|
|
||||||
|
|
||||||
// 生成停止条件
|
|
||||||
i2c_stop_on_bus(LDC_I2C);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void I2C_Scan(void) {
|
|
||||||
uint32_t timeout;
|
uint32_t timeout;
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
int found_devices = 0;
|
int found_devices = 0;
|
||||||
@@ -405,37 +55,38 @@ void I2C_Scan(void) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
// 生成起始条件
|
// 生成起始条件
|
||||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT))
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
continue; // 超时,跳过该地址
|
continue; // 超时,跳过该地址
|
||||||
}
|
}
|
||||||
i2c_start_on_bus(LDC_I2C);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
// 等待起始条件发送完成
|
// 等待起始条件发送完成
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT))
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout >= I2C_TIME_OUT) {
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
continue; // 超时,跳过该地址
|
continue; // 超时,跳过该地址
|
||||||
}
|
}
|
||||||
i2c_master_addressing(LDC_I2C, address << 1, I2C_TRANSMITTER);
|
i2c_master_addressing(I2C0, (address << 1), I2C_TRANSMITTER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
// 等待地址发送完成
|
// 等待地址发送完成
|
||||||
while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
printf("Found device at 0x%02X\r\n", address);
|
printf("Found device at 0x%02X\r\n", address);
|
||||||
found_devices++;
|
found_devices++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成停止条件
|
// 生成停止条件
|
||||||
i2c_stop_on_bus(LDC_I2C);
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
|
|
||||||
while (i2c_flag_get(LDC_I2C, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT))
|
while (i2c_flag_get(I2C0, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT))
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,80 +97,9 @@ void I2C_Scan(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LDC1612_IIC_read_16bits(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};
|
uint8_t data[2] = {0};
|
||||||
|
uint16_t raw_temp = 0;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
@@ -531,16 +111,16 @@ int LDC_getID(void) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
printf("err\r\n");
|
printf("err\r\n");
|
||||||
return -4101; // 超时返回错误
|
return -1; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_master_addressing(I2C0, (0x2B << 1), I2C_TRANSMITTER);
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4102; // 超时返回错误
|
return -2; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||||
@@ -549,17 +129,17 @@ int LDC_getID(void) {
|
|||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4103; // 超时返回错误
|
return -3; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址
|
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址
|
||||||
timeout++;
|
timeout++;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_data_transmit(I2C0, 0x7f);
|
i2c_data_transmit(I2C0, READ_DEVICE_ID);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
// i2c_start_on_bus(IR_I2C);
|
// i2c_start_on_bus(I2C0);
|
||||||
} else {
|
} else {
|
||||||
return -4104; // 超时返回错误
|
return -4; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
|
while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
|
||||||
@@ -568,17 +148,17 @@ int LDC_getID(void) {
|
|||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4105; // 超时返回错误
|
return -5; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_master_addressing(I2C0, (0x2B << 1), I2C_RECEIVER);
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4106; // 超时返回错误
|
return -6; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||||
@@ -587,7 +167,7 @@ int LDC_getID(void) {
|
|||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4107; // 超时返回错误
|
return -7; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取第一个字节的数据
|
// 读取第一个字节的数据
|
||||||
@@ -598,9 +178,10 @@ int LDC_getID(void) {
|
|||||||
data[0] = i2c_data_receive(I2C0);
|
data[0] = i2c_data_receive(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4108; // 超时返回错误
|
return -8; // 超时返回错误
|
||||||
}
|
}
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
||||||
|
// i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK
|
||||||
|
|
||||||
// 读取第二个字节的数据
|
// 读取第二个字节的数据
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
||||||
@@ -610,12 +191,11 @@ int LDC_getID(void) {
|
|||||||
data[1] = i2c_data_receive(I2C0);
|
data[1] = i2c_data_receive(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
} else {
|
||||||
return -4109; // 超时返回错误
|
return -9; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
printf("device id:%x %x\r\n",data[0],data[1]);
|
printf("device id = %x\r\n", (data[0] <<8 | data[1]));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@@ -99,26 +99,6 @@ void SysTick_Handler(void)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void TIMER13_IRQHandler(void)
|
|
||||||
{
|
|
||||||
if (timer_interrupt_flag_get(TIMER13, TIMER_INT_FLAG_UP) == SET)
|
|
||||||
{
|
|
||||||
timer_interrupt_flag_clear(TIMER13, TIMER_INT_FLAG_UP);
|
|
||||||
static uint8_t led_status = 0;
|
|
||||||
if (led_status)
|
|
||||||
{
|
|
||||||
//! turn on led & reconfig timer13 period to 19000(1900ms)
|
|
||||||
gpio_bit_write(GPIOB, GPIO_PIN_1, RESET);
|
|
||||||
timer_autoreload_value_config(TIMER13, 19200);
|
|
||||||
} else {
|
|
||||||
//! turn off led & reconfig timer13 period to 1000(100ms)
|
|
||||||
gpio_bit_write(GPIOB, GPIO_PIN_1, SET);
|
|
||||||
timer_autoreload_value_config(TIMER13, 800);
|
|
||||||
}
|
|
||||||
led_status = !led_status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function handles TIMER5 interrupt request.
|
* @brief This function handles TIMER5 interrupt request.
|
||||||
* @param[in] none
|
* @param[in] none
|
||||||
|
99
src/main.c
99
src/main.c
@@ -40,6 +40,51 @@ void led_config(void)
|
|||||||
nvic_irq_enable(LED_IRQ, 2);
|
nvic_irq_enable(LED_IRQ, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void iicSendData(void)
|
||||||
|
{
|
||||||
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
printf("1\r\n");
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
|
||||||
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
||||||
|
printf("2\r\n");
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
printf("3\r\n");
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE));
|
||||||
|
i2c_data_transmit(I2C0, 0x7E);
|
||||||
|
printf("4\r\n");
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void iicReceiveData(void)
|
||||||
|
{
|
||||||
|
uint16_t data[3] = {0};
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
// i2c_stop_on_bus(I2C0);
|
||||||
|
printf("5\r\n");
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
|
||||||
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
||||||
|
printf("6\r\n");
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
printf("7\r\n");
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
|
||||||
|
data[1] = i2c_data_receive(I2C0);
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
|
||||||
|
data[2] = i2c_data_receive(I2C0);
|
||||||
|
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
printf("data[0] = %x\r\n", data[0]);
|
||||||
|
printf("data[1] = %x\r\n", data[1]);
|
||||||
|
printf("data[2] = %x\r\n", data[2]);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief main function
|
\brief main function
|
||||||
\param[in] none
|
\param[in] none
|
||||||
@@ -52,30 +97,60 @@ int main(void)
|
|||||||
systick_config();
|
systick_config();
|
||||||
RS485_config();
|
RS485_config();
|
||||||
led_config();
|
led_config();
|
||||||
|
I2C_config();
|
||||||
|
|
||||||
// delay_ms(5000);
|
// delay_ms(5000);
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
|
|
||||||
LDC1612_I2CConfig();
|
// uint16_t data[3] = {0};
|
||||||
// LDC1612_Init();
|
|
||||||
// I2C_Scan();
|
// I2C_scan();
|
||||||
LDC1612_read_sensor_infomation();
|
|
||||||
|
delay_ms(1000);
|
||||||
|
|
||||||
|
// while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||||
|
// i2c_start_on_bus(I2C0);
|
||||||
|
// printf("1\r\n");
|
||||||
|
// while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
|
||||||
|
// i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
||||||
|
// printf("2\r\n");
|
||||||
|
// while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
|
||||||
|
// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
// printf("3\r\n");
|
||||||
|
// while (!i2c_flag_get(I2C0, I2C_FLAG_TBE));
|
||||||
|
// i2c_data_transmit(I2C0, 0x7E);
|
||||||
|
// printf("4\r\n");
|
||||||
|
// i2c_start_on_bus(I2C0);
|
||||||
|
// // i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
|
||||||
|
// delay_ms(100);
|
||||||
|
// i2c_start_on_bus(I2C0);
|
||||||
|
// delay_ms(100);
|
||||||
|
// i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
|
||||||
|
// i2c_stop_on_bus(I2C0);
|
||||||
|
// i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
// iicSendData();
|
||||||
|
// iicReceiveData();
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
result = LDC1612_IIC_read_16bits();
|
||||||
|
printf("result = %d\r\n", result);
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
// LDC1612_read_reg(LDC1612_ADDR, 0x7f, data, 2);
|
|
||||||
// printf("msb:%x %x\r\n",data[0],data[1]);
|
|
||||||
// LDC1612_read_reg(LDC1612_ADDR, 0x01, data, 2);
|
delay_ms(1000);
|
||||||
// printf("msb:%x %x\r\n",data[0],data[1]);
|
printf("OK!!!\r\n");
|
||||||
// printf("hello world!\r\n");
|
// iicSendData();
|
||||||
// LDC1612_Init();
|
|
||||||
// LDC1612_read_reg(LDC1612_ADDR, 0x01, data, 2);
|
// i2c_flag_clear(I2C0, I2C_FLAG_I2CBSY);
|
||||||
delay_ms(1000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user