Compare commits

...

2 Commits

7 changed files with 69 additions and 27 deletions

View File

@ -5,8 +5,8 @@ project(xlsw_3dp_ultrasonic_300K)
set(POWER_VOLTAGE "12V") set(POWER_VOLTAGE "12V")
set(VERSION_MAJOR 0) set(VERSION_MAJOR 0)
set(VERSION_MINOR 0) set(VERSION_MINOR 1)
set(VERSION_PATCH 13) set(VERSION_PATCH 14)
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")

View File

@ -5,7 +5,12 @@
#ifndef RS485_H #ifndef RS485_H
#define RS485_H #define RS485_H
#include "gd32e23x_it.h"
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#define RX_BUFFER_SIZE 64
void process_command(char *cmd);
#endif //RS485_H #endif //RS485_H

View File

@ -23,7 +23,7 @@
#endif #endif
#define ULTRASONIC_CYCLES 0x05U #define ULTRASONIC_CYCLES 0x05U
#define ULTRASONIC_TRAN_US 998 // (ms) #define ULTRASONIC_TRAN_US 498 // (ms)
#define LED_PORT GPIOA #define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_9 #define LED_PIN GPIO_PIN_9

View File

@ -11,19 +11,25 @@
#define MAX_CMD_SIZE 16 #define MAX_CMD_SIZE 16
#define BUFSIZE 8 #define BUFSIZE 8
static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE]; extern uint16_t g_distance_uint16;
static char *strchr_pointer = NULL; extern uint16_t g_temperature_uint16;
static int bufindr = 0; void process_command(char *cmd) {
static int bufindw = 0; if (strncmp(cmd, "M1", 2) == 0) {
static int buflen = 0; printf("M1 -=-=- OK!\r\n");
printf("Distance: %d\r\n", g_distance_uint16);
bool code_seen(char code) } else if (strncmp(cmd, "M2", 2) == 0) {
{ printf("M2 -=-=- OK!\r\n");
strchr_pointer = strchr(cmdbuffer[bufindr], code); printf("Temperature: %d\r\n", g_temperature_uint16);
return (strchr_pointer != NULL); //Return True if a character was found // } else if (strncmp(cmd, "M3", 2) == 0) {
} // char *param_str = cmd + 2; // Skip "M3"
float code_value(void) // int param = atoi(param_str + 1); // Skip "S" and convert to integer
{ // if (param >= 0 && param <= 100) {
return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL)); // printf("M3 with parameter %d -=-=- OK!\r\n", param);
// } else {
// printf("Invalid parameter for M3 command!\r\n");
// }
} else {
printf("Invalid Command!\r\n");
}
} }

View File

@ -37,9 +37,13 @@ OF SUCH DAMAGE.
#include "main.h" #include "main.h"
#include "systick.h" #include "systick.h"
#include "ultrasonic_driver.h" #include "ultrasonic_driver.h"
#include "rs485.h"
__IO uint32_t g_capture_value; __IO uint32_t g_capture_value;
char rx_buffer[RX_BUFFER_SIZE];
uint8_t rx_index = 0;
/*! /*!
\brief this function handles NMI exception \brief this function handles NMI exception
\param[in] none \param[in] none
@ -62,6 +66,7 @@ void NMI_Handler(void)
void HardFault_Handler(void) void HardFault_Handler(void)
{ {
/* if Hard Fault exception occurs, go to infinite loop */ /* if Hard Fault exception occurs, go to infinite loop */
// log_error("HardFault_Handler");
while(1) { while(1) {
} }
} }
@ -156,4 +161,24 @@ void EXTI0_1_IRQHandler(void) {
timer_disable(TIMER16); timer_disable(TIMER16);
exti_interrupt_disable(EXTI_0); 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; // 重置缓冲区索引
}
}
} }

View File

@ -14,7 +14,8 @@
#include "mlx90614.h" #include "mlx90614.h"
extern uint32_t g_capture_value; extern uint32_t g_capture_value;
uint16_t distance_uint16; uint16_t g_distance_uint16;
uint16_t g_temperature_uint16;
/*! /*!
\brief main function \brief main function
@ -40,7 +41,7 @@ int main(void)
/* ---------- debug end ---------- */ /* ---------- debug end ---------- */
printf("\r\n"); 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"); printf("\r\n");
delay_ms(2000); delay_ms(2000);
@ -48,17 +49,19 @@ int main(void)
while (1) while (1)
{ {
delay_ms(ULTRASONIC_TRAN_US); delay_ms(ULTRASONIC_TRAN_US);
UltraSonic_PwmOut_Cycles(ULTRASONIC_CYCLES); UltraSonic_PwmOut_Cycles(ULTRASONIC_CYCLES);
delay_ms(2); delay_ms(2);
printf("cap_val:%ld\t", g_capture_value); // printf("cap_val:%ld\t", g_capture_value);
// const char* result = (g_capture_value <= CAPTURE_VALUE_MAX) ? "Distance: %d\t" : "Over Range\t";
if (g_capture_value <= CAPTURE_VALUE_MAX) {
g_distance_uint16 = UltraSonic_CalcDistance(g_capture_value);
} else {
g_distance_uint16 = 0x0000;
}
// printf(result, distance_uint16);
const char* result = (g_capture_value <= CAPTURE_VALUE_MAX) ? "Distance: %d\t" : "Over Range\t"; // printf("Temp:%d\n", MLX90614_GetObjectTemperature());
distance_uint16 = UltraSonic_CalcDistance(g_capture_value); g_temperature_uint16 = MLX90614_GetObjectTemperature();
printf(result, distance_uint16);
printf("Temp:%d\n", MLX90614_GetObjectTemperature());
} }
} }

View File

@ -57,6 +57,9 @@ void usart_config(void)
usart_transmit_config(USART0_PHY, USART_TRANSMIT_ENABLE); usart_transmit_config(USART0_PHY, USART_TRANSMIT_ENABLE);
usart_enable(USART0_PHY); usart_enable(USART0_PHY);
nvic_irq_enable(USART0_IRQn, 0);
usart_interrupt_enable(USART0_PHY, USART_INT_RBNE);
} }
void UltraSonic_GPIO_Config(void) void UltraSonic_GPIO_Config(void)