31 lines
1.4 KiB
C
31 lines
1.4 KiB
C
/* Includes ----------------------------------------------------------------- */
|
|
#include "task\user_task.h"
|
|
#include "usart.h"
|
|
#include "bsp\led.h"
|
|
#include <string.h> // 用于 sprintf 函数
|
|
|
|
/* Private variables -------------------------------------------------------- */
|
|
float distance = 14.0f; // 用于存储距离数据单位为米
|
|
// char distance_str[32]; // 用于存储格式化后的字符串
|
|
|
|
/* Exported functions ------------------------------------------------------- */
|
|
void Task_PC(void *argument) {
|
|
(void)argument; // 消除未使用参数的警告
|
|
const uint32_t delay_tick = osKernelGetTickFreq() / 100; // 50Hz
|
|
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
|
|
|
while (1) {
|
|
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
|
BSP_LED_Set(BSP_LED_BLUE, BSP_LED_TAGGLE, 0); // 50% 占空比
|
|
|
|
// 将 distance 格式化为字符串
|
|
// sprintf(distance_str, "Distance: %.2f m\r\n", distance);
|
|
|
|
//使用串口发送"123"
|
|
// HAL_UART_Transmit(&huart2, (uint8_t *)distance_str, strlen(distance_str), 1000); // 发送数据
|
|
// HAL_UART_Transmit(&huart3, (uint8_t *)distance_str, strlen(distance_str), 1000); // 发送数据
|
|
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)"123", 3); // 发送数据
|
|
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)"123", 3); // 发送数据
|
|
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
|
}
|
|
} |