/*
调试STM32串口过程中发现一个奇怪的问题,初始化串口1口,使能串口发送完成中断后,立刻就进入了发送完成中断。
仔细的查阅了STM32手册中的串口部分的介绍:
以下是字符发送的配置过程,注意第6点,在设置USART_CR1中的TE位时,会发送一个空闲帧作为第一次数据发送,所以即便你执行了USART_ClearFlag(USART1, USART_FLAG_TC); (这个函数肯定在空闲帧数据发送完成前执行),所以当空闲帧发送完后,就进入发送完成中断。
配置步骤:
1. 通过在USART_CR1寄存器上置位UE位来激活USART
2. 编程USART_CR1的M位来定义字长。
3. 在USART_CR2中编程停止位的位数。
4. 如果采用多缓冲器通信,配置USART_CR3中的DMA使能位(DMAT)。按多缓冲器通信中
的描述配置DMA寄存器。
5. 利用USART_BRR寄存器选择要求的波特率。
6. 设置USART_CR1中的TE位,发送一个空闲帧作为第一次数据发送。
7. 把要发送的数据写进USART_DR寄存器(此动作清除TXE位)。在只有一个缓冲器的情况
下,对每个待发送的数据重复步骤7。
8. 在USART_DR寄存器中写入最后一个数据字后,要等待TC=1,它表示最后一个数据帧的
传输结束。当需要关闭USART或需要进入停机模式之前,需要确认传输结束,避免破坏
最后一次传输。*/
<div class="line number19 index18 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;"></code></div><div class="line number21 index20 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//解决的办法:</code></div><div class="line number22 index21 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//方法一</code></div><div class="line number23 index22 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//在执行</code></div><div class="line number24 index23 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">USART_ITConfig(USART1, USART_IT_TC, ENABLE); </code></div><div class="line number25 index24 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//之前,先延时一段时间,基本上比一个字符发送的时间长一点就可以了,然后再执行</code></div><div class="line number26 index25 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">USART_ClearFlag(USART1, USART_FLAG_TC);</code></div><div class="line number27 index26 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp spaces" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;"> </code> </div><div class="line number28 index27 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//方法二:</code></div><div class="line number29 index28 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//在执行</code></div><div class="line number30 index29 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">USART_ITConfig(USART1, USART_IT_TC, ENABLE);</code></div><div class="line number31 index30 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp keyword bold" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">while</code> <code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)</code></div><div class="line number32 index31 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">{</code></div><div class="line number33 index32 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp spaces" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;"> </code> <code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">; </code> <code class="cpp comments" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">//等待空闲帧发送完成后 再清零发送标志</code></div><div class="line number34 index33 alt1" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">}</code></div><div class="line number35 index34 alt2" style="box-sizing: border-box; outline: 0px; color: rgb(79, 79, 79); font-family: Tahoma, Helvetica, Arial, STHeiti; font-size: 14px;"><code class="cpp plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; word-break: break-word;">USART_ClearFlag(USART1,USART_FLAG_TC);</code></div>
|
|