找回密码
 立即注册

QQ登录

只需一步,快速开始

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

HAL库版本的stm32 usart dma空闲中断接收源码

[复制链接]
跳转到指定楼层
楼主
ID:256558 发表于 2018-2-2 15:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
分享个stm32单片机的 usart dma空闲中断发送源码
是基于HAL库版本的
单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f4xx_hal.h"
  41. #include "dma.h"
  42. #include "usart.h"
  43. #include "gpio.h"

  44. /* USER CODE BEGIN Includes */
  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/

  49. /* USER CODE END PV */

  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);

  52. /* USER CODE BEGIN PFP */
  53. /* Private function prototypes -----------------------------------------------*/

  54. /* USER CODE END PFP */

  55. /* USER CODE BEGIN 0 */

  56. /* USER CODE END 0 */

  57. int main(void)
  58. {

  59.   /* USER CODE BEGIN 1 */

  60.   /* USER CODE END 1 */

  61.   /* MCU Configuration----------------------------------------------------------*/

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

  64.   /* USER CODE BEGIN Init */

  65.   /* USER CODE END Init */

  66.   /* Configure the system clock */
  67.   SystemClock_Config();

  68.   /* USER CODE BEGIN SysInit */

  69.   /* USER CODE END SysInit */

  70.   /* Initialize all configured peripherals */
  71.   MX_GPIO_Init();
  72.   MX_DMA_Init();
  73.   MX_USART3_UART_Init();
  74.         

  75.   /* USER CODE BEGIN 2 */
  76. HAL_UART_Receive_DMA(&huart3,Test1.Data_Space,DataSize);
  77. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  78.   /* USER CODE END 2 */

  79.   /* Infinite loop */
  80.   /* USER CODE BEGIN WHILE */
  81.   while (1)
  82.   {
  83.   /* USER CODE END WHILE */

  84.   /* USER CODE BEGIN 3 */
  85. if(Test1.Send_Flag==1 && Test1.Send1==1)
  86. {
  87.    HAL_UART_Transmit_DMA(&huart3, SENDDATA, Test1.RLen);  
  88.          Test1.Send1=0;
  89.          Test1.Send_Flag=0;
  90. }
  91. if(Test1.Send_Flag==1 && Test1.Send1==0)
  92. {
  93.    HAL_UART_Transmit_DMA(&huart3, SENDDATB, Test1.RLen);  
  94.          Test1.Send1=1;
  95.          Test1.Send_Flag=0;

  96. }
  97.   }
  98.   /* USER CODE END 3 */

  99. }

  100. /** System Clock Configuration
  101. */
  102. void SystemClock_Config(void)
  103. {

  104.   RCC_OscInitTypeDef RCC_OscInitStruct;
  105.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  106.     /**Configure the main internal regulator output voltage
  107.     */
  108.   __HAL_RCC_PWR_CLK_ENABLE();

  109.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  110.     /**Initializes the CPU, AHB and APB busses clocks
  111.     */
  112.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  113.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  114.   RCC_OscInitStruct.HSICalibrationValue = 16;
  115.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  116.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  117.   RCC_OscInitStruct.PLL.PLLM = 8;
  118.   RCC_OscInitStruct.PLL.PLLN = 84;
  119.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  120.   RCC_OscInitStruct.PLL.PLLQ = 4;
  121.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  122.   {
  123.     _Error_Handler(__FILE__, __LINE__);
  124.   }

  125.     /**Initializes the CPU, AHB and APB busses clocks
  126.     */
  127.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  128.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  129.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  130.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  131.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  132.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  133.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  134.   {
  135.     _Error_Handler(__FILE__, __LINE__);
  136.   }

  137.     /**Configure the Systick interrupt time
  138.     */
  139.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  140.     /**Configure the Systick
  141.     */
  142.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  143.   /* SysTick_IRQn interrupt configuration */
  144.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  145. }

  146. /* USER CODE BEGIN 4 */
  147. void HAL_SYSTICK_Callback(void)
  148. {
  149.         
  150. }
  151. /* USER CODE END 4 */

  152. /**
  153.   * @brief  This function is executed in case of error occurrence.
  154.   * @param  None
  155.   * @retval None
  156.   */
  157. void _Error_Handler(char * file, int line)
  158. {
  159.   /* USER CODE BEGIN Error_Handler_Debug */
  160.   /* User can add his own implementation to report the HAL error return state */
  161.   while(1)
  162.   {
  163.   }
  164.   /* USER CODE END Error_Handler_Debug */
  165. }

  166. #ifdef USE_FULL_ASSERT

  167. /**
  168.    * @brief Reports the name of the source file and the source line number
  169.    * where the assert_param error has occurred.
  170.    * @param file: pointer to the source file name
  171.    * @param line: assert_param error line source number
  172.    * @retval None
  173.    */
  174. void assert_failed(uint8_t* file, uint32_t line)
  175. {
  176.   /* USER CODE BEGIN 6 */
  177.   /* User can add his own implementation to report the file name and line number,
  178.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  179.   /* USER CODE END 6 */

  180. }


  181. #endif

  182. /**
  183.   * @}
  184.   */

  185. /**
  186.   * @}
  187. */

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

所有资料51hei提供下载:
F4usart3_test2.rar (523.35 KB, 下载次数: 53)


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

使用道具 举报

沙发
ID:226410 发表于 2018-6-11 19:04 | 只看该作者
取走了~谢谢了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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