7 Commits

Author SHA1 Message Date
d6f3ea33b1 recevice function 2024-12-13 16:31:51 +08:00
de94a72f72 recevice function 2024-12-12 15:48:48 +08:00
44cf0426e5 recevice function 2024-12-11 15:42:11 +08:00
c90ecf05f9 recevice function 2024-12-11 14:30:26 +08:00
38849856af unfunction 2024-12-10 18:04:12 +08:00
f508a7cd2d format 2024-12-09 08:08:04 +08:00
e0b9aab103 fix IIC speed 2024-12-07 16:30:43 +08:00
8 changed files with 132 additions and 86 deletions

View File

@@ -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

View File

@@ -14,7 +14,6 @@
#include <stdlib.h> #include <stdlib.h>
#define LDC1612_ADDR (0x2B << 1) #define LDC1612_ADDR (0x2B << 1)
#define TMP112A_ADDR (0x49 << 1)
/*Register Rddr*/ /*Register Rddr*/
/***************************************************************************/ /***************************************************************************/
@@ -45,7 +44,7 @@ typedef enum {
I2C_STOP I2C_STOP
} i2c_process_enum; } i2c_process_enum;
#define I2C_SPEED 400000 #define I2C_SPEED 100000
#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
@@ -58,9 +57,9 @@ 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);
uint32_t TMP112A_ReadTemperature(void); int LDC1612_IIC_read_16bits(void);
#endif //LDC1612_H #endif //LDC1612_H

View File

@@ -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 */

View File

@@ -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

View File

