下面是我代码的一部分,实现的功能很简单,就是用串口小助手给单片机发一个数据,单片机不断查询接收,一旦接收马上返回原数据给电脑:
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Private functions ---------------------------------------------------------*/
void rcc_cfg()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
}
void gpio_cfg()
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //configure PA9 as TX and PA10 as RX
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);
}
void usart_cfg()
{
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* Add your application code here
*/
uint16_t data;
rcc_cfg();
gpio_cfg();
usart_cfg();
/* Infinite loop */
while (1)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET); //wait until USART_FLAG_RXNE=RESET
data=USART_ReceiveData(USART1);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); // wait until USART_FLAG_TXE=RESET
USART_SendData(USART1,data);
}
}
我的问题是,按我的理解,单片机每次应该只能接受最多两字节的数据吧,因为我定义的数据缓存为 uint16_t data,可是,我在串口调试工具里一次性发送连续的字符串,单片机也返回连续的字符串,是不是挺诡异?比如,我发送hello,马上就能收到hello。莫非,电脑给串口发送字符串是单个单个发送出去而不是一次性连续发送?
其实知道这个问题比较水,但想了很久都没明白,求前辈们解释解释! |