while循环 while(1) { if(ReceiveState==1) { ReceiveState=0; i=0; while(Rxcounter--) { USART_SendData(USART1,RX_buff[i]); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);//·¢ËÍ¿Õ±ê־λ } Rxcounter=0; } } } 串口中断 void USART1_IRQHandler(void) { u8 clear=clear; if( USART_GetITStatus(USART1,USART_IT_RXNE) != RESET ) { RX_buff[Rxcounter++]=USART_ReceiveData(USART1); } else if(USART_GetITStatus(USART1,USART_IT_IDLE)!=RESET) { clear=USART1->SR; clear=USART1->DR; ReceiveState=1; } } 配置 void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); } void USART_Configuration(void) { USART_InitTypeDef USART1_InitStructure; USART1_InitStructure.USART_BaudRate=9600; USART1_InitStructure.USART_WordLength=USART_WordLength_8b;//Ô¤·ÖƵ USART1_InitStructure.USART_StopBits=USART_StopBits_1; USART1_InitStructure.USART_Parity=USART_Parity_No; USART1_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART1_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; USART_Init(USART1,&USART1_InitStructure); //USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//·¢ËÍÖжϲ»ÐèÒª´ò¿ª USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//ʹÄܽÓÊÕÖÐ¶Ï USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//ʹÄÜ¿ÕÏÐÖÐ¶Ï // USART_ITConfig(USART1, USART_IT_ORE, ENABLE); USART_Cmd(USART1,ENABLE); USART_ClearFlag(USART1,USART_FLAG_TC); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } |
评分
查看全部评分
时钟忘记贴出来了,开启了时钟。
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
这个是时钟配置
void USART1_IRQHandler(void)
{
u8 clear=clear;
if( USART_GetITStatus(USART1,USART_IT_RXNE) != RESET )
{
RX_buff[Rxcounter++]=USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
else if(USART_GetITStatus(USART1,USART_IT_IDLE)!=RESET)
{
clear=USART1->SR;
clear=USART1->DR;
ReceiveState=1;
}
}
可以了,谢谢