27,374
社区成员
发帖
与我相关
我的任务
分享
void USART1_Config(){
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //波特率设置为:115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据长度
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
void GPIO_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO_InitStructure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
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;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//开漏输出
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//开漏输出
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Config(void){
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void){
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
NVIC_Config();
USART1_Config();
GPIO_Config();
USART_ClearFlag(USART1, USART_FLAG_TC);
while(1){
// usart_data = USART_ReceiveData(USART1); <- 为什么只有执行了这句话才进入中断函数
if(usart_data_flag){
printf("%s,%c\n","Hello, I'm YinJianFeng~ ", usart_data);
usart_data_flag = 0;
}
}
}
void USART1_IRQHandler(void){
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET){
usart_data = USART_ReceiveData(USART1);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
usart_data_flag = 1;
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
usart_data_flag = 1;
}
void USART1_IRQHandler(void){
ITStatus ErrorITStatus = USART_GetITStatus(USART1, USART_IT_RXNE);
if(ErrorITStatus == SET){
usart_data = USART_ReceiveData(USART1);
usart_data_flag = 1;
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
// usart_data_flag = 1;
}
if(USART_GetITStatus(USART1, USART_IT_RXNE)==SET){...
代码修改为:
ITStatus ErrorITStatus = USART_GetITStatus(USART1, USART_IT_RXNE);
if(ErrorITStatus == SET){...
居然就可以了,我不明白,不知道如何考证。。。