|
主要内容:使用STM32 L475 的 串口 三打印信息。和用户交互。
L476 的UART3 连接的是PC4和PC5 ,分别对应 TX和RX:
PC4 ----- TX
PC5 ----- RX
使用USB ---TTL 模块和PC4、5 连接即可。使用cubeMX 配置相应的串口代码,既可以与外部通信。
关于CubeMX 生成UART代码的过程这里就不详细介绍了,可以参考以前的帖子。
串口代码配置如下:
- <font face="微软雅黑" size="3">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;
- huart3.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
- huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- HAL_UART_Init(&huart3);
- }</font>
复制代码 本文中采用的是9600的波特率,开启了串口的中断传输的功能。
CubeMX 的固件库,开始使用有点不太习惯了,不过开发上CubeMX 确实上手简单了,相信以后会用的越来越多的。
- <font face="微软雅黑" size="4"> uint32_t BaudRate
- uint32_t WordLength
- uint32_t StopBits
- uint32_t Parity
- uint32_t Mode
- uint32_t HwFlowCtl
- uint32_t OverSampling
- uint32_t OneBitSampling</font>
复制代码 官方了也给出了如何使用HAL固件库,但是比较精简,可能是我理解能力太差,实际操作的时候,好多还是需要查阅别的资料,希望官方能对小白做个完善,给出稍微详细的资料。
下面就是官方的关于串口的使用配置,
1 首先声明 UART_HandleTypeDef
2 初始化串口,包括了,串口波特率等等 NVIC 和DMA 传输,当然,NVIC 和DMA 是可选的。
3 配置使用。
- <font face="微软雅黑" size="3">The UART HAL driver can be used as follows:
- 1. Declare a UART_HandleTypeDef handle structure.
- 2. Initialize the UART low level resources by implementing the HAL_UART_MspInit()
- API:
- a. Enable the USARTx interface clock.
- b. UART pins configuration:
- Enable the clock for the UART GPIOs.
- Configure these UART pins as alternate function pull-up.
- c. NVIC configuration if you need to use interrupt process
- (HAL_UART_Transmit_IT() and HAL_UART_Receive_IT() APIs):
- Configure the USARTx interrupt priority.
- Enable the NVIC USART IRQ handle.
- d. DMA Configuration if you need to use DMA process
- (HAL_UART_Transmit_DMA() and HAL_UART_Receive_DMA() APIs):
- Declare a DMA handle structure for the Tx/Rx stream.
- Enable the DMAx interface clock.
- Configure the declared DMA handle structure with the required Tx/Rx
- parameters.
- Configure the DMA Tx/Rx Stream.
- Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
- Configure the priority and enable the NVIC for the transfer complete
- interrupt on the DMA Tx/Rx Stream.
- 3. Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware flow control and
- Mode(Receiver/Transmitter) in the Init structure.
- 4. For the UART asynchronous mode, initialize the UART registers by calling the
- HAL_UART_Init() API.
- 5. For the UART Half duplex mode, initialize the UART registers by calling the
- HAL_HalfDuplex_Init() API.
- 6. For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
- 7. For the Multi-Processor mode, initialize the UART registers by calling the
- HAL_MultiProcessor_Init() API.</font>
复制代码
使用到的传输函数如下:
- <font face="微软雅黑" size="2">HAL_UART_Transmit()
- HAL_UART_Receive()
- HAL_UART_Transmit_IT()
- HAL_UART_Receive_IT()
- HAL_UART_Transmit_DMA()
- HAL_UART_Receive_DMA()
- HAL_UART_DMAPause()
- HAL_UART_DMAResume()
- HAL_UART_DMAStop()
- HAL_UART_IRQHandler()
- UART_WaitOnFlagUntilTimeout()
- HAL_UART_TxCpltCallback()
- HAL_UART_TxHalfCpltCallback()
- HAL_UART_RxCpltCallback()
- HAL_UART_RxHalfCpltCallback()
- HAL_UART_ErrorCallback()</font>
复制代码 本文中使用的是中断传输,重写了 HAL_UART_TxCpltCallback() 和 HAL_UART_RxCpltCallback(),即是传输完成的时候调用这个函数,这个函数在程序初始化的时候调用即可。
HAL_UART_TxCpltCallback() 和 HAL_UART_RxCpltCallback(),这两个函数,分别是在串口收、发完成之后调用。
下面是两个返送函数,通过中断发送和普通的发送,通过中断发送,发送完成后调用 HAL_UART_TxCpltCallback() 进行相关的处理。
- HAL_UART_Transmit_IT(&huart3, "USART OK \n",strlen("USART3 OK \n"));
- HAL_UART_Transmit(&huart3, "USART3 OK \n",strlen("USART3 OK \n"), 10);
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|