Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
0398076df2 | |||
0a85320c68 | |||
f5a936e022 | |||
699a8218f7 | |||
70e3e162ae | |||
5936f69ec4 |
@ -5,8 +5,8 @@ project(xlsw_3dp_ultrasonic_300K)
|
||||
|
||||
set(POWER_VOLTAGE "12V")
|
||||
set(VERSION_MAJOR 0)
|
||||
set(VERSION_MINOR 0)
|
||||
set(VERSION_PATCH 13)
|
||||
set(VERSION_MINOR 1)
|
||||
set(VERSION_PATCH 14)
|
||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
|
||||
|
||||
@ -29,6 +29,7 @@ set(TARGET_C_SRC
|
||||
${CMAKE_SOURCE_DIR}/src/systick.c
|
||||
${CMAKE_SOURCE_DIR}/src/ultrasonic_driver.c
|
||||
${CMAKE_SOURCE_DIR}/src/mlx90614.c
|
||||
${CMAKE_SOURCE_DIR}/src/RS485.c
|
||||
)
|
||||
|
||||
add_executable(xlsw_3dp_ultrasonic_300K ${TARGET_C_SRC})
|
||||
|
@ -33,8 +33,6 @@ find_program(CMAKE_OBJCOPY NAMES ${TARGET_TRIPLET}-objcopy HINTS ${TOOLCHAIN_BIN
|
||||
find_program(CMAKE_OBJDUMP NAMES ${TARGET_TRIPLET}-objdump HINTS ${TOOLCHAIN_BIN_PATH})
|
||||
find_program(CMAKE_SIZE NAMES ${TARGET_TRIPLET}-size HINTS ${TOOLCHAIN_BIN_PATH})
|
||||
|
||||
|
||||
|
||||
function(print_size_of_target TARGET)
|
||||
add_custom_target(${TARGET}_always_display_size
|
||||
ALL COMMAND ${CMAKE_SIZE} "$<TARGET_FILE:${TARGET}>"
|
||||
|
16
inc/RS485.h
Normal file
16
inc/RS485.h
Normal file
@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by dell on 24-11-29.
|
||||
//
|
||||
|
||||
#ifndef RS485_H
|
||||
#define RS485_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define RX_BUFFER_SIZE 64
|
||||
|
||||
void process_command(char *cmd);
|
||||
|
||||
#endif //RS485_H
|
@ -23,7 +23,7 @@
|
||||
#endif
|
||||
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
#define ULTRASONIC_TRAN_US 998 // (ms)
|
||||
#define ULTRASONIC_TRAN_US 498 // (ms)
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_9
|
||||
@ -34,12 +34,14 @@
|
||||
|
||||
#define USART_RCU RCU_USART0
|
||||
#define USART_GPIO_RCU RCU_GPIOA
|
||||
#define USARET_GPIO_PORT GPIOA
|
||||
#define USART_GPIO_PORT GPIOA
|
||||
#define USART_TX_PIN GPIO_PIN_2
|
||||
#define USART_RX_PIN GPIO_PIN_3
|
||||
#define USART0_PHY USART0
|
||||
#define USART_BAUDRATE 115200U
|
||||
|
||||
#define USART_EN_PIN GPIO_PIN_4
|
||||
|
||||
#define US_TRAN_GPIO_RCU RCU_GPIOB
|
||||
#define US_TRAN_GPIO_PORT GPIOB
|
||||
#define US_TRAN_PIN GPIO_PIN_1
|
||||
|
@ -1,7 +1,7 @@
|
||||
# 连接cmsis-dap(喝粥)
|
||||
; interface cmsis-dap
|
||||
source [find interface/cmsis-dap.cfg]
|
||||
|
||||
; source [find interface/jlink.cfg]
|
||||
# 选择SWD
|
||||
transport select swd
|
||||
|
||||
|
35
src/RS485.c
Normal file
35
src/RS485.c
Normal file
@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by dell on 24-11-29.
|
||||
//
|
||||
|
||||
#include "RS485.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_CMD_SIZE 16
|
||||
#define BUFSIZE 8
|
||||
|
||||
extern uint16_t g_distance_uint16;
|
||||
extern uint16_t g_temperature_uint16;
|
||||
|
||||
void process_command(char *cmd) {
|
||||
if (strncmp(cmd, "M1", 2) == 0) {
|
||||
printf("M1 -=-=- OK!\r\n");
|
||||
printf("Distance: %d\r\n", g_distance_uint16);
|
||||
} else if (strncmp(cmd, "M2", 2) == 0) {
|
||||
printf("M2 -=-=- OK!\r\n");
|
||||
printf("Temperature: %d\r\n", g_temperature_uint16);
|
||||
// } else if (strncmp(cmd, "M3", 2) == 0) {
|
||||
// char *param_str = cmd + 2; // Skip "M3"
|
||||
// int param = atoi(param_str + 1); // Skip "S" and convert to integer
|
||||
// if (param >= 0 && param <= 100) {
|
||||
// printf("M3 with parameter %d -=-=- OK!\r\n", param);
|
||||
// } else {
|
||||
// printf("Invalid parameter for M3 command!\r\n");
|
||||
// }
|
||||
} else {
|
||||
printf("Invalid Command!\r\n");
|
||||
}
|
||||
}
|
@ -37,9 +37,13 @@ OF SUCH DAMAGE.
|
||||
#include "main.h"
|
||||
#include "systick.h"
|
||||
#include "ultrasonic_driver.h"
|
||||
#include "rs485.h"
|
||||
|
||||
__IO uint32_t g_capture_value;
|
||||
|
||||
char rx_buffer[RX_BUFFER_SIZE];
|
||||
uint8_t rx_index = 0;
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
\param[in] none
|
||||
@ -62,6 +66,7 @@ void NMI_Handler(void)
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* if Hard Fault exception occurs, go to infinite loop */
|
||||
// log_error("HardFault_Handler");
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
@ -156,4 +161,24 @@ void EXTI0_1_IRQHandler(void) {
|
||||
timer_disable(TIMER16);
|
||||
exti_interrupt_disable(EXTI_0);
|
||||
}
|
||||
}
|
||||
|
||||
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; // 重置缓冲区索引
|
||||
}
|
||||
}
|
||||
}
|
26
src/main.c
26
src/main.c
@ -14,7 +14,8 @@
|
||||
#include "mlx90614.h"
|
||||
|
||||
extern uint32_t g_capture_value;
|
||||
uint16_t distance_uint16;
|
||||
uint16_t g_distance_uint16;
|
||||
uint16_t g_temperature_uint16;
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
@ -34,13 +35,10 @@ int main(void)
|
||||
|
||||
/* ---------- debug start ---------- */
|
||||
|
||||
|
||||
|
||||
|
||||
/* ---------- debug end ---------- */
|
||||
|
||||
printf("\r\n");
|
||||
printf("XLSW-3DP-UltraSonic Analog 300K!\r\n");
|
||||
printf("XLSW-3DP-UltraSonic Analog 300K! V0.1.14\r\n");
|
||||
printf("\r\n");
|
||||
|
||||
delay_ms(2000);
|
||||
@ -48,17 +46,19 @@ int main(void)
|
||||
while (1)
|
||||
{
|
||||
delay_ms(ULTRASONIC_TRAN_US);
|
||||
|
||||
UltraSonic_PwmOut_Cycles(ULTRASONIC_CYCLES);
|
||||
|
||||
delay_ms(2);
|
||||
printf("cap_val:%ld\t", g_capture_value);
|
||||
// printf("cap_val:%ld\t", g_capture_value);
|
||||
if (g_capture_value <= CAPTURE_VALUE_MAX) {
|
||||
g_distance_uint16 = UltraSonic_CalcDistance(g_capture_value);
|
||||
} else {
|
||||
g_distance_uint16 = 0x0000;
|
||||
}
|
||||
// const char* result = (g_capture_value <= CAPTURE_VALUE_MAX) ? "Distance: %d\t" : "Over Range\t";
|
||||
// printf(result, distance_uint16);
|
||||
|
||||
const char* result = (g_capture_value <= CAPTURE_VALUE_MAX) ? "Distance: %d\t" : "Over Range\t";
|
||||
distance_uint16 = UltraSonic_CalcDistance(g_capture_value);
|
||||
printf(result, distance_uint16);
|
||||
|
||||
printf("Temp:%d\n", MLX90614_GetObjectTemperature());
|
||||
// printf("Temp:%d\n", MLX90614_GetObjectTemperature());
|
||||
g_temperature_uint16 = MLX90614_GetObjectTemperature();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,11 +39,16 @@ void usart_config(void)
|
||||
rcu_periph_clock_enable(USART_GPIO_RCU);
|
||||
rcu_periph_clock_enable(USART_RCU);
|
||||
|
||||
gpio_af_set(USARET_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
||||
gpio_af_set(USART_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
||||
|
||||
/* configure USART Tx as alternate function push-pull */
|
||||
gpio_mode_set(USARET_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN | USART_RX_PIN);
|
||||
gpio_output_options_set(USARET_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN | USART_RX_PIN);
|
||||
/* 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_OUTPUT, GPIO_PUPD_NONE, USART_EN_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, USART_EN_PIN);
|
||||
gpio_bit_write(USART_GPIO_PORT, USART_EN_PIN, SET);
|
||||
|
||||
/* USART configure */
|
||||
usart_deinit(USART0_PHY);
|
||||
@ -52,6 +57,9 @@ void usart_config(void)
|
||||
usart_transmit_config(USART0_PHY, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART0_PHY);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
usart_interrupt_enable(USART0_PHY, USART_INT_RBNE);
|
||||
}
|
||||
|
||||
void UltraSonic_GPIO_Config(void)
|
||||
|
Loading…
x
Reference in New Issue
Block a user