×
单片机 > 单片机程序设计 > 详情

STM32串口发送数据详解

发布时间:2020-08-27 发布时间:
|
串口的中断方式:

/**
* @brief Enables or disables the specified USART interrupts.
* @param USARTx: where x can be 1 or 2 to select the USART peripheral.
* @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
* This parameter can be one of the following values:
* @arg USART_IT_WU: Wake up interrupt. //唤醒中断
* @arg USART_IT_CM: Character match interrupt.//字符匹配中断
* @arg USART_IT_EOB: End of block interrupt. //块结束中断
* @arg USART_IT_RTO: Receive time out interrupt. //接收超时中断
* @arg USART_IT_CTS: CTS change interrupt. //CTS变化中断
* @arg USART_IT_LBD: LIN Break detection interrupt. //终止检测中断
* @arg USART_IT_TXE: Tansmit Data Register empty interrupt. //发送数据寄存器空中断
* @arg USART_IT_TC: Transmission complete interrupt. //发送完成中断
* @arg USART_IT_RXNE: Receive Data register not empty interrupt. //接收寄存器非空中断
* @arg USART_IT_IDLE: Idle line detection interrupt. //空闲线检测中断
* @arg USART_IT_PE: Parity Error interrupt. //奇偶校验错误中断
* @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error) //错误中断
* @param NewState: new state of the specified USARTx interrupts.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/

 
 
首先把数据手册的这部分内容贴出来
 
Character transmission procedure
1. Program the M bits in USARTx_CR1 to define the word length.
2. Select the desired baud rate using the USARTx_BRR register.
3. Program the number of stop bits in USARTx_CR2.
4. Enable the USART by writing the UE bit in USARTx_CR1 register to 1.
5. Select DMA enable (DMAT) in USARTx_CR3 if Multi buffer Communication is to take 
place. Configure the DMA register as explained in multibuffer communication.
6. Set the TE bit in USARTx_CR1 to send an idle frame as first transmission.
7. Write the data to send in the USARTx_TDR register (this clears the TXE bit). Repeat 
this for each data to be transmitted in case of single buffer. 
8. After writing the last data into the USARTx_TDR register, wait until TC=1. This 
indicates that the transmission of the last frame is complete. This is required for 
instance when the USART is disabled or enters the Halt mode to avoid corrupting the 
last transmission.
 
字符传输过程 
1。方案中USARTx_CR1的M位来定义字长。 
2。选择所需的波特率使用USARTx_BRR寄存器。 
3。计划在USARTx_CR2停止位的数目。 
4。通过写寄存器1 USARTx_CR1在UE位使能USART的。 
5。选择DMA使能(DMAT)在USARTx_CR3如果多缓冲器通信是采取 
地方。配置DMA寄存器在多缓冲器通信解释。 
6。设置在USARTx_CR1的TE位发送空闲帧作为第一次传输。 
7。写入要发送的数据在USARTx_TDR寄存器(这将清除TXE位)。重复 
这对每个数据进行的情况下单个缓冲器的传输。 
8。写在最后一个数据到USARTx_TDR寄存器后,等到TC=1。这 
表示最后帧的传输完成。这是必需的 
例如当USART被禁用或进入暂停模式,以避免破坏 
最后一次传输。
 
 
首先仔细看看这个发送的数据时序图。
发现,USART_TDR没有数据的时候,也就是空,TXE会被置位,当往USART_TDR写数据的时候,TXE低
列出TXE,TCflag变化的条件
TXE:只要USART_TDR没有数据就被置位
TCflag:只有TXline数据全部发送出去才会中断。
发送过程:往USART_TDR里面写数据->TXline往外发送。
也就是TCflag置位会晚于TXEflag一个字符的时间。
 
 
字符传输过程 
 
(转)

1、 使能串口发送TE,此时USART_DR为空,此时应查询TXE是否置1,TXE置1,TX脚先发送一个空闲帧,把F1帧写入USART_DR,TXE被清零。因为这时正在发送空闲帧,所以写入USART_DR的数据被放入TDR寄存器,还没有拷贝到移位寄存器。
2、 在空闲帧发送完后,TDR寄存器中的数据被拷贝到移位寄存器,此时应查询TXE是否置1,TXE置1,表示TDR已空,可以放入下一个数据。此时在TX脚上将会发送F1帧的数据,同时软件把F2帧的数据写入USART_DR,TXE被清零。
3、 在F1帧的停止位发送完后,因为TDR寄存器中的F2还没被拷入移位寄存器,所以此时TXE仍为0,TC不置1.此时应查询TXE是否置1,TXE置1,表示TDR已空,可以放入下一个数据。此时在TX脚上将会发送F2帧的数据,同时软件把F3帧的数据写入USART_DR,TXE被清零。
4、 在F2帧的停止位发送完后,因为TDR寄存器中的F3还没被拷入移位寄存器,所以此时TXE仍为0,TC不置1.此时应查询TXE是否置1,TXE置1,表示TDR已空,后面没有数据写入USART_DR,TXE保持高电平,此时在TX脚上将会发送F3帧的数据。
5、 在F3帧的停止位发送完后,因为此时TXE为1,所以TC标志会置1.如果TCIE为1,将会产生中断。

串口发送注意几点:
1、 如果正在发送数据,写数据进USART_DR将会把数据写到TDR缓冲寄存器,在本次发送完成后,再把TDR中的数据拷贝进移位寄存器。
2、 如果当前没有发送数据,写数据进USART_DR将会把数据直接放到移位寄存器,不经过TDR,TXE被清零,然后发送开始,TXE被硬件置1。
3、 只有当一帧数据的停止位发送完成并且TXE为1,TC才会被置1



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
qemu+chroot构建arm aarch64虚拟机