11 Commits

Author SHA1 Message Date
73caf651ef 更新版本号为V0.2.0,同时更新readme 2025-06-03 23:55:49 +08:00
da00f1d758 更改为非自动换向SP3485接口芯片,更改代码预期适配 2025-06-03 14:00:24 +08:00
1f64a500f9 delete IR Temperature sensor 2025-04-01 17:27:42 +08:00
385b6fda17 delete IR Temperature sensor 2025-04-01 15:51:35 +08:00
5714a10061 Revert "test"
This reverts commit f8a41af344.
2025-01-20 15:29:12 +08:00
af629a6bc4 Revert "update"
This reverts commit cc4d898114.
2025-01-20 15:29:12 +08:00
b0b2966efc Revert "更改协议处理部分,但是串口没反应?"
This reverts commit 705cb68764.
2025-01-20 15:29:12 +08:00
6e0ec2b64e 更改协议处理部分,但是串口没反应? 2025-01-20 15:29:12 +08:00
b5b9d538a1 update 2025-01-20 15:29:12 +08:00
81182cbafa test 2025-01-20 15:29:12 +08:00
7e9aefdd5f 发布0.1.0版本 2025-01-20 15:22:32 +08:00
12 changed files with 138 additions and 131 deletions

View File

@@ -12,7 +12,7 @@ set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d") string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
# Options 1 # Options 1
set(OPT1 "_[12V]") set(OPT1 "_[24V]")
#set(OPT1 "_[SW_IIC]") #set(OPT1 "_[SW_IIC]")
# Options 2 # Options 2

61
CommunicationProtocol.md Normal file
View File

@@ -0,0 +1,61 @@
# US&IR传感器模块通信协议
## US&IR传感器模块通信协议
| **序号** | **修改内容** | **版本** | **日期** | **修改人** |
|:------:|:--------:|:------:|:----------:|:-------:|
| 1 | 初版 | V0.1 | 2025-01-20 | Hulk |
| | | | | |
| | | | | |
| | | | | |
### 发包格式
| **包头** | **类型** | **数据长度** | **数据** | **校验** |
|:------:|:------:|:-----------:|:------:|:------:|
| D5 | 0x04 | Data Length | Data | CRC |
- 数据长度只包含数据部分,不包含包头、类型、数据长度、校验
- CRC求和校验包含类型、数据长度、数据
- 数据部分为ascii码
### 回包格式
| **包头** | **状态码** | **数据长度** | **数据** | **校验** |
|:------:|:----------:|:-----------:|:------:|:------:|
| B5 | 0xF0 正常包 | Data Length | Data | CRC |
| B5 | 0xF1 CRC错误 | Data Length | Data | CRC |
| B5 | 0xF2 包头错误 | Data Length | Data | CRC |
| B5 | 0xF3 类型错误 | Data Length | Data | CRC |
| B5 | 0xF4 包长度错误 | Data Length | Data | CRC |
- 数据长度只包含数据部分,不包含包头、类型、数据长度、校验
- CRC求和校验包含状态码、数据长度、数据
- 有效数据部分为uint32_t高字节在前
- 包错误和指令错误时数据部分为ascii码 `err`
-------------------
## US&IR传感器模块功能
### 1. 读取液面的超声距离
- 发送M1指令读取距离液面的超声距离。
- `D5 04 02 4D 31 84`
- 电涡流传感器模块涡流回复数据
- `B5 F0 02 1D C0 CF`, 有效数据为 `0x1DC0`,转换为`7616`,距离为76.16mm
- `B5 F0 02 16 A5 AD`, 有效数据为 `0x16A5`,转换为`5797`,距离为57.97mm
- 错误命令M3指令回包
- `B5 F0 03 65 72 72 3C`,有效数据为 `err`
- CRC错误回包
- `B5 F1 03 65 72 72 3D`, 有效数据为 `err`
- 包头错误回包
- `B5 F2 03 65 72 72 3E`, 有效数据为 `err`
- 类型错误回包
- `B5 F3 03 65 72 72 3F`, 有效数据为 `err`
- 数据长度错误回包
- `B5 F4 03 65 72 72 40`, 有效数据为 `err`
### 2. 读取数据时间间隔
- 推荐数据时间间隔至少为50ms

