generated from hulk/gd32e23x_template
Compare commits
24 Commits
main
...
feature-ti
Author | SHA1 | Date | |
---|---|---|---|
e5f3bf900a | |||
3f75e99553 | |||
f67baed8c5 | |||
ac74c30923 | |||
9f615ba3a3 | |||
705cb68764 | |||
cc4d898114 | |||
f8a41af344 | |||
7ffb8c0612 | |||
7075fd16ea | |||
f7ef4b5af7 | |||
e940c69d49 | |||
b4901f5f37 | |||
c9c1e00d6a | |||
6dd053e217 | |||
8a7baaf87a | |||
1af7a98a5f | |||
aee082068e | |||
a0ba0c6bd1 | |||
389d480da7 | |||
c12273fd46 | |||
e86f8b0dd3 | |||
997637efcf | |||
93b24cc454 |
@ -6,17 +6,17 @@ set(PROJECT_NAME "XLSW_3DP_US-IR")
|
|||||||
project(${PROJECT_NAME})
|
project(${PROJECT_NAME})
|
||||||
|
|
||||||
set(VERSION_MAJOR 0)
|
set(VERSION_MAJOR 0)
|
||||||
set(VERSION_MINOR 0)
|
set(VERSION_MINOR 1)
|
||||||
set(VERSION_PATCH 1)
|
set(VERSION_PATCH 0)
|
||||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
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 "")
|
set(OPT1 "_[24V]")
|
||||||
#set(OPT1 "_[SW_IIC]")
|
#set(OPT1 "_[SW_IIC]")
|
||||||
|
|
||||||
# Options 2
|
# Options 2
|
||||||
set(OPT2 "")
|
set(OPT2 "_[HW_IIC]")
|
||||||
#set(OPT2 "_[NO_LED]")
|
#set(OPT2 "_[NO_LED]")
|
||||||
|
|
||||||
enable_language(C)
|
enable_language(C)
|
||||||
@ -39,6 +39,9 @@ set(TARGET_C_SRC
|
|||||||
${CMAKE_SOURCE_DIR}/src/soft_i2c.c
|
${CMAKE_SOURCE_DIR}/src/soft_i2c.c
|
||||||
${CMAKE_SOURCE_DIR}/src/i2c.c
|
${CMAKE_SOURCE_DIR}/src/i2c.c
|
||||||
${CMAKE_SOURCE_DIR}/src/fwdgt.c
|
${CMAKE_SOURCE_DIR}/src/fwdgt.c
|
||||||
|
${CMAKE_SOURCE_DIR}/src/rs485_protocol.c
|
||||||
|
${CMAKE_SOURCE_DIR}/src/ultrasonic_analog.c
|
||||||
|
${CMAKE_SOURCE_DIR}/src/gd60914.c
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${TARGET_C_SRC})
|
add_executable(${PROJECT_NAME} ${TARGET_C_SRC})
|
||||||
|
79
CommunicationProtocol.md
Normal file
79
CommunicationProtocol.md
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
# 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. 读取液面红外温度传感器数据
|
||||||
|
|
||||||
|
- 发送M2指令,读取液面红外温度传感器数据。
|
||||||
|
- `D5 04 02 4D 32 85`
|
||||||
|
- 电涡流传感器模块温度补偿回复数据
|
||||||
|
- `B5 F0 02 00 BD AF`, 有效数据为 `0x00BD`,转换为`189`(单位为摄氏度*10)温度为18.9℃
|
||||||
|
- `B5 F0 02 01 0C FF`, 有效数据为 `0x010C`,转换为`268`(单位为摄氏度*10)温度为26.8℃
|
||||||
|
- 错误命令(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`
|
||||||
|
|
||||||
|
### 3. 读取数据时间间隔
|
||||||
|
|
||||||
|
- 推荐数据时间间隔至少为1s
|
83
README.md
83
README.md
@ -1,80 +1,9 @@
|
|||||||
# 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`即可。
|
|
||||||
### 项目名称
|
|
||||||
默认项目名称为`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设置,目前能用。
|
|
||||||
|
|
||||||
## 添加源文件与头文件
|
|
||||||
在`ProjectDir/CMakeLists.txt`中21行左右,添加对应源文件即可。
|
|
||||||
|
|
||||||
```cmake
|
|
||||||
set(TARGET_C_SRC
|
|
||||||
${CMAKE_SOURCE_DIR}/src/main.c
|
|
||||||
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
|
|
||||||
${CMAKE_SOURCE_DIR}/src/systick.c
|
|
||||||
)
|
|
||||||
```
|
|
||||||
## 关于链接脚本
|
|
||||||
注意芯片选型, 不同型号的芯片 FLASH 和 RAM 大小不同。需要修改链接脚本`ld/gd32e23x_gcc.ld`
|
|
||||||
|
|
||||||
| 芯片型号 | FLASH | RAM |
|
|
||||||
|------------|-------|-----|
|
|
||||||
| GD32E230F4 | 16K | 4K |
|
|
||||||
| GD32E230F8 | 64K | 8K |
|
|
||||||
|
|
||||||
```linkerscript
|
|
||||||
/* memory map */
|
|
||||||
MEMORY
|
|
||||||
{
|
|
||||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 关于Startup文件
|
|
||||||
|
|
||||||
[@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标准库生成的模板
|
|
@ -91,9 +91,15 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
|
|||||||
|
|
||||||
# Conditional flags
|
# Conditional flags
|
||||||
# DEBUG
|
# DEBUG
|
||||||
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 -O2 -g")
|
||||||
|
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O2 -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
|
||||||
|
@ -9,6 +9,38 @@
|
|||||||
|
|
||||||
// #define DEBUG_VERBOES
|
// #define DEBUG_VERBOES
|
||||||
|
|
||||||
|
// #define POWER_SUPPLY_12V
|
||||||
|
#define POWER_SUPPLY_24V
|
||||||
|
|
||||||
|
// #define GD32E230F4
|
||||||
|
// #define GD32E230F8
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#ifdef GD32E230F8
|
||||||
|
#define USART_RCU RCU_USART1
|
||||||
|
#define USART_PHY USART1
|
||||||
|
#define USART_PHY_IRQ USART1_IRQn
|
||||||
|
#else
|
||||||
|
#define USART_RCU RCU_USART0
|
||||||
|
#define USART_PHY USART0
|
||||||
|
#define USART_PHY_IRQ USART0_IRQn
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#ifdef POWER_SUPPLY_12V
|
||||||
|
#define POWER_VOLTAGE "12V"
|
||||||
|
#define TIME_CORRECTION_US 250
|
||||||
|
#define CAPTURE_VALUE_MAX 515
|
||||||
|
#elif defined(POWER_SUPPLY_24V)
|
||||||
|
#define POWER_VOLTAGE "24V"
|
||||||
|
#define TIME_CORRECTION_US 230
|
||||||
|
#define CAPTURE_VALUE_MAX 550
|
||||||
|
#else
|
||||||
|
#error "Please define either POWER_SUPPLY_12V or POWER_SUPPLY_24V"
|
||||||
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_GPIO_RCU RCU_GPIOF
|
#define I2C_GPIO_RCU RCU_GPIOF
|
||||||
@ -23,24 +55,57 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define USART_GPIO_RCU RCU_GPIOA
|
#define USART_GPIO_RCU RCU_GPIOA
|
||||||
#define USART_RCU RCU_USART0
|
// #define USART_RCU RCU_USART0
|
||||||
#define USART_GPIO_PORT GPIOA
|
#define USART_GPIO_PORT GPIOA
|
||||||
#define USART_GPIO_AF GPIO_AF_1
|
#define USART_GPIO_AF GPIO_AF_1
|
||||||
#define USART_TX_PIN GPIO_PIN_2
|
#define USART_TX_PIN GPIO_PIN_2
|
||||||
#define USART_RX_PIN GPIO_PIN_3
|
#define USART_RX_PIN GPIO_PIN_3
|
||||||
#define USART_PHY USART0
|
// #define USART_PHY USART0
|
||||||
#define USART_PHY_BAUDRATE 115200U
|
#define USART_PHY_BAUDRATE 115200U
|
||||||
|
// #define USART_PHY_IRQ USART0_IRQn
|
||||||
#define RS485_EN_PORT GPIOA
|
#define RS485_EN_PORT GPIOA
|
||||||
#define RS485_EN_PIN GPIO_PIN_1
|
#define RS485_EN_PIN GPIO_PIN_4
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define LED_PORT GPIOA
|
#define LED_PORT GPIOA
|
||||||
#define LED_PIN GPIO_PIN_9
|
#define LED_PIN GPIO_PIN_9
|
||||||
#define LED_RCU RCU_GPIOA
|
#define LED_RCU RCU_GPIOA
|
||||||
#define LED_BLINK_TIMER_RCU RCU_TIMER16
|
#define LED_BLINK_TIMER_RCU RCU_TIMER5
|
||||||
#define LED_BLINK_TIMER TIMER16
|
#define LED_BLINK_TIMER TIMER5
|
||||||
#define LED_BLINK_IRQ TIMER16_IRQn
|
#define LED_BLINK_IRQ TIMER5_IRQn
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#define US_TX_GPIO_RCU RCU_GPIOB
|
||||||
|
#define US_TX_GPIO_PORT GPIOB
|
||||||
|
#define US_TX_PIN GPIO_PIN_1
|
||||||
|
#define US_TX_GPIO_AF GPIO_AF_0
|
||||||
|
#define US_TX_RCU RCU_TIMER13
|
||||||
|
#define US_TX_TIMER TIMER13
|
||||||
|
#define US_TX_CH TIMER_CH_0
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define US_TX_DELAY_RCU RCU_TIMER15
|
||||||
|
#define US_TX_DELAY_TIMER TIMER15
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define US_RX_GPIO_RCU RCU_GPIOA
|
||||||
|
#define US_RX_EXTI_RCU RCU_CFGCMP
|
||||||
|
#define US_RX_GPIO_PORT GPIOA
|
||||||
|
#define US_RX_GPIO_PIN GPIO_PIN_0
|
||||||
|
#define US_RX_EXTI_IRQ EXTI0_1_IRQn
|
||||||
|
#define US_RX_GPIO_EXTI EXTI_0
|
||||||
|
#define US_RX_EXTI_LINE EXTI_SOURCE_PIN0
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define US_ECHO_RCU RCU_TIMER16
|
||||||
|
#define US_ECHO_TIMER TIMER16
|
||||||
|
#define US_ECHO_CH TIMER_CH_0
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
@ -39,6 +39,9 @@ OF SUCH DAMAGE.
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include "fwdgt.h"
|
||||||
|
#include "rs485_protocol.h"
|
||||||
|
|
||||||
/* function declarations */
|
/* function declarations */
|
||||||
/* this function handles NMI exception */
|
/* this function handles NMI exception */
|
||||||
@ -52,6 +55,6 @@ void PendSV_Handler(void);
|
|||||||
/* this function handles SysTick exception */
|
/* this function handles SysTick exception */
|
||||||
void SysTick_Handler(void);
|
void SysTick_Handler(void);
|
||||||
|
|
||||||
void TIMER16_IRQHandler(void);
|
void TIMER5_IRQHandler(void);
|
||||||
|
|
||||||
#endif /* GD32E23X_IT_H */
|
#endif /* GD32E23X_IT_H */
|
||||||
|
34
inc/gd60914.h
Normal file
34
inc/gd60914.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// Created by dell on 25-1-7.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GD60914_H
|
||||||
|
#define GD60914_H
|
||||||
|
|
||||||
|
#include "gd32e23x_it.h"
|
||||||
|
#include "gd32e23x.h"
|
||||||
|
|
||||||
|
#include "board_config.h"
|
||||||
|
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
#include "soft_i2c.h"
|
||||||
|
#else
|
||||||
|
#include "i2c.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define GD60914_ADDR (0x18 << 1)
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define GD60914_HUM_TEMP 0x1A
|
||||||
|
#define GD60914_OBJ_TEMP 0x1F
|
||||||
|
#define GD60914_AMB_TEMP 0x1E
|
||||||
|
#define GD60914_TEMP_REG 0x1C
|
||||||
|
|
||||||
|
void gd60914_get_object_tempture(void);
|
||||||
|
|
||||||
|
void gd60914_read_temp(void);
|
||||||
|
|
||||||
|
#endif //GD60914_H
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_SPEED 20000
|
#define I2C_SPEED 100000
|
||||||
|
|
||||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
#define I2C_TIME_OUT (uint16_t)(5000)
|
||||||
#define I2C_OK 1
|
#define I2C_OK 1
|
||||||
|
@ -43,6 +43,7 @@ OF SUCH DAMAGE.
|
|||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
#include "fwdgt.h"
|
#include "fwdgt.h"
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
|
#include "gd60914.h"
|
||||||
|
|
||||||
#ifdef SOFTWARE_IIC
|
#ifdef SOFTWARE_IIC
|
||||||
#include "soft_i2c.h"
|
#include "soft_i2c.h"
|
||||||
|
56
inc/rs485_protocol.h
Normal file
56
inc/rs485_protocol.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
//
|
||||||
|
// Created by yelv1 on 24-12-31.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RS485_PROTOCOL_H
|
||||||
|
#define RS485_PROTOCOL_H
|
||||||
|
|
||||||
|
#include "gd32e23x.h"
|
||||||
|
#include "systick.h"
|
||||||
|
#include "gd32e23x_libopt.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include "fwdgt.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "ultrasonic_analog.h"
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define RX_BUFFER_SIZE 32
|
||||||
|
|
||||||
|
#define PROTOCOL_PACKAGE_HEADER 0xD5
|
||||||
|
#define PROTOCOL_BOARD_TYPE 0x04
|
||||||
|
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
VALIDATION_SUCCESS = 0,
|
||||||
|
VALIDATION_CRC_ERROR = 1,
|
||||||
|
VALIDATION_HEADER_ERROR = 2,
|
||||||
|
VALIDATION_TYPE_ERROR = 4,
|
||||||
|
VALIDATION_LENGTH_ERROR = 8
|
||||||
|
} validation_result_t;
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void process_command(uint8_t* cmd, size_t length);
|
||||||
|
|
||||||
|
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
|
||||||
|
|
||||||
|
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
|
||||||
|
|
||||||
|
validation_result_t validate_package_header(uint8_t* data);
|
||||||
|
|
||||||
|
validation_result_t validate_package_type(uint8_t* data);
|
||||||
|
|
||||||
|
validation_result_t validate_data_length(uint8_t* data);
|
||||||
|
|
||||||
|
void gd60914_tempture_report(void);
|
||||||
|
|
||||||
|
void ultrasonic_distance_report(void);
|
||||||
|
|
||||||
|
#endif //RS485_PROTOCOL_H
|
43
inc/ultrasonic_analog.h
Normal file
43
inc/ultrasonic_analog.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
//
|
||||||
|
// Created by yelv1 on 24-12-31.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ULTRASONIC_ANALOG_H
|
||||||
|
#define ULTRASONIC_ANALOG_H
|
||||||
|
|
||||||
|
#include "gd32e23x.h"
|
||||||
|
#include "systick.h"
|
||||||
|
#include "gd32e23x_libopt.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include "fwdgt.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
extern volatile bool ultrasonicMeasurementDone;
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define ULTRASONIC_TX_CYCLES 0x05U
|
||||||
|
#define ULTRASONIC_TX_TIME 498 // (ms)
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void ultrasonic_gpio_config(void);
|
||||||
|
|
||||||
|
void ultrasonic_pwm_out_cycles(const uint8_t cycles);
|
||||||
|
|
||||||
|
void ultrasonic_transmit_delay(const uint16_t micro_second);
|
||||||
|
|
||||||
|
void ultrasonic_rece_exti_config(void);
|
||||||
|
|
||||||
|
void ultrasonic_echo_timer_config(void);
|
||||||
|
|
||||||
|
void ultrasonic_config(void);
|
||||||
|
|
||||||
|
uint16_t ultrasonic_calc_distance(void);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ULTRASONIC_ANALOG_H
|
@ -34,6 +34,8 @@ OF SUCH DAMAGE.
|
|||||||
|
|
||||||
#include "gd32e23x_it.h"
|
#include "gd32e23x_it.h"
|
||||||
|
|
||||||
|
extern uint16_t g_capture_value;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief this function handles NMI exception
|
\brief this function handles NMI exception
|
||||||
\param[in] none
|
\param[in] none
|
||||||
@ -102,8 +104,7 @@ void SysTick_Handler(void)
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void TIMER16_IRQHandler(void)
|
void TIMER5_IRQHandler(void) {
|
||||||
{
|
|
||||||
if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET)
|
if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET)
|
||||||
{
|
{
|
||||||
timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP);
|
timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP);
|
||||||
@ -121,3 +122,61 @@ void TIMER16_IRQHandler(void)
|
|||||||
led_status = !led_status;
|
led_status = !led_status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles TIMER15 interrupt request.
|
||||||
|
* @param[in] none
|
||||||
|
* @param[out] none
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
void TIMER15_IRQHandler(void) {
|
||||||
|
if (timer_interrupt_flag_get(US_TX_DELAY_TIMER, TIMER_INT_FLAG_UP) == SET)
|
||||||
|
{
|
||||||
|
timer_interrupt_flag_clear(US_TX_DELAY_TIMER, TIMER_INT_FLAG_UP);
|
||||||
|
exti_interrupt_enable(US_RX_GPIO_EXTI); // turn on hardware external input interrupt
|
||||||
|
timer_counter_value_config(US_ECHO_TIMER, 0);
|
||||||
|
timer_enable(US_ECHO_TIMER); // turn on timer to calculate the first ultrasonic echo time
|
||||||
|
timer_disable(US_TX_DELAY_TIMER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles external lines 0 to 1 interrupt request
|
||||||
|
* @param[in] none
|
||||||
|
* @param[out] none
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
void EXTI0_1_IRQHandler(void) {
|
||||||
|
if (exti_interrupt_flag_get(US_RX_GPIO_EXTI) == SET)
|
||||||
|
{
|
||||||
|
exti_interrupt_flag_clear(US_RX_GPIO_EXTI);
|
||||||
|
g_capture_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
|
||||||
|
ultrasonicMeasurementDone = true;
|
||||||
|
timer_disable(US_ECHO_TIMER);
|
||||||
|
exti_interrupt_disable(US_RX_GPIO_EXTI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void USART0_IRQHandler(void) {
|
||||||
|
static uint8_t rx_index = 0;
|
||||||
|
static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
||||||
|
|
||||||
|
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 (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
|
||||||
|
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
|
||||||
|
|
||||||
|
process_command(rx_buffer, rx_index); // 处理指令
|
||||||
|
|
||||||
|
rx_index = 0; // 重置缓冲区索引
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
51
src/gd60914.c
Normal file
51
src/gd60914.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Created by dell on 25-1-7.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "gd60914.h"
|
||||||
|
|
||||||
|
void gd60914_get_object_tempture(void) {
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
soft_i2c_config();
|
||||||
|
#else
|
||||||
|
i2c_config();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static uint8_t sensor_validation_data[2];
|
||||||
|
extern uint8_t g_temperature_uint8[2];
|
||||||
|
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data);
|
||||||
|
#else
|
||||||
|
i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, sensor_validation_data);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (sensor_validation_data[0] != 0xAA || sensor_validation_data[1] != 0x55) {
|
||||||
|
#ifdef DEBUG_VERBOES
|
||||||
|
printf("sensor error\r\n");
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay_ms(350);
|
||||||
|
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
|
||||||
|
#else
|
||||||
|
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// printf("%d\r\n", g_temperature_uint8[1] << 8 | g_temperature_uint8[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void gd60914_read_temp(void) {
|
||||||
|
|
||||||
|
uint8_t value[2] = {0};
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, value);
|
||||||
|
#else
|
||||||
|
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, value);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
printf("%x %x\r\n", value[1], value[0]);
|
||||||
|
}
|
19
src/main.c
19
src/main.c
@ -6,6 +6,9 @@
|
|||||||
*/
|
*/
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
volatile uint8_t g_temperature_uint8[2] = {0};
|
||||||
|
volatile uint16_t g_capture_value;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief main function
|
\brief main function
|
||||||
\param[in] none
|
\param[in] none
|
||||||
@ -14,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
setbuf(stdout, NULL);
|
||||||
/* configure systick */
|
/* configure systick */
|
||||||
systick_config();
|
systick_config();
|
||||||
/* configure USART */
|
/* configure USART */
|
||||||
@ -23,11 +27,22 @@ int main(void)
|
|||||||
/* configure FWDGT */
|
/* configure FWDGT */
|
||||||
watchdog_init();
|
watchdog_init();
|
||||||
|
|
||||||
|
#ifdef SOFTWARE_IIC
|
||||||
|
soft_i2c_config();
|
||||||
|
#else
|
||||||
|
i2c_config();
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("system start!\r\n");
|
printf("system start!\r\n");
|
||||||
|
|
||||||
|
ultrasonic_config();
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
printf("hello world!\r\n");
|
gd60914_get_object_tempture();
|
||||||
delay_ms(500);
|
|
||||||
|
delay_ms(50);
|
||||||
|
|
||||||
|
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||||
|
|
||||||
watchdog_reload();
|
watchdog_reload();
|
||||||
}
|
}
|
||||||
|
123
src/rs485_protocol.c
Normal file
123
src/rs485_protocol.c
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
//
|
||||||
|
// Created by yelv1 on 24-12-31.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "rs485_protocol.h"
|
||||||
|
|
||||||
|
extern uint8_t g_temperature_uint8[2];
|
||||||
|
|
||||||
|
void process_command(uint8_t *cmd, size_t length) {
|
||||||
|
char combined_str[3];
|
||||||
|
validation_result_t validate = VALIDATION_SUCCESS;
|
||||||
|
|
||||||
|
validate = (validate_package_header(cmd) |
|
||||||
|
validate_package_type(cmd) |
|
||||||
|
validate_data_length(cmd) |
|
||||||
|
validate_package_crc(cmd, length));
|
||||||
|
|
||||||
|
switch (validate) {
|
||||||
|
case VALIDATION_SUCCESS:
|
||||||
|
// printf("%d", length);
|
||||||
|
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||||
|
if (strcmp(combined_str, "M1") == 0) {
|
||||||
|
ultrasonic_distance_report();
|
||||||
|
} else if (strcmp(combined_str, "M2") == 0) {
|
||||||
|
gd60914_tempture_report();
|
||||||
|
} else if (strcmp(combined_str, "M3") == 0)
|
||||||
|
{
|
||||||
|
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
||||||
|
fwdgt_reset_mcu();
|
||||||
|
} else {
|
||||||
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case VALIDATION_CRC_ERROR:
|
||||||
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
|
||||||
|
break;
|
||||||
|
case VALIDATION_HEADER_ERROR:
|
||||||
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
|
||||||
|
break;
|
||||||
|
case VALIDATION_TYPE_ERROR:
|
||||||
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||||
|
break;
|
||||||
|
case VALIDATION_LENGTH_ERROR:
|
||||||
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
|
||||||
|
uint8_t crc = 0;
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i < data_length - 1; i++) {
|
||||||
|
crc += data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return (uint8_t) (crc & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
|
||||||
|
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
return VALIDATION_CRC_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_package_header(uint8_t *data) {
|
||||||
|
if (data[0] == PROTOCOL_PACKAGE_HEADER) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
return VALIDATION_HEADER_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_package_type(uint8_t *data) {
|
||||||
|
if (data[1] == PROTOCOL_BOARD_TYPE) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
return VALIDATION_TYPE_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_data_length(uint8_t *data) {
|
||||||
|
if (data[2] == PROTOCOL_PACKAGE_LENGTH) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
return VALIDATION_LENGTH_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gd60914_tempture_report(void) {
|
||||||
|
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
||||||
|
|
||||||
|
uint8_t combined_data[5];
|
||||||
|
memcpy(combined_data, package_header, 3);
|
||||||
|
memcpy(combined_data + 3, g_temperature_uint8, 2);
|
||||||
|
|
||||||
|
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
||||||
|
printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]);
|
||||||
|
printf("%c", calculate_crc(combined_data, 6));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ultrasonic_distance_report(void) {
|
||||||
|
static uint16_t distance_uint16 = 0;
|
||||||
|
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
||||||
|
static uint8_t package_data[4] = {0};
|
||||||
|
|
||||||
|
distance_uint16 = ultrasonic_calc_distance();
|
||||||
|
|
||||||
|
package_data[0] = (distance_uint16 >> 8) & 0xFF;
|
||||||
|
package_data[1] = distance_uint16 & 0xFF;
|
||||||
|
|
||||||
|
uint8_t combined_data[7];
|
||||||
|
memcpy(combined_data, package_header, 3);
|
||||||
|
memcpy(combined_data + 3, package_data, 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", calculate_crc(combined_data, 6));
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void soft_i2c_delay(void) {
|
void soft_i2c_delay(void) {
|
||||||
delay_us(20); // Adjust delay as needed
|
delay_us(1); // Adjust delay as needed
|
||||||
/* delay to freq
|
/* delay to freq
|
||||||
* 15KHz: delay_us(20);
|
* 15KHz: delay_us(20);
|
||||||
* 65KHz: delay_us(1);
|
* 65KHz: delay_us(1);
|
||||||
|
154
src/ultrasonic_analog.c
Normal file
154
src/ultrasonic_analog.c
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
//
|
||||||
|
// Created by yelv1 on 24-12-31.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "ultrasonic_analog.h"
|
||||||
|
|
||||||
|
volatile bool ultrasonicMeasurementDone = false;
|
||||||
|
extern uint32_t g_capture_value;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief configure ultrasonic gpio & timer13 ch0 pwm output
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval none
|
||||||
|
*/
|
||||||
|
void ultrasonic_gpio_config(void) {
|
||||||
|
rcu_periph_clock_enable(US_TX_GPIO_RCU);
|
||||||
|
|
||||||
|
gpio_mode_set(US_TX_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, US_TX_PIN);
|
||||||
|
gpio_output_options_set(US_TX_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, US_TX_PIN);
|
||||||
|
gpio_af_set(US_TX_GPIO_PORT, US_TX_GPIO_AF, US_TX_PIN);
|
||||||
|
|
||||||
|
timer_oc_parameter_struct timer_ocinitpara;
|
||||||
|
timer_parameter_struct timer_initpara;
|
||||||
|
|
||||||
|
rcu_periph_clock_enable(US_TX_RCU);
|
||||||
|
timer_deinit(US_TX_TIMER);
|
||||||
|
|
||||||
|
timer_struct_para_init(&timer_initpara);
|
||||||
|
timer_initpara.prescaler = 0;
|
||||||
|
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||||
|
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||||
|
timer_initpara.period = 239;
|
||||||
|
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||||
|
timer_init(US_TX_TIMER, &timer_initpara);
|
||||||
|
|
||||||
|
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
||||||
|
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
|
||||||
|
timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
|
||||||
|
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
||||||
|
timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
|
||||||
|
timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
|
||||||
|
timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
|
||||||
|
timer_channel_output_config(US_TX_TIMER, US_TX_CH, &timer_ocinitpara);
|
||||||
|
|
||||||
|
timer_channel_output_pulse_value_config(US_TX_TIMER, US_TX_CH, 120);
|
||||||
|
timer_channel_output_mode_config(US_TX_TIMER, US_TX_CH, TIMER_OC_MODE_PWM0);
|
||||||
|
timer_auto_reload_shadow_enable(US_TX_TIMER);
|
||||||
|
|
||||||
|
timer_interrupt_enable(US_TX_TIMER, TIMER_INT_UP);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ultrasonic_pwm_out_cycles(const uint8_t cycles) {
|
||||||
|
ultrasonicMeasurementDone = false;
|
||||||
|
uint8_t current_cycle = 0;
|
||||||
|
|
||||||
|
timer_channel_output_pulse_value_config(US_TX_TIMER, US_TX_CH, 120);
|
||||||
|
timer_channel_output_mode_config(US_TX_TIMER, US_TX_CH, TIMER_OC_MODE_PWM1);
|
||||||
|
timer_enable(US_TX_TIMER);
|
||||||
|
|
||||||
|
timer_enable(US_TX_DELAY_TIMER);
|
||||||
|
|
||||||
|
while (current_cycle < cycles)
|
||||||
|
{
|
||||||
|
while (!timer_interrupt_flag_get(US_TX_TIMER, TIMER_INT_FLAG_UP));
|
||||||
|
timer_interrupt_flag_clear(US_TX_TIMER, TIMER_INT_FLAG_UP);
|
||||||
|
current_cycle++;
|
||||||
|
}
|
||||||
|
// delay_nop();
|
||||||
|
timer_disable(US_TX_TIMER);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief configure ultrasonic transmit debounce delay(timer15) 72MHz/72 = 1MHz 1us.
|
||||||
|
\param[in] micro_second: delay time in micro second
|
||||||
|
\param[out] none
|
||||||
|
\retval none
|
||||||
|
*/
|
||||||
|
void ultrasonic_transmit_debounce_delay(const uint16_t micro_second) {
|
||||||
|
rcu_periph_clock_enable(US_TX_DELAY_RCU);
|
||||||
|
timer_deinit(US_TX_DELAY_TIMER);
|
||||||
|
|
||||||
|
timer_parameter_struct timer_initpara;
|
||||||
|
timer_struct_para_init(&timer_initpara);
|
||||||
|
timer_initpara.prescaler = 71;
|
||||||
|
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||||
|
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||||
|
timer_initpara.period = micro_second - 1;
|
||||||
|
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||||
|
timer_initpara.repetitioncounter = 0;
|
||||||
|
timer_init(US_TX_DELAY_TIMER, &timer_initpara);
|
||||||
|
|
||||||
|
timer_auto_reload_shadow_enable(US_TX_DELAY_TIMER);
|
||||||
|
timer_interrupt_enable(US_TX_DELAY_TIMER, TIMER_INT_UP);
|
||||||
|
nvic_irq_enable(TIMER15_IRQn, 1U);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ultrasonic_receive_exti_config(void) {
|
||||||
|
rcu_periph_clock_enable(US_RX_GPIO_RCU);
|
||||||
|
rcu_periph_clock_enable(US_RX_EXTI_RCU);
|
||||||
|
|
||||||
|
gpio_mode_set(US_RX_GPIO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, US_RX_GPIO_PIN);
|
||||||
|
nvic_irq_enable(US_RX_EXTI_IRQ, 0U);
|
||||||
|
syscfg_exti_line_config(EXTI_SOURCE_GPIOA, US_RX_EXTI_LINE);
|
||||||
|
|
||||||
|
exti_init(US_RX_GPIO_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
|
||||||
|
exti_flag_clear(US_RX_GPIO_EXTI);
|
||||||
|
|
||||||
|
// exti_interrupt_enable(EXTI_0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ultrasonic_echo_timer_config(void) {
|
||||||
|
rcu_periph_clock_enable(US_ECHO_RCU);
|
||||||
|
timer_deinit(US_ECHO_TIMER);
|
||||||
|
|
||||||
|
timer_parameter_struct timer_initpara;
|
||||||
|
timer_struct_para_init(&timer_initpara);
|
||||||
|
timer_initpara.prescaler = 71;
|
||||||
|
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||||
|
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||||
|
timer_initpara.period = 59999;
|
||||||
|
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||||
|
timer_initpara.repetitioncounter = 0;
|
||||||
|
timer_init(US_ECHO_TIMER, &timer_initpara);
|
||||||
|
|
||||||
|
timer_ic_parameter_struct timer_icinitpara;
|
||||||
|
timer_channel_input_struct_para_init(&timer_icinitpara);
|
||||||
|
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_BOTH_EDGE;
|
||||||
|
timer_icinitpara.icselection = TIMER_IC_SELECTION_INDIRECTTI;
|
||||||
|
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
|
||||||
|
timer_icinitpara.icfilter = 0x03;
|
||||||
|
timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ultrasonic_config(void) {
|
||||||
|
ultrasonic_gpio_config();
|
||||||
|
ultrasonic_transmit_debounce_delay(TIME_CORRECTION_US);
|
||||||
|
ultrasonic_receive_exti_config();
|
||||||
|
ultrasonic_echo_timer_config();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t ultrasonic_calc_distance(void) {
|
||||||
|
while (!ultrasonicMeasurementDone);
|
||||||
|
// 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;
|
||||||
|
/*
|
||||||
|
* (TIME_CORRECTION_US + us_value) * 340 m/s
|
||||||
|
* -----------------------------------------
|
||||||
|
* 1000 000
|
||||||
|
* ----------------------------------------------
|
||||||
|
* 2
|
||||||
|
*/
|
||||||
|
return distance;
|
||||||
|
}
|
@ -26,7 +26,9 @@ void usart_config(void)
|
|||||||
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
|
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
|
||||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||||
|
nvic_irq_enable(USART_PHY_IRQ, 0);
|
||||||
|
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
||||||
|
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
||||||
usart_enable(USART_PHY);
|
usart_enable(USART_PHY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user