ST MCU Finder
安装免费手机应用,
寻找理想的ST MCU

[STM32F1] 【分享】HAL串口的正确使用

[复制链接]
500|33
 楼主 | 2018-4-10 11:02 | 显示全部楼层 |阅读模式
直接贴上生成的usart.c经过修改后的文件
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : USART.c
  4.   * Description        : This file provides code for the configuration
  5.   *                      of the USART instances.
  6.   ******************************************************************************
  7.   * This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * Copyright (c) 2018 STMicroelectronics International N.V.
  14.   * All rights reserved.
  15.   *
  16.   * Redistribution and use in source and binary forms, with or without
  17.   * modification, are permitted, provided that the following conditions are met:
  18.   *
  19.   * 1. Redistribution of source code must retain the above copyright notice,
  20.   *    this list of conditions and the following disclaimer.
  21.   * 2. Redistributions in binary form must reproduce the above copyright notice,
  22.   *    this list of conditions and the following disclaimer in the documentation
  23.   *    and/or other materials provided with the distribution.
  24.   * 3. Neither the name of STMicroelectronics nor the names of other
  25.   *    contributors to this software may be used to endorse or promote products
  26.   *    derived from this software without specific written permission.
  27.   * 4. This software, including modifications and/or derivative works of this
  28.   *    software, must execute solely and exclusively on microcontroller or
  29.   *    microprocessor devices manufactured by or for STMicroelectronics.
  30.   * 5. Redistribution and use of this software other than as permitted under
  31.   *    this license is void and will automatically terminate your rights under
  32.   *    this license.
  33.   *
  34.   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35.   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36.   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37.   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38.   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39.   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40.   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42.   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43.   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44.   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45.   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46.   *
  47.   ******************************************************************************
  48.   */

  49. /* Includes ------------------------------------------------------------------*/
  50. #include "usart.h"

  51. #include "gpio.h"

  52. /* USER CODE BEGIN 0 */
  53. void (*UART1_Callback)(uint8_t)=NULL;
  54. void (*UART2_Callback)(uint8_t)=NULL;
  55. void (*UART3_Callback)(uint8_t)=NULL;
  56. uint8_t a,b,c;
  57. /* USER CODE END 0 */

  58. UART_HandleTypeDef huart1;
  59. UART_HandleTypeDef huart2;
  60. UART_HandleTypeDef huart3;

  61. /* USART1 init function */

  62. void MX_USART1_UART_Init(void)
  63. {

  64.   huart1.Instance = USART1;
  65.   huart1.Init.BaudRate = 9600;
  66.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  67.   huart1.Init.StopBits = UART_STOPBITS_1;
  68.   huart1.Init.Parity = UART_PARITY_NONE;
  69.   huart1.Init.Mode = UART_MODE_TX_RX;
  70.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  71.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  72.   if (HAL_UART_Init(&huart1) != HAL_OK)
  73.   {
  74.     _Error_Handler(__FILE__, __LINE__);
  75.   }

  76. }
  77. /* USART2 init function */

  78. void MX_USART2_UART_Init(void)
  79. {

  80.   huart2.Instance = USART2;
  81.   huart2.Init.BaudRate = 9600;
  82.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  83.   huart2.Init.StopBits = UART_STOPBITS_1;
  84.   huart2.Init.Parity = UART_PARITY_NONE;
  85.   huart2.Init.Mode = UART_MODE_TX_RX;
  86.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  87.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  88.   if (HAL_UART_Init(&huart2) != HAL_OK)
  89.   {
  90.     _Error_Handler(__FILE__, __LINE__);
  91.   }

  92. }
  93. /* USART3 init function */

  94. void MX_USART3_UART_Init(void)
  95. {

  96.   huart3.Instance = USART3;
  97.   huart3.Init.BaudRate = 9600;
  98.   huart3.Init.WordLength = UART_WORDLENGTH_8B;
  99.   huart3.Init.StopBits = UART_STOPBITS_1;
  100.   huart3.Init.Parity = UART_PARITY_NONE;
  101.   huart3.Init.Mode = UART_MODE_TX_RX;
  102.   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  103.   huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  104.   if (HAL_UART_Init(&huart3) != HAL_OK)
  105.   {
  106.     _Error_Handler(__FILE__, __LINE__);
  107.   }

  108. }

  109. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  110. {

  111.   GPIO_InitTypeDef GPIO_InitStruct;
  112.   if(uartHandle->Instance==USART1)
  113.   {
  114.   /* USER CODE BEGIN USART1_MspInit 0 */

  115.   /* USER CODE END USART1_MspInit 0 */
  116.     /* USART1 clock enable */
  117.     __HAL_RCC_USART1_CLK_ENABLE();
  118.   
  119.     /**USART1 GPIO Configuration   
  120.     PA9     ------> USART1_TX
  121.     PA10     ------> USART1_RX
  122.     */
  123.     GPIO_InitStruct.Pin = GPIO_PIN_9;
  124.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  125.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  126.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  127.     GPIO_InitStruct.Pin = GPIO_PIN_10;
  128.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  129.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  130.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  131.     /* USART1 interrupt Init */
  132.     HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
  133.     HAL_NVIC_EnableIRQ(USART1_IRQn);
  134.   /* USER CODE BEGIN USART1_MspInit 1 */

  135.   /* USER CODE END USART1_MspInit 1 */
  136.   }
  137.   else if(uartHandle->Instance==USART2)
  138.   {
  139.   /* USER CODE BEGIN USART2_MspInit 0 */

  140.   /* USER CODE END USART2_MspInit 0 */
  141.     /* USART2 clock enable */
  142.     __HAL_RCC_USART2_CLK_ENABLE();
  143.   
  144.     /**USART2 GPIO Configuration   
  145.     PA2     ------> USART2_TX
  146.     PA3     ------> USART2_RX
  147.     */
  148.     GPIO_InitStruct.Pin = GPIO_PIN_2;
  149.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  150.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  151.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  152.     GPIO_InitStruct.Pin = GPIO_PIN_3;
  153.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  154.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  155.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  156.     /* USART2 interrupt Init */
  157.     HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
  158.     HAL_NVIC_EnableIRQ(USART2_IRQn);
  159.   /* USER CODE BEGIN USART2_MspInit 1 */

  160.   /* USER CODE END USART2_MspInit 1 */
  161.   }
  162.   else if(uartHandle->Instance==USART3)
  163.   {
  164.   /* USER CODE BEGIN USART3_MspInit 0 */

  165.   /* USER CODE END USART3_MspInit 0 */
  166.     /* USART3 clock enable */
  167.     __HAL_RCC_USART3_CLK_ENABLE();
  168.   
  169.     /**USART3 GPIO Configuration   
  170.     PB10     ------> USART3_TX
  171.     PB11     ------> USART3_RX
  172.     */
  173.     GPIO_InitStruct.Pin = GPIO_PIN_10;
  174.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  175.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  176.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  177.     GPIO_InitStruct.Pin = GPIO_PIN_11;
  178.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  179.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  180.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  181.     /* USART3 interrupt Init */
  182.     HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
  183.     HAL_NVIC_EnableIRQ(USART3_IRQn);
  184.   /* USER CODE BEGIN USART3_MspInit 1 */

  185.   /* USER CODE END USART3_MspInit 1 */
  186.   }
  187. }

  188. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  189. {

  190.   if(uartHandle->Instance==USART1)
  191.   {
  192.   /* USER CODE BEGIN USART1_MspDeInit 0 */

  193.   /* USER CODE END USART1_MspDeInit 0 */
  194.     /* Peripheral clock disable */
  195.     __HAL_RCC_USART1_CLK_DISABLE();
  196.   
  197.     /**USART1 GPIO Configuration   
  198.     PA9     ------> USART1_TX
  199.     PA10     ------> USART1_RX
  200.     */
  201.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

  202.     /* USART1 interrupt Deinit */
  203.     HAL_NVIC_DisableIRQ(USART1_IRQn);
  204.   /* USER CODE BEGIN USART1_MspDeInit 1 */

  205.   /* USER CODE END USART1_MspDeInit 1 */
  206.   }
  207.   else if(uartHandle->Instance==USART2)
  208.   {
  209.   /* USER CODE BEGIN USART2_MspDeInit 0 */

  210.   /* USER CODE END USART2_MspDeInit 0 */
  211.     /* Peripheral clock disable */
  212.     __HAL_RCC_USART2_CLK_DISABLE();
  213.   
  214.     /**USART2 GPIO Configuration   
  215.     PA2     ------> USART2_TX
  216.     PA3     ------> USART2_RX
  217.     */
  218.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

  219.     /* USART2 interrupt Deinit */
  220.     HAL_NVIC_DisableIRQ(USART2_IRQn);
  221.   /* USER CODE BEGIN USART2_MspDeInit 1 */

  222.   /* USER CODE END USART2_MspDeInit 1 */
  223.   }
  224.   else if(uartHandle->Instance==USART3)
  225.   {
  226.   /* USER CODE BEGIN USART3_MspDeInit 0 */

  227.   /* USER CODE END USART3_MspDeInit 0 */
  228.     /* Peripheral clock disable */
  229.     __HAL_RCC_USART3_CLK_DISABLE();
  230.   
  231.     /**USART3 GPIO Configuration   
  232.     PB10     ------> USART3_TX
  233.     PB11     ------> USART3_RX
  234.     */
  235.     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

  236.     /* USART3 interrupt Deinit */
  237.     HAL_NVIC_DisableIRQ(USART3_IRQn);
  238.   /* USER CODE BEGIN USART3_MspDeInit 1 */

  239.   /* USER CODE END USART3_MspDeInit 1 */
  240.   }
  241. }

  242. /* USER CODE BEGIN 1 */
  243. void StartUART_Rx(UART_HandleTypeDef *uartHandle)
  244. {
  245.         if(uartHandle->Instance==USART1)
  246.         {
  247.                 HAL_UART_Receive_IT(uartHandle,&a,1);               
  248.         }
  249.         else if(uartHandle->Instance==USART2)
  250.         {
  251.                 HAL_UART_Receive_IT(uartHandle,&b,1);
  252.         }
  253.         else if(uartHandle->Instance==USART3)
  254.         {
  255.                 HAL_UART_Receive_IT(uartHandle,&c,1);
  256.         }
  257. }

  258. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *uartHandle)
  259. {       
  260.         if(uartHandle->Instance==USART1)
  261.         {
  262.                 if(UART1_Callback!=NULL)
  263.                 {
  264.                         UART1_Callback(a);
  265.                 }
  266.                 HAL_UART_Receive_IT(uartHandle,&a,1);                                       
  267.         }
  268.         else if(uartHandle->Instance==USART2)
  269.         {
  270.                 if(UART2_Callback!=NULL)
  271.                 {
  272.                         UART2_Callback(b);
  273.                 }
  274.                 HAL_UART_Receive_IT(uartHandle,&b,1);
  275.         }
  276.         else if(uartHandle->Instance==USART3)
  277.         {
  278.                 if(UART3_Callback!=NULL)
  279.                 {
  280.                         UART3_Callback(c);
  281.                 }
  282.                 HAL_UART_Receive_IT(uartHandle,&c,1);
  283.         }
  284. }
  285. void HAL_UART_ErrorCallback(UART_HandleTypeDef *uartHandle)
  286. {
  287.         uartHandle->RxState = HAL_UART_STATE_READY;
  288.         if(uartHandle->Instance==USART1)
  289.         {
  290.                 HAL_UART_Receive_IT(uartHandle,&a,1);
  291.         }
  292.         else if(uartHandle->Instance==USART2)
  293.         {
  294.                 HAL_UART_Receive_IT(uartHandle,&b,1);
  295.         }
  296.         else if(uartHandle->Instance==USART3)
  297.         {
  298.                 HAL_UART_Receive_IT(uartHandle,&c,1);
  299.         }
  300. }
  301. /* USER CODE END 1 */

  302. /**
  303.   * @}
  304.   */

  305. /**
  306.   * @}
  307.   */

  308. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
 楼主 | 2018-4-10 11:04 | 显示全部楼层
本帖最后由 七颗咖啡豆 于 2018-4-10 11:07 编辑

使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)
{
  //类似标准库中断函数   这里不需要处理各种状态寄存器了
   RxBuffer[Rx_p++] = a;
}
void ConnectDetectorInit()
{
        UART2_Callback = InterruptReviceCallback;
        StartUART_Rx(&huart2);
}

接下来标准库代码怎么处理就怎么处理

| 2018-4-10 20:09 | 显示全部楼层
受教了,讲的可以
| 2018-4-15 16:46 | 显示全部楼层
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。
| 2018-4-16 22:34 | 显示全部楼层
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个波特率能够设置多大呢
| 2018-4-16 22:35 | 显示全部楼层
学习一下。
| 2018-4-16 22:35 | 显示全部楼层
usart.c是自己编写 代码?
| 2018-4-16 22:35 | 显示全部楼层
huart3是什么?
| 2018-4-16 22:36 | 显示全部楼层
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个简单了。
| 2018-4-16 22:37 | 显示全部楼层
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个是什么芯片的?
| 2018-4-16 22:38 | 显示全部楼层
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

UART2_Callback = InterruptReviceCallback;还有这种操作呢。
| 2018-4-16 22:38 | 显示全部楼层
| 2018-4-16 22:38 | 显示全部楼层
请问楼主修改之后有什么不同了吗
| 2018-4-16 22:39 | 显示全部楼层
xuanhuanzi 发表于 2018-4-15 16:46
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。 ...

HAL库没有使用过。
| 2018-4-16 22:39 | 显示全部楼层
huart3跟uart有什么区别呢
| 2018-4-16 22:39 | 显示全部楼层
怎么不使用库函数配置呢
| 2018-4-16 22:39 | 显示全部楼层
直接使用网上的代码就可以吧。
| 2018-4-16 22:39 | 显示全部楼层
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

能够配置到1Mhz吗?
| 2018-4-16 22:39 | 显示全部楼层
xuanhuanzi 发表于 2018-4-15 16:46
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。 ...

HAL库不是可以stm32cube配置吗
| 2018-4-16 22:39 | 显示全部楼层
串口的效率提高了吗?
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式
我要创建版块 申请成为版主

论坛热帖

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