关注+星标公众号,不错过精彩内容
作者 | strongerHuang
微信公众号 | strongerHuang
在春节假期,我也试着在 DeepSeek 上问了一些嵌入式相关的问题,给出的答案,会明显的比国内其他工具要好的多。那么,利用Deepseek学习嵌入式技术,合适吗?
比如问:学习嵌入式的路线
QueueHandle_t uart_rx_queue;
QueueHandle_t uart_tx_queue;
typedef struct {
uint8_t length;
uint8_t data[256];
uint8_t checksum;
} uart_frame_t;
void uart_rx_task(void *pvParameters) {
uint8_t rx_byte;
uart_frame_t rx_frame;
uint8_t state = 0;
uint8_t index = 0;
uint8_t checksum = 0;
while (1) {
if (xQueueReceive(uart_rx_queue, &rx_byte, portMAX_DELAY) == pdPASS) {
switch (state) {
case 0: // 等待起始字节
if (rx_byte == START_BYTE) {
state = 1;
}
break;
case 1: // 读取长度
rx_frame.length = rx_byte;
checksum = rx_byte;
state = 2;
index = 0;
break;
case 2: // 读取数据
rx_frame.data[index++] = rx_byte;
checksum += rx_byte;
if (index >= rx_frame.length) {
state = 3;
}
break;
case 3: // 读取校验和
rx_frame.checksum = rx_byte;
if (checksum == rx_frame.checksum) {
// 校验成功,处理数据
process_rx_frame(&rx_frame);
}
state = 0;
break;
default:
state = 0;
break;
}
}
}
}
void uart_tx_task(void *pvParameters) {
uart_frame_t tx_frame;
while (1) {
if (xQueueReceive(uart_tx_queue, &tx_frame, portMAX_DELAY) == pdPASS) {
// 发送起始字节
USART_SendData(USART1, START_BYTE);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
// 发送长度
USART_SendData(USART1, tx_frame.length);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
// 发送数据
for (uint8_t i = 0; i < tx_frame.length; i++) {
USART_SendData(USART1, tx_frame.data[i]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
// 计算并发送校验和
uint8_t checksum = tx_frame.length;
for (uint8_t i = 0; i < tx_frame.length; i++) {
checksum += tx_frame.data[i];
}
USART_SendData(USART1, checksum);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
}
void process_rx_frame(uart_frame_t *frame) {
// 在这里处理接收到的数据帧
// 例如:打印数据、响应命令等
}
void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
uint8_t rx_byte = USART_ReceiveData(USART1);
xQueueSendFromISR(uart_rx_queue, &rx_byte, NULL);
}
}
int main(void) {
// 初始化硬件和外设
USART_Init();
NVIC_Init();
// 创建队列
uart_rx_queue = xQueueCreate(UART_RX_QUEUE_SIZE, sizeof(uint8_t));
uart_tx_queue = xQueueCreate(UART_TX_QUEUE_SIZE, sizeof(uart_frame_t));
// 创建任务
xTaskCreate(uart_rx_task, "UART_RX", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(uart_tx_task, "UART_TX", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
// 启动调度器
vTaskStartScheduler();
while (1);
}
------------ END ------------
●专栏《嵌入式工具》
●专栏《嵌入式开发》
●专栏《Keil教程》
●嵌入式专栏精选教程
点击“阅读原文”查看更多分享。