完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在usart.c上增加配置NVIC优先级中断控制器 NVIC_InitTypeDef Nvic_init; //misc.h NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置NVIC中断组 //3.配置NVIC中断控制器 在misc.h里 Nvic_init.NVIC_IRQChannel = EXTI1_IRQn; //= 配置在启动文件里 //中断通道 Nvic_init.NVIC_IRQChannelCmd = ENABLE; //使能 找到FunctionalState字眼 右键goto Nvic_init.NVIC_IRQChannelPreemptionPriority = 1; //因为只配置了一个中断 不考虑优先级 所以只有1个 Nvic_init.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NvicInitStructure); usart.c #include "stm32f10x.h" // Device header #include "usart.h" //#include "stdio.h" void Usart_Init(void) { //2. 配置GPIO的结构体 GPIO_InitTypeDef GpioInitStructure; //初始化GPIO结构体命名 USART_InitTypeDef UsartInitStructure;//初始化USART结构体命名 //加入 NVIC_InitTypeDef NvicInitStructure; //初始化USART结构体命名 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//配置NVIC中断组 //1. 配置时钟:GPIO口的时钟,引脚复用的时钟,串口的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //2.1 配置PA9 TX GpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出 GpioInitStructure.GPIO_Pin = GPIO_Pin_9; GpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GpioInitStructure); //2.2 配置PA10 RX GpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GpioInitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA,&GpioInitStructure); //3.配置串口结构体 UsartInitStructure.USART_BaudRate = 115200; //波特率 UsartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流 UsartInitStructure.USART_Mode = USART_Mode_Rx| USART_Mode_Tx; //模式 UsartInitStructure.USART_Parity = USART_Parity_No; //校验位 UsartInitStructure.USART_StopBits = USART_StopBits_1; //停止位 UsartInitStructure.USART_WordLength = USART_WordLength_8b; //字节长度 USART_Init(USART1, &UsartInitStructure); USART_ITConfig( USART1, USART_IT_RXNE, ENABLE ); //中断标志位 USART_Cmd(USART1, ENABLE);//打开串口 比配置GPIO多这一步 NvicInitStructure.NVIC_IRQChannel = USART1_IRQn; NvicInitStructure.NVIC_IRQChannelPreemptionPriority = 1; NvicInitStructure.NVIC_IRQChannelSubPriority = 1; NvicInitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NvicInitStructure); } //发送字符 void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data) { USART_SendData( USARTx, Data); while( USART_GetFlagStatus( USARTx, USART_FLAG_TXE) == RESET);//USART_GetFlagStatus是判断标志位 USART_FLAG_TXE 去usart.h FLAG找 } //发送字符串 void USARTSendString( USART_TypeDef* USARTx, char *str) { uint16_t i = 0; do{ USARTSendByte(USART1,*(str+i)); i++; }while(*(str+i) != ' |