generated from hulk/gd32e23x_template
unfunction
This commit is contained in:
parent
f508a7cd2d
commit
38849856af
@ -57,8 +57,8 @@ typedef enum {
|
|||||||
|
|
||||||
void I2C_config(void);
|
void I2C_config(void);
|
||||||
void I2C_scan(void);
|
void I2C_scan(void);
|
||||||
uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value);
|
|
||||||
void LDC1612_read_sensor_infomation(void);
|
int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
174
src/LDC1612.c
174
src/LDC1612.c
@ -26,18 +26,24 @@ void I2C_config(void) {
|
|||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LDC1612_Init(void) {
|
// void LDC1612_Init(void) {
|
||||||
uint8_t RCOUNT0_ALL[3]={SET_CONVERSION_TIME_REG_START,0x05,0x36};//csdn
|
// 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 SETTLECOUNT0_ALL[3]={SET_LC_STABILIZE_REG_START,0x00,0x0a};
|
||||||
uint8_t CLOCK_DIVIDERS0_ALL[3]={SET_FREQ_REG_START,0x10,0x02};
|
// 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 ERROR_CONFIG_ALL[3]={ERROR_CONFIG_REG,0x00,0x00};
|
||||||
uint8_t MUX_CONFIG_ALL[3]={MUL_CONFIG_REG,0x82,0x0c};
|
// 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 DRIVE_CURRENT0_ALL[3]={SET_DRIVER_CURRENT_REG,0x90,0x00};
|
||||||
uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn
|
// uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 扫描I2C总线,查找连接的设备
|
||||||
|
*
|
||||||
|
* 该函数会扫描I2C总线上的所有地址(1到126),并尝试与每个地址进行通信。
|
||||||
|
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||||
|
* 最后会打印出找到的设备总数。
|
||||||
|
*/
|
||||||
void I2C_scan(void) {
|
void I2C_scan(void) {
|
||||||
uint32_t timeout;
|
uint32_t timeout;
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
@ -91,99 +97,101 @@ void I2C_scan(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data) {
|
||||||
/**
|
|
||||||
* @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;
|
uint32_t timeout = 0;
|
||||||
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
// 发送启动信号
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) //判断IIC总线是否忙,发送起始信号
|
||||||
|
timeout++;
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
} else {
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
printf("err\r\n");
|
||||||
|
return -1; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送从设备地址和写信号
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写
|
||||||
|
timeout++;
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
} else {
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 2; // 超时
|
return -1; // 超时返回错误
|
||||||
}
|
}
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
|
|
||||||
// 发送寄存器地址
|
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 -1; // 超时返回错误
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址
|
||||||
|
timeout++;
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_data_transmit(I2C0, reg);
|
i2c_data_transmit(I2C0, reg);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE)) {
|
// i2c_start_on_bus(I2C0);
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 3; // 超时
|
} else {
|
||||||
|
return -1; // 超时返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重新启动信号
|
while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
|
||||||
|
timeout++;
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
} else {
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 4; // 超时
|
return -1; // 超时返回错误
|
||||||
}
|
}
|
||||||
printf("test1\n");
|
|
||||||
// 发送从设备地址和读信号
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 5; // 超时
|
|
||||||
}
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
|
|
||||||
// 读取数据
|
|
||||||
timeout = 0;
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
|
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 6; // 超时
|
|
||||||
}
|
|
||||||
*value = i2c_data_receive(I2C0) << 8; // 读取高8位
|
|
||||||
|
|
||||||
// i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK
|
|
||||||
|
|
||||||
timeout = 0;
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
|
|
||||||
if ((timeout++) > I2C_TIME_OUT) return 7; // 超时
|
|
||||||
}
|
|
||||||
*value |= i2c_data_receive(I2C0); // 读取低8位
|
|
||||||
|
|
||||||
// 发送停止信号
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
return 0; // 成功
|
|
||||||
}
|
|
||||||
|
|
||||||
void LDC1612_read_sensor_infomation(void)
|
|
||||||
{
|
|
||||||
uint16_t device_id;
|
|
||||||
uint16_t manufacturer_id;
|
|
||||||
uint8_t result;
|
|
||||||
|
|
||||||
// 读取设备ID
|
|
||||||
result = IIC_read_16bit(READ_DEVICE_ID, &device_id);
|
|
||||||
|
|
||||||
if (result == 0) {
|
|
||||||
printf("Device ID: 0x%04X\n", device_id);
|
|
||||||
} else {
|
} else {
|
||||||
printf("Failed to read Device ID\n");
|
return -1; // 超时返回错误
|
||||||
printf("result: %d\n", result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// result = IIC_read_16bit(READ_MANUFACTURER_ID, &manufacturer_id);
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||||
//
|
timeout++;
|
||||||
// if (result == 0) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
// printf("manufacturer ID: 0x%04X\n", manufacturer_id);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
// } else {
|
timeout = 0;
|
||||||
// printf("Failed to read manufacturer ID\n");
|
} else {
|
||||||
// printf("result: %d\n", result);
|
return -1; // 超时返回错误
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// 读取第一个字节的数据
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
} else {
|
||||||
|
return -1; // 超时返回错误
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK
|
||||||
|
|
||||||
|
// 读取第二个字节的数据
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout < I2C_TIME_OUT) {
|
||||||
|
data[1] = i2c_data_receive(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
} else {
|
||||||
|
return -1; // 超时返回错误
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
10
src/main.c
10
src/main.c
@ -63,18 +63,20 @@ int main(void)
|
|||||||
LDC1612_read_sensor_infomation();
|
LDC1612_read_sensor_infomation();
|
||||||
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
while(1){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* retarget the C library printf function to the USART */
|
/* retarget the C library printf function to the USART */
|
||||||
int _write(int fd, char* pBuffer, int size)
|
int _write (int fd, char *pBuffer, int size)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < size; i++)
|
for (int i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
usart_data_transmit(USART0, (uint8_t)pBuffer[i]);
|
usart_data_transmit(USART0, (uint8_t)pBuffer[i]);
|
||||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user