找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2292|回复: 1
打印 上一主题 下一主题
收起左侧

STM32L476串口DMA应用

[复制链接]
跳转到指定楼层
楼主
ID:417297 发表于 2018-10-29 15:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
串口DMA应用

STM32L476单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32l4xx_hal.h"
  4. #include "dma.h"
  5. #include "usart.h"
  6. #include "gpio.h"

  7. /* USER CODE BEGIN Includes */
  8. #include "led_blink.h"
  9. /* USER CODE END Includes */

  10. /* Private variables ---------------------------------------------------------*/

  11. /* USER CODE BEGIN PV */
  12. /* Private variables ---------------------------------------------------------*/
  13. #define APP_BUF_LEN     100
  14.    
  15. uint8_t app_buf[APP_BUF_LEN];
  16. /* USER CODE END PV */

  17. /* Private function prototypes -----------------------------------------------*/
  18. void SystemClock_Config(void);

  19. /* USER CODE BEGIN PFP */
  20. /* Private function prototypes -----------------------------------------------*/

  21. /* USER CODE END PFP */

  22. /* USER CODE BEGIN 0 */

  23. /* USER CODE END 0 */

  24. int main(void)
  25. {

  26.   /* USER CODE BEGIN 1 */
  27.   uint16_t msgLen = 0;
  28.   /* USER CODE END 1 */

  29.   /* MCU Configuration----------------------------------------------------------*/

  30.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  31.   HAL_Init();

  32.   /* USER CODE BEGIN Init */

  33.   /* USER CODE END Init */

  34.   /* Configure the system clock */
  35.   SystemClock_Config();

  36.   /* USER CODE BEGIN SysInit */

  37.   /* USER CODE END SysInit */

  38.   /* Initialize all configured peripherals */
  39.   MX_GPIO_Init();
  40.   MX_DMA_Init();
  41.   MX_USART1_UART_Init();

  42.   /* USER CODE BEGIN 2 */
  43.   Bsp_init();
  44.   HAL_LedCtl_Init();
  45.   HAL_UARTDMA_Init();
  46.   printf("NB_EK Uart in DMA example\r\n");
  47.   /* USER CODE END 2 */

  48.   /* Infinite loop */
  49.   /* USER CODE BEGIN WHILE */
  50.   while (1)
  51.   {
  52.   /* USER CODE END WHILE */

  53.   /* USER CODE BEGIN 3 */
  54.     HAL_Led_poll();
  55.     if(HAL_UART_Poll())
  56.     {
  57.       msgLen = HAL_UART_RxBufLen();
  58.       
  59.       if(msgLen > APP_BUF_LEN)
  60.       {
  61.         msgLen = APP_BUF_LEN;
  62.       }
  63.       msgLen = HAL_UART_Read(app_buf,msgLen);
  64.       HAL_UART_Write(app_buf,msgLen);
  65.       HAL_LED_Blink(200,20,1);
  66.     }
  67.   }
  68.   /* USER CODE END 3 */

  69. }

  70. /** System Clock Configuration
  71. */
  72. void SystemClock_Config(void)
  73. {

  74.   RCC_OscInitTypeDef RCC_OscInitStruct;
  75.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  76.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  77.     /**Initializes the CPU, AHB and APB busses clocks
  78.     */
  79.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  80.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  81.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  82.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  83.   RCC_OscInitStruct.PLL.PLLM = 1;
  84.   RCC_OscInitStruct.PLL.PLLN = 20;
  85.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  86.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  87.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  88.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  89.   {
  90.     _Error_Handler(__FILE__, __LINE__);
  91.   }

  92.     /**Initializes the CPU, AHB and APB busses clocks
  93.     */
  94.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  95.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  96.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  97.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  98.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  99.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  100.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  101.   {
  102.     _Error_Handler(__FILE__, __LINE__);
  103.   }

  104.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  105.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  106.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  107.   {
  108.     _Error_Handler(__FILE__, __LINE__);
  109.   }

  110.     /**Configure the main internal regulator output voltage
  111.     */
  112.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  113.   {
  114.     _Error_Handler(__FILE__, __LINE__);
  115.   }

  116.     /**Configure the Systick interrupt time
  117.     */
  118.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  119.     /**Configure the Systick
  120.     */
  121.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  122.   /* SysTick_IRQn interrupt configuration */
  123.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  124. }

  125. /* USER CODE BEGIN 4 */

  126. /* USER CODE END 4 */

  127. /**
  128.   * @brief  This function is executed in case of error occurrence.
  129.   * @param  None
  130.   * @retval None
  131.   */
  132. void _Error_Handler(char * file, int line)
  133. {
  134.   /* USER CODE BEGIN Error_Handler_Debug */
  135.   /* User can add his own implementation to report the HAL error return state */
  136.   while(1)
  137.   {
  138.   }
  139.   /* USER CODE END Error_Handler_Debug */
  140. }

  141. #ifdef USE_FULL_ASSERT

  142. /**
  143.    * @brief Reports the name of the source file and the source line number
  144.    * where the assert_param error has occurred.
  145.    * @param file: pointer to the source file name
  146.    * @param line: assert_param error line source number
  147.    * @retval None
  148.    */
  149. void assert_failed(uint8_t* file, uint32_t line)
  150. {
  151.   /* USER CODE BEGIN 6 */
  152.   /* User can add his own implementation to report the file name and line number,
  153.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  154.   /* USER CODE END 6 */

  155. }

  156. #endif

  157. /**
  158.   * @}
  159.   */

  160. /**
  161.   * @}
  162. */

  163. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
07-串口DMA接发实验.rar (9.24 MB, 下载次数: 40)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:370815 发表于 2019-10-2 14:56 | 只看该作者
为啥我用这段代码,串口工具只有接收,不能发送呢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表