@@ -21,7 +21,7 @@ void I2C_config(void) {
i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2); i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2);
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x72); // i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR);
i2c_enable(I2C0); i2c_enable(I2C0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
} }
@@ -37,7 +37,13 @@ void I2C_config(void) {
// //
// } // }
/**
* @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,11 +97,10 @@ void I2C_scan(void) {
} }
} }
uint32_t TMP112A_ReadTemperature(void) { int LDC1612_IIC_read_16bits(void) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
uint16_t raw_temp = 0; uint16_t raw_temp = 0;
uint16_t timeout = 0; uint16_t timeout = 0;
uint32_t temperature = 0;
i2c_ack_config(I2C0, I2C_ACK_ENABLE); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
@@ -112,10 +117,10 @@ uint32_t TMP112A_ReadTemperature(void) {
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, TMP112A_ADDR, I2C_TRANSMITTER); i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 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))
@@ -124,17 +129,17 @@ uint32_t TMP112A_ReadTemperature(void) {
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 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, 0x00); i2c_data_transmit(I2C0, READ_DEVICE_ID);
timeout = 0; timeout = 0;
// i2c_start_on_bus(I2C0); // i2c_start_on_bus(I2C0);
} else { } else {
return -1; // 超时返回错误 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)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
@@ -143,17 +148,17 @@ uint32_t TMP112A_ReadTemperature(void) {
i2c_start_on_bus(I2C0); i2c_start_on_bus(I2C0);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 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, TMP112A_ADDR, I2C_RECEIVER); i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 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))
@@ -162,7 +167,7 @@ uint32_t TMP112A_ReadTemperature(void) {
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 return -7; // 超时返回错误
} }
// 读取第一个字节的数据 // 读取第一个字节的数据
@@ -173,10 +178,10 @@ uint32_t TMP112A_ReadTemperature(void) {
data[0] = i2c_data_receive(I2C0); data[0] = i2c_data_receive(I2C0);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 return -8; // 超时返回错误
} }
i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK它会在下一个字节完成后发送NAK // 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)) {
@@ -186,17 +191,11 @@ uint32_t TMP112A_ReadTemperature(void) {
data[1] = i2c_data_receive(I2C0); data[1] = i2c_data_receive(I2C0);
timeout = 0; timeout = 0;
} else { } else {
return -1; // 超时返回错误 return -9; // 超时返回错误
} }
i2c_stop_on_bus(I2C0); i2c_stop_on_bus(I2C0);
// printf("data[0]: %x, data[1]: %x\r\n", data[0], data[1]); printf("device id = %x\r\n", (data[0] <<8 | data[1]));
raw_temp = ((uint16_t) (data[0] << 4) | (data[1]>>4)); return 0;
// printf("raw_temp: %x\r\n", raw_temp);
// printf("raw_temp(dec): %d\r\n", raw_temp);
temperature = raw_temp * 625;
// printf("temperature: %d\r\n", temperature);
return temperature;
} }

View File

@@ -4,8 +4,6 @@
#include "RS485.h" #include "RS485.h"
extern uint32_t g_temperature_uint32;
void RS485_config(void) { void RS485_config(void) {
rcu_periph_clock_enable(RS485_GPIO_RCU); rcu_periph_clock_enable(RS485_GPIO_RCU);
rcu_periph_clock_enable(RS485_RCU); rcu_periph_clock_enable(RS485_RCU);
@@ -37,8 +35,7 @@ void process_command(char *cmd) {
if (strncmp(cmd, "M1", 2) == 0) { if (strncmp(cmd, "M1", 2) == 0) {
printf("M1 -=-=- OK!\r\n"); printf("M1 -=-=- OK!\r\n");
} else if (strncmp(cmd, "M2", 2) == 0) { } else if (strncmp(cmd, "M2", 2) == 0) {
// printf("M2 -=-=- OK!\r\n"); printf("M2 -=-=- OK!\r\n");
printf("Temperature: %lu\r\n", g_temperature_uint32);
// } else if (strncmp(cmd, "M3", 2) == 0) { // } else if (strncmp(cmd, "M3", 2) == 0) {
// char *param_str = cmd + 2; // Skip "M3" // char *param_str = cmd + 2; // Skip "M3"
// int param = atoi(param_str + 1); // Skip "S" and convert to integer // int param = atoi(param_str + 1); // Skip "S" and convert to integer

View File

@@ -36,10 +36,6 @@ OF SUCH DAMAGE.
#include "main.h" #include "main.h"
#include "systick.h" #include "systick.h"
#include "LDC1612.h" #include "LDC1612.h"
#include "RS485.h"
char rx_buffer[RX_BUFFER_SIZE];
uint8_t rx_index = 0;
/*! /*!
\brief this function handles NMI exception \brief this function handles NMI exception
@@ -103,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
@@ -145,23 +121,3 @@ void TIMER16_IRQHandler(void) {
led_status = !led_status; led_status = !led_status;
} }
} }
void USART0_IRQHandler(void) {
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
uint8_t received_data = (uint8_t)usart_data_receive(USART0);
// 将接收到的数据存储到缓冲区
if(rx_index < RX_BUFFER_SIZE - 1) {
rx_buffer[rx_index++] = received_data;
}
// 检查是否接收到换行符,表示指令结束
if(received_data == '\n') {
rx_buffer[rx_index] = '\0'; // 添加字符串结束符
process_command(rx_buffer); // 处理指令
rx_index = 0; // 重置缓冲区索引
}
}
}

View File

@@ -12,8 +12,6 @@
#include "RS485.h" #include "RS485.h"
#include "LDC1612.h" #include "LDC1612.h"
uint32_t g_temperature_uint32;
void led_config(void) void led_config(void)
{ {
rcu_periph_clock_enable(LED_RCU); rcu_periph_clock_enable(LED_RCU);
@@ -42,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
@@ -61,11 +104,53 @@ int main(void)
printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
printf("\r\n"); printf("\r\n");
// uint16_t data[3] = {0};
// I2C_scan(); // I2C_scan();
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){
g_temperature_uint32 = TMP112A_ReadTemperature();
delay_ms(500);
delay_ms(1000);
printf("OK!!!\r\n");
// iicSendData();
// i2c_flag_clear(I2C0, I2C_FLAG_I2CBSY);
} }
} }