View File

@@ -1,80 +1,31 @@
# gd32e23x_template # XLSW-3DP-Sensor-UltraSonic&IR
本项目为`GD32E230Fx`系列的基于Clion的CMake开发的工程模板。本人暂未入门强行上强度放弃keil拥抱开源。遂尝试使用arm-none-eabi-gcc进行开发。
有幸寻得[@mo10 ](https://github.com/mo10)大佬的帮助本项目的基础目录架构与CMakeLists.txt与toolchain.cmake均为大佬提供。
## 关于本项目 | **版本号** | **修改内容** | **日期** | **修改人** |
本项目默认的芯片型号为`GD32E230F4`,但是可以根据需要修改为其他型号,具体修改方法请参考下方`关于链接脚本`的说明。 |:-------:|:--------:|:----------:|:-------:|
### 版本号 | v0.0.1 | 非稳定版本 | 2025-01-20 | Hulk |
默认版本号为`0.0.1`,在`CMakeLists.txt`中修改`PROJECT_VERSION`即可。 | v0.2.0 | 硬件修改RS485接口芯片为SP3485非自动换向 | 2025-06-03 | Hulk |
### 项目名称
默认项目名称为`gd32e23x_template`,在`CMakeLists.txt`中修改`PROJECT_NAME`即可。请先修改项目名称再配置编译环境。
### 软件IIC与硬件IIC
本项目中提供了软件IIC与硬件IIC的驱动但是默认使用硬件IIC如果需要使用软件IIC请在`board_config.h`中取消注释`// #define SOFTWARE_IIC`(line 8)。
### 编译选项
本项目预留了两个编译选项,`OPT1``OPT2`,默认均为空,请根据需要自行修改,例如`OPT1``_[HW_IIC]``OPT2``_[NO_LED]`
`OPT1``OPT2`均在`CMakeLists.txt`中可修改。
### 关于编译日期
本项目在`CMakeLists.txt`中添加了编译日期。
### 关于led ## US&IR传感器模块通信协议
本项目默认开启了LED闪烁并使用TIMER16进行定时。
通信协议:[US&IR通信协议](CommunicationProtocol.md)
## 关于C标准库的printf的重写 ---
在Keil开发中ARMClang有自己的microLIB所以直接调用然后重写fputc函数即可但在gcc中需要重写`_write`函数,本项目模板中已经在`main.c`中完成重写。
同时需要添加`--spaces=nano.spaces`编译参数。
但是printf本身占用flash比较大建议谨慎使用尤其是本项目搭建时候采用的型号为`GD32E230F4V6`内存非常有限重写后加上spaces设置目前能用。
## 添加源文件与头文件 ## 300K超声换能头工作流程
`ProjectDir/CMakeLists.txt`中21行左右添加对应源文件即可。 ### **1. 超声波发送**
通过TIMER13定时器配置为输出300KHz的PWM信号驱动超声换能头发射超声波。TIMER13的输出模式为PWM1模式同时用TIMER13的更新中断触发次数进行计数。
最终使驱动引脚发送300KHz 50%占空比 5个周期的PWM信号。同时开启计时。
```cmake ### **2. 超声波计时**
set(TARGET_C_SRC 通过TIMER15定时器配置为us计时器并传入预先测得的超声消隐延时值例如230us。该定时器用于在发送超声波后延时一段时间确保接收通道不会受到发射信号的干扰。但是暂不开启该定时器。
${CMAKE_SOURCE_DIR}/src/main.c 同时在TIMER15的更新中断中断服务函数中开启外部输入中断用以检测超声回波并开启TIMER16定时器。
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c TIMER16定时器输入捕获用于计算超声回波的时间。
${CMAKE_SOURCE_DIR}/src/systick.c 通过前面的消隐时间与回波时间的和值,计算出实际的距离值。
)
```
## 关于链接脚本
注意芯片选型, 不同型号的芯片 FLASH 和 RAM 大小不同。需要修改链接脚本`ld/gd32e23x_gcc.ld`
| 芯片型号 | FLASH | RAM | ### **3. 距离值输出**
|------------|-------|-----|
| GD32E230F4 | 16K | 4K |
| GD32E230F8 | 64K | 8K |
```linkerscript \[
/* memory map */ \frac{\frac{(\verb|TIME_CORRECTION_US| + \verb|us_value|)\times 340\,\text{m/s}}{10^{3} \ast 10^{3}} }{2}
MEMORY \]
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
}
```
## 关于Startup文件 其中,`TIME_CORRECTION_US`为超声消隐延时定时器的初始值,`us_value`为外部输入中断计数值。
[@mo10 ](https://github.com/mo10)大佬提供的一些想法,具体如下。但是我目前还没测试到实际的影响和作用范围,所以暂未同步进来,仅作为备忘内容
```asm
Reset_Handler:
ldr r0, =_sp
mov sp, r0
ldr r0, =_end
msr msplim, r0
/* copy the data segment into ram */
movs r1, #0
b LoopCopyDataInit
```
## Ref
1. 参考LD/Startup
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld)
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s)
2. 官方LD/Startup
使用Embedded Builder工具生成的C标准库生成的模板

View File

@@ -9,8 +9,10 @@
// #define DEBUG_VERBOES // #define DEBUG_VERBOES
#define POWER_SUPPLY_12V // #define POWER_SUPPLY_12V
// #define POWER_SUPPLY_24V #define POWER_SUPPLY_24V
// #define DEBUG_VOFA_TOOL
/******************************************************************************/ /******************************************************************************/
@@ -49,7 +51,7 @@
#define USART_PHY USART0 #define USART_PHY USART0
#define USART_PHY_BAUDRATE 115200U #define USART_PHY_BAUDRATE 115200U
#define RS485_EN_PORT GPIOA #define RS485_EN_PORT GPIOA
#define RS485_EN_PIN GPIO_PIN_4 #define RS485_EN_PIN GPIO_PIN_1
/******************************************************************************/ /******************************************************************************/

View File

@@ -44,7 +44,6 @@ OF SUCH DAMAGE.
#include "fwdgt.h" #include "fwdgt.h"
#include "board_config.h" #include "board_config.h"
#include "gd60914.h" #include "gd60914.h"
#include "ultrasonic_analog.h"
#ifdef SOFTWARE_IIC #ifdef SOFTWARE_IIC
#include "soft_i2c.h" #include "soft_i2c.h"

View File

@@ -8,8 +8,6 @@
#include "gd32e23x.h" #include "gd32e23x.h"
#include "board_config.h" #include "board_config.h"
void usart_config(void);
void rs485_config(void); void rs485_config(void);
#endif //USART_H #endif //USART_H

View File

@@ -15,7 +15,7 @@ void watchdog_init(void) {
rcu_osci_stab_wait(RCU_IRC40K); rcu_osci_stab_wait(RCU_IRC40K);
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
fwdgt_config(625, FWDGT_PSC_DIV256); // Set timeout to 1 seconds (625 / 0.625 KHz) fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
/* Enable FWDGT */ /* Enable FWDGT */
fwdgt_enable(); fwdgt_enable();

View File

@@ -35,5 +35,5 @@ void led_blink_config(void)
timer_enable(LED_BLINK_TIMER); timer_enable(LED_BLINK_TIMER);
nvic_irq_enable(LED_BLINK_IRQ, 2); nvic_irq_enable(LED_BLINK_IRQ, 3U);
} }

View File

@@ -25,7 +25,7 @@ int main(void)
/* configure LED */ /* configure LED */
led_blink_config(); led_blink_config();
/* configure FWDGT */ /* configure FWDGT */
watchdog_init(); // watchdog_init();
// #ifdef SOFTWARE_IIC // #ifdef SOFTWARE_IIC
// soft_i2c_config(); // soft_i2c_config();
@@ -40,17 +40,10 @@ int main(void)
while(1){ while(1){
// gd60914_get_object_tempture(); // gd60914_get_object_tempture();
delay_ms(500); delay_ms(50);
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES); ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
delay_ms(10); // watchdog_reload();
uint16_t distance = ultrasonic_calc_distance();
printf("%u\r\n", distance);
watchdog_reload();
} }
} }

View File

@@ -21,12 +21,12 @@ void process_command(uint8_t *cmd, size_t length) {
sprintf(combined_str, "%c%c", cmd[3], cmd[4]); sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) { if (strcmp(combined_str, "M1") == 0) {
ultrasonic_distance_report(); ultrasonic_distance_report();
} else if (strcmp(combined_str, "M2") == 0) { // } else if (strcmp(combined_str, "M2") == 0) {
gd60914_tempture_report(); // gd60914_tempture_report();
} else if (strcmp(combined_str, "M3") == 0) // } else if (strcmp(combined_str, "M3") == 0)
{ // {
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC); // printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
fwdgt_reset_mcu(); // fwdgt_reset_mcu();
} else { } else {
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
return; return;
@@ -110,6 +110,9 @@ void ultrasonic_distance_report(void) {
distance_uint16 = ultrasonic_calc_distance(); distance_uint16 = ultrasonic_calc_distance();
#ifdef DEBUG_VOFA_TOOL
printf("%d\n", distance_uint16);
#else
package_data[0] = (distance_uint16 >> 8) & 0xFF; package_data[0] = (distance_uint16 >> 8) & 0xFF;
package_data[1] = distance_uint16 & 0xFF; package_data[1] = distance_uint16 & 0xFF;
@@ -120,4 +123,5 @@ void ultrasonic_distance_report(void) {
printf("%c%c%c", package_header[0], package_header[1], package_header[2]); printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
printf("%c%c", package_data[0], package_data[1]); printf("%c%c", package_data[0], package_data[1]);
printf("%c", calculate_crc(combined_data, 6)); printf("%c", calculate_crc(combined_data, 6));
#endif
} }

View File

@@ -140,7 +140,7 @@ void ultrasonic_config(void) {
} }
uint16_t ultrasonic_calc_distance(void) { uint16_t ultrasonic_calc_distance(void) {
while (!ultrasonicMeasurementDone); // while (!ultrasonicMeasurementDone);
// uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH); // uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17; uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17;
/* /*

View File

@@ -4,36 +4,6 @@
#include "usart.h" #include "usart.h"
/**
* @brief configure the USART
* @param none
* @retval none
*/
void usart_config(void)
{
rcu_periph_clock_enable(USART_GPIO_RCU);
rcu_periph_clock_enable(USART_RCU);
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_RX_PIN);
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_TX_PIN);
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_RX_PIN);
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_RX_PIN);
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN);
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN);
usart_deinit(USART_PHY);
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
usart_enable(USART_PHY);
nvic_irq_enable(USART0_IRQn, 0);
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
}
/** /**
* @brief configure the RS485(MAX13487) driver * @brief configure the RS485(MAX13487) driver
* @param none * @param none
@@ -41,10 +11,39 @@ void usart_config(void)
*/ */
void rs485_config(void) void rs485_config(void)
{ {
usart_config(); rcu_periph_clock_enable(USART_GPIO_RCU);
rcu_periph_clock_enable(USART_RCU);
gpio_mode_set(RS485_EN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN); gpio_af_set(USART_GPIO_PORT, GPIO_AF_1, USART_TX_PIN | USART_RX_PIN | RS485_EN_PIN);
gpio_output_options_set(RS485_EN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control /* configure USART Tx&Rx as alternate function push-pull */
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN | USART_RX_PIN);
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN | USART_RX_PIN);
/* configure RS485 EN Pin */
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN);
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
// gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET);
/* USART configure */
usart_deinit(USART_PHY);
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
// usart_parity_config(RS485_PHY, USART_PM_NONE);
// usart_word_length_set(RS485_PHY, USART_WL_8BIT);
// usart_stop_bit_set(RS485_PHY, USART_STB_1BIT);
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
usart_driver_assertime_config(USART_PHY, 0x01);
usart_driver_deassertime_config(USART_PHY, 0x01);
// usart_depolarity_config(USART0, USART_DEP_HIGH);
// usart_depolarity_config(USART0, USART_DEP_LOW);
usart_rs485_driver_enable(USART_PHY);
usart_enable(USART_PHY);
nvic_irq_enable(USART0_IRQn, 0);
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
} }