QJ32F407 USART串口_DMA发送和接收 CubeMx设置串口设置PB11和PB10为串口3的GPIO设置波特率设置中断开启发送接收DMA编写测试代码/* USER CODE BEGIN Header *//** ****************************************************************************** * file : main.c * brief : Main program body ****************************************************************************** * attention * * Copyright (c) 2026 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* USER CODE END Header *//* Includes ------------------------------------------------------------------*/#includemain.h#includedma.h#includeusart.h#includegpio.h/* Private includes ----------------------------------------------------------*//* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*//* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*//* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*//* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */uint8_tRX_Buff_FLAG0;uint8_tRX_Buff[300]{0};uint8_tTX_Buff[300]{0};intRX_Length0;/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/voidSystemClock_Config(void);/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*//* USER CODE BEGIN 0 */// 串口接收空闲函数voidHAL_UARTEx_RxEventCallback(UART_HandleTypeDef*huart,uint16_tSize){if(huarthuart3){RX_Buff_FLAG1;RX_Length300-__HAL_DMA_GET_COUNTER(huart-hdmarx);HAL_UARTEx_ReceiveToIdle_DMA(huart3,RX_Buff,300);}}/* USER CODE END 0 *//** * brief The application entry point. * retval int */intmain(void){/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_USART3_UART_Init();/* USER CODE BEGIN 2 */// 启动串口DMA接收HAL_UARTEx_ReceiveToIdle_DMA(huart3,RX_Buff,300);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while(1){HAL_GPIO_WritePin(GPIOF,GPIO_PIN_6,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_7,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_SET);HAL_Delay(100);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_6,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_7,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET);HAL_Delay(100);// 发送数据TX_Buff[0]0x01;TX_Buff[1]0x02;TX_Buff[2]0x03;TX_Buff[4]RX_Buff[0];TX_Buff[5]RX_Buff[1];TX_Buff[6]RX_Buff[2];HAL_UART_Transmit_DMA(huart3,TX_Buff,10);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */}/** * brief System Clock Configuration * retval None */voidSystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct{0};RCC_ClkInitTypeDef RCC_ClkInitStruct{0};/** Configure the main internal regulator output voltage */__HAL_RCC_PWR_CLK_ENABLE();__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);/** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */RCC_OscInitStruct.OscillatorTypeRCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEStateRCC_HSE_ON;RCC_OscInitStruct.PLL.PLLStateRCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSourceRCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM4;RCC_OscInitStruct.PLL.PLLN168;RCC_OscInitStruct.PLL.PLLPRCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ4;if(HAL_RCC_OscConfig(RCC_OscInitStruct)!HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks */RCC_ClkInitStruct.ClockTypeRCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSourceRCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDividerRCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDividerRCC_HCLK_DIV4;RCC_ClkInitStruct.APB2CLKDividerRCC_HCLK_DIV2;if(HAL_RCC_ClockConfig(RCC_ClkInitStruct,FLASH_LATENCY_5)!HAL_OK){Error_Handler();}}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//** * brief This function is executed in case of error occurrence. * retval None */voidError_Handler(void){/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while(1){}/* USER CODE END Error_Handler_Debug */}#ifdefUSE_FULL_ASSERT/** * brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * param file: pointer to the source file name * param line: assert_param error line source number * retval None */voidassert_failed(uint8_t*file,uint32_tline){/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number, ex: printf(Wrong parameters value: file %s on line %d\r\n, file, line) *//* USER CODE END 6 */}#endif/* USE_FULL_ASSERT */代码运行结果串口工具接收和发送数据01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 00 00 00 00 00 00 00 01 02 03 04 05 06 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00 01 02 03 00 01 02 03 00 00 00示例工程https://download.csdn.net/download/hmxm6/93126339

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/21 18:32:40
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/21 18:32:39
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/21 18:31:24
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/21 18:31:34
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/21 18:31:25
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/21 18:31:13

日新闻

周新闻