打印
[STM32F1]

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

[复制链接]
4450|33
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
七颗咖啡豆|  楼主 | 2018-4-10 11:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
直接贴上生成的usart.c经过修改后的文件
/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * Copyright (c) 2018 STMicroelectronics International N.V.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other
  *    contributors to this software may be used to endorse or promote products
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under
  *    this license is void and will automatically terminate your rights under
  *    this license.
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

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

#include "gpio.h"

/* USER CODE BEGIN 0 */
void (*UART1_Callback)(uint8_t)=NULL;
void (*UART2_Callback)(uint8_t)=NULL;
void (*UART3_Callback)(uint8_t)=NULL;
uint8_t a,b,c;
/* USER CODE END 0 */

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}
/* USART2 init function */

void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}
/* USART3 init function */

void MX_USART3_UART_Init(void)
{

  huart3.Instance = USART3;
  huart3.Init.BaudRate = 9600;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

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

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    /**USART1 GPIO Configuration   
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

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

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();
  
    /**USART2 GPIO Configuration   
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

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

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspInit 0 */

  /* USER CODE END USART3_MspInit 0 */
    /* USART3 clock enable */
    __HAL_RCC_USART3_CLK_ENABLE();
  
    /**USART3 GPIO Configuration   
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

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

    /* USART3 interrupt Init */
    HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspInit 1 */

  /* USER CODE END USART3_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

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

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration   
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

  /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();
  
    /**USART2 GPIO Configuration   
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

    /* USART2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspDeInit 1 */

  /* USER CODE END USART2_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspDeInit 0 */

  /* USER CODE END USART3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART3_CLK_DISABLE();
  
    /**USART3 GPIO Configuration   
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

    /* USART3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspDeInit 1 */

  /* USER CODE END USART3_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
void StartUART_Rx(UART_HandleTypeDef *uartHandle)
{
        if(uartHandle->Instance==USART1)
        {
                HAL_UART_Receive_IT(uartHandle,&a,1);               
        }
        else if(uartHandle->Instance==USART2)
        {
                HAL_UART_Receive_IT(uartHandle,&b,1);
        }
        else if(uartHandle->Instance==USART3)
        {
                HAL_UART_Receive_IT(uartHandle,&c,1);
        }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *uartHandle)
{       
        if(uartHandle->Instance==USART1)
        {
                if(UART1_Callback!=NULL)
                {
                        UART1_Callback(a);
                }
                HAL_UART_Receive_IT(uartHandle,&a,1);                                       
        }
        else if(uartHandle->Instance==USART2)
        {
                if(UART2_Callback!=NULL)
                {
                        UART2_Callback(b);
                }
                HAL_UART_Receive_IT(uartHandle,&b,1);
        }
        else if(uartHandle->Instance==USART3)
        {
                if(UART3_Callback!=NULL)
                {
                        UART3_Callback(c);
                }
                HAL_UART_Receive_IT(uartHandle,&c,1);
        }
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *uartHandle)
{
        uartHandle->RxState = HAL_UART_STATE_READY;
        if(uartHandle->Instance==USART1)
        {
                HAL_UART_Receive_IT(uartHandle,&a,1);
        }
        else if(uartHandle->Instance==USART2)
        {
                HAL_UART_Receive_IT(uartHandle,&b,1);
        }
        else if(uartHandle->Instance==USART3)
        {
                HAL_UART_Receive_IT(uartHandle,&c,1);
        }
}
/* USER CODE END 1 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (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| | 2018-4-10 20:09 | 只看该作者
受教了,讲的可以

使用特权

评论回复
地板
xuanhuanzi| | 2018-4-15 16:46 | 只看该作者
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。

使用特权

评论回复
5
gygp| | 2018-4-16 22:34 | 只看该作者
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个波特率能够设置多大呢

使用特权

评论回复
6
chenci2013| | 2018-4-16 22:35 | 只看该作者
学习一下。

使用特权

评论回复
7
biechedan| | 2018-4-16 22:35 | 只看该作者
usart.c是自己编写 代码?

使用特权

评论回复
8
wangdezhi| | 2018-4-16 22:35 | 只看该作者
huart3是什么?

使用特权

评论回复
9
isseed| | 2018-4-16 22:36 | 只看该作者
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个简单了。

使用特权

评论回复
10
suzhanhua| | 2018-4-16 22:37 | 只看该作者
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

这个是什么芯片的?

使用特权

评论回复
11
mituzu| | 2018-4-16 22:38 | 只看该作者
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

UART2_Callback = InterruptReviceCallback;还有这种操作呢。

使用特权

评论回复
12
hellosdc| | 2018-4-16 22:38 | 只看该作者

使用特权

评论回复
13
uiint| | 2018-4-16 22:38 | 只看该作者
请问楼主修改之后有什么不同了吗

使用特权

评论回复
14
sanfuzi| | 2018-4-16 22:39 | 只看该作者
xuanhuanzi 发表于 2018-4-15 16:46
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。 ...

HAL库没有使用过。

使用特权

评论回复
15
wangdezhi| | 2018-4-16 22:39 | 只看该作者
huart3跟uart有什么区别呢

使用特权

评论回复
16
biechedan| | 2018-4-16 22:39 | 只看该作者
怎么不使用库函数配置呢

使用特权

评论回复
17
chenci2013| | 2018-4-16 22:39 | 只看该作者
直接使用网上的代码就可以吧。

使用特权

评论回复
18
gygp| | 2018-4-16 22:39 | 只看该作者
七颗咖啡豆 发表于 2018-4-10 11:04
使用:
先调用初始化
static void InterruptReviceCallback(uint8_t a)

能够配置到1Mhz吗?

使用特权

评论回复
19
sanfuzi| | 2018-4-16 22:39 | 只看该作者
xuanhuanzi 发表于 2018-4-15 16:46
静下心来看完,收获很大。之前总觉得32的HAL库难学,这看楼主的代码,竟然懂了。 ...

HAL库不是可以stm32cube配置吗

使用特权

评论回复
20
uiint| | 2018-4-16 22:39 | 只看该作者
串口的效率提高了吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

51

主题

508

帖子

4

粉丝