sick_dt35/User/task/can.c
2025-04-02 21:42:58 +08:00

53 lines
1.9 KiB
C

/*
CAN总线数据处理
仅发送模式,创建结构体存储发送数据及调试信息。
*/
/* Includes ----------------------------------------------------------------- */
#include "can.h"
#include "task\user_task.h"
#include "bsp\led.h"
#include "device\ads8864.h"
#include "device\can.h"
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
can_t can_debug = {
.TxData = {0},
.TxID = 0x023,
.TxStatus = 0,
.DebugCounter = 0
};
/* Private variables -------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_Can(void *argument) {
(void)argument; // 消除未使用参数的警告
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CAN; // 100Hz
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
CAN_Init(&hcan); // 初始化CAN模块
uint16_t adc_data = 0; // 用于存储ADC数据
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
// 周期性发送数据
if (osMessageQueueGet(task_runtime.msgq.adc, &adc_data, NULL, 100) == osOK) {
// 将 ADC 数据填充到 CAN 数据帧
can_debug.TxData[0] = (adc_data >> 8) & 0xFF; // 高字节
can_debug.TxData[1] = adc_data & 0xFF; // 低字节
// 发送 CAN 数据
can_debug.TxStatus = CAN_SendData(&hcan, can_debug.TxData, can_debug.TxID);
can_debug.DebugCounter++; // 调试计数器递增
}
// 每发送10条数据切换一次LED状态
if (can_debug.DebugCounter % 10 == 0) {
BSP_LED_SET(BSP_LED2, BSP_LED_TAGGLE); // 切换 LED 状态
}
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}