1.问题描述:
1.不停的发送数据(实际需要点击发送按钮就发送一次)。
2.每次打开串口只能发送一次,再次发送需要重新打开串口。
如图所示:
图 1
2.问题分析
1.不必要的循环。
2.USART_GetFlagStatus和USART_GetITStatus产生误用,理解不够。
3.错误代码
void USART1_SendByte(uint8_t byte){
while(USART_GetITStatus(USART1, USART_IT_TC) != SET) {
USART_SendData(USART1, byte);
}
上面的代码有多处错误:
1.while循环应该是一个空循环。
2.不应该使用USART_GetITStatus,而应该使用USART_GetFlagStatus。
3.USART_IT_TC替换成USART_FLAG_TC。
修改代码如下:
void USART1_SendByte(uint8_t byte){
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
USART_SendData(USART1, byte);
}
4.USART_GetFlagStatus和USART_GetITStatus
1.USART_GETFlagStatus()函数(可以在固件库查找到该函数)
图 2
图 3
如图所示,该函数用来检查USART的标志为设置与否,在我的程序中应该是利用标志位USART_FLAG_TC,USART_FLAG_TC标志位用来检测发送是否完成,即SET和RESET。当发送完成时为SET状态,便跳出while,执行下面的语句,在我的程序中就开始发送数据。在我的错误的程序中,我点击发送后,由于程序没有正常发送,便执行了while循环中的语句,而恰好我的发送数据的函数在该循环中,便不停的发送数据。
2.USART_GetITStatus()函数
图 4
图 5
如图所示,USART_GetITStatus()函数用于检查指定的 USART 中断发生与否,标志位USART_IT_RXNE用来接收中断。
5.所有代码
#include "usart1.h"
volatile uint8_t Flag = FALSE;
volatile uint16_t Rx232buffer;
//初始化串口相关代码
void Usart1_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructrue;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 19200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ClockInitStructrue.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructrue.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructrue.USART_CPHA = USART_CPHA_1Edge;
USART_ClockInitStructrue.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructrue);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//错误代码=====================================================
//void USART1_SendByte(uint8_t byte){
// while(USART_GetITStatus(USART1, USART_IT_TC) != SET) {
// USART_SendData(USART1, byte);
// }
//
//
//}
//=============================================================
//发送数据的函数
void USART1_SendByte(uint8_t byte){
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
USART_SendData(USART1, byte);
}
void USART1_SendStr(uint8_t *str){//该函数没有使用
while(0 != *str){
USART1_SendByte(*str);
}
}
void USART1_Tx_Puts(void){//在中断发生并接收数据后,调用USART1_SendByte发送数据
if(Flag){
USART1_SendByte(Rx232buffer);//发送字符
USART1_SendByte(0x0D);//发送换行符
USART1_SendByte(0x0A);//发送换行符
Flag = FALSE;
}
}
void USART1_IRQHandler(void){
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET) //接收中断
{
Rx232buffer= USART_ReceiveData(USART1); //通过外设USART1接收数据
Flag=TRUE; //接收到数据,接收标识符有效
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除USART1的中断待处理位
}
}
int main(void)
{
Usart1_Init();
//主循环
while(1)
{
USART1_Tx_Puts();
}
}