查看: 832|回复: 1
打印 上一主题 下一主题

【L476 Nucleo】UART3 使用

[复制链接] qrcode

20

主题

33

帖子

94

积分

注册会员

Rank: 2

积分
94
楼主
跳转到指定楼层
发表于 2016-5-22 05:50 PM | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


主要内容:使用STM32 L475 的 串口 三打印信息。和用户交互。

L476 的UART3 连接的是PC4和PC5 ,分别对应 TX和RX:
PC4  ----- TX
PC5  ----- RX


使用USB ---TTL 模块和PC4、5 连接即可。使用cubeMX 配置相应的串口代码,既可以与外部通信。
关于CubeMX 生成UART代码的过程这里就不详细介绍了,可以参考以前的帖子。

串口代码配置如下:

  1. <font face="微软雅黑" size="3">void MX_USART3_UART_Init(void)
  2. {

  3.   huart3.Instance = USART3;
  4.   huart3.Init.BaudRate = 9600;
  5.   huart3.Init.WordLength = UART_WORDLENGTH_8B;
  6.   huart3.Init.StopBits = UART_STOPBITS_1;
  7.   huart3.Init.Parity = UART_PARITY_NONE;
  8.   huart3.Init.Mode = UART_MODE_TX_RX;
  9.   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  10.   huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  11.   huart3.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
  12.   huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  13.   HAL_UART_Init(&huart3);

  14. }</font>
复制代码
本文中采用的是9600的波特率,开启了串口的中断传输的功能。
CubeMX 的固件库,开始使用有点不太习惯了,不过开发上CubeMX 确实上手简单了,相信以后会用的越来越多的。
  1. <font face="微软雅黑" size="4"> uint32_t BaudRate
  2.  uint32_t WordLength
  3.  uint32_t StopBits
  4.  uint32_t Parity
  5.  uint32_t Mode
  6.  uint32_t HwFlowCtl
  7.  uint32_t OverSampling
  8.  uint32_t OneBitSampling</font>
复制代码
官方了也给出了如何使用HAL固件库,但是比较精简,可能是我理解能力太差,实际操作的时候,好多还是需要查阅别的资料,希望官方能对小白做个完善,给出稍微详细的资料。
下面就是官方的关于串口的使用配置,
1 首先声明 UART_HandleTypeDef
2 初始化串口,包括了,串口波特率等等 NVIC 和DMA 传输,当然,NVIC 和DMA 是可选的。
3 配置使用。


  1. <font face="微软雅黑" size="3">The UART HAL driver can be used as follows:
  2. 1. Declare a UART_HandleTypeDef handle structure.
  3. 2. Initialize the UART low level resources by implementing the HAL_UART_MspInit()
  4. API:
  5. a. Enable the USARTx interface clock.
  6. b. UART pins configuration:
  7.  Enable the clock for the UART GPIOs.
  8.  Configure these UART pins as alternate function pull-up.
  9. c. NVIC configuration if you need to use interrupt process
  10. (HAL_UART_Transmit_IT() and HAL_UART_Receive_IT() APIs):
  11.  Configure the USARTx interrupt priority.
  12.  Enable the NVIC USART IRQ handle.
  13. d. DMA Configuration if you need to use DMA process
  14. (HAL_UART_Transmit_DMA() and HAL_UART_Receive_DMA() APIs):
  15.  Declare a DMA handle structure for the Tx/Rx stream.
  16.  Enable the DMAx interface clock.
  17.  Configure the declared DMA handle structure with the required Tx/Rx
  18. parameters.
  19.  Configure the DMA Tx/Rx Stream.
  20.  Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  21.  Configure the priority and enable the NVIC for the transfer complete
  22. interrupt on the DMA Tx/Rx Stream.
  23. 3. Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware flow control and
  24. Mode(Receiver/Transmitter) in the Init structure.
  25. 4. For the UART asynchronous mode, initialize the UART registers by calling the
  26. HAL_UART_Init() API.
  27. 5. For the UART Half duplex mode, initialize the UART registers by calling the
  28. HAL_HalfDuplex_Init() API.
  29. 6. For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
  30. 7. For the Multi-Processor mode, initialize the UART registers by calling the
  31. HAL_MultiProcessor_Init() API.</font>
复制代码

使用到的传输函数如下:

  1. <font face="微软雅黑" size="2">HAL_UART_Transmit()
  2.  HAL_UART_Receive()
  3.  HAL_UART_Transmit_IT()
  4.  HAL_UART_Receive_IT()
  5.  HAL_UART_Transmit_DMA()
  6.  HAL_UART_Receive_DMA()
  7.  HAL_UART_DMAPause()
  8.  HAL_UART_DMAResume()
  9.  HAL_UART_DMAStop()
  10.  HAL_UART_IRQHandler()
  11.  UART_WaitOnFlagUntilTimeout()
  12.  HAL_UART_TxCpltCallback()
  13.  HAL_UART_TxHalfCpltCallback()
  14.  HAL_UART_RxCpltCallback()
  15.  HAL_UART_RxHalfCpltCallback()
  16.  HAL_UART_ErrorCallback()</font>
复制代码
本文中使用的是中断传输,重写了 HAL_UART_TxCpltCallback() 和 HAL_UART_RxCpltCallback(),即是传输完成的时候调用这个函数,这个函数在程序初始化的时候调用即可。
HAL_UART_TxCpltCallback() 和 HAL_UART_RxCpltCallback(),这两个函数,分别是在串口收、发完成之后调用。


下面是两个返送函数,通过中断发送和普通的发送,通过中断发送,发送完成后调用
HAL_UART_TxCpltCallback() 进行相关的处理。

  1. HAL_UART_Transmit_IT(&huart3, "USART OK \n",strlen("USART3 OK \n"));
  2. HAL_UART_Transmit(&huart3, "USART3 OK \n",strlen("USART3 OK \n"), 10);
复制代码







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

0

主题

71

帖子

18

积分

新手上路

Rank: 1

积分
18
沙发
发表于 2016-7-7 08:49 PM | 只看该作者
感谢分享。
回复 支持 反对

使用道具 举报

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

本版积分规则

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