串口配置完成可以發(fā)送但是無法接收
void?USART1_Init(void) { ????GPIO_InitTypeDef??GPIO_InitStructure={0}; ????USART_InitTypeDef?USART_InitStructure={0}; ????NVIC_InitTypeDef??NVIC_InitStructure={0}; ????/*?打開GPIO和USART部件的時鐘?*/ ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA?|?RCC_APB2Periph_AFIO,?ENABLE); ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,?ENABLE); ????/*?配置GPIO的模式和IO口?*/ ????GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;????????????//?串口輸出PA9(TX) ????GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;????//?IO口頻率 ????GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;??????//?復用推挽輸出 ????GPIO_Init(GPIOA,&GPIO_InitStructure);??????????????//?初始化 ????GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;???????????//?串口輸入PA10(RX) ????GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//?浮空輸入 ????GPIO_Init(GPIOA,&GPIO_InitStructure);??????????????//?初始化 ????/*?配置串口硬件參數?*/ ????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_HardwareFlowControl?=?USART_HardwareFlowControl_None; ????USART_InitStructure.USART_Mode?=?USART_Mode_Rx?|?USART_Mode_Tx; ????USART_Init(USART1,?&USART_InitStructure); ????/*?使能串口1中斷?*/ ????NVIC_InitStructure.NVIC_IRQChannel?=?USART1_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?1; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?1; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????USART_ITConfig(USART1,?USART_IT_RXNE,?ENABLE);????/*?使能串口空閑中斷?*/ ????USART_Cmd(USART1,?ENABLE);????????/*?使能串口?*/ }
void?USART1_IRQHandler(void) { ????if(USART_GetITStatus(USART1,?USART_IT_RXNE)?!=?RESET)?//中斷產生 ????{ uint8_t?RxData?=?USART_ReceiveData?(USART1); ?USART_ClearITPendingBit(USART1,USART_IT_RXNE); } }
熱門產品 :
CH339: 7端口多功能USB HUB控制器芯片