void USART1_IRQHandler(void)//串口中断 { if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { /* Write one byte to the transmit data register */ if(TxCounter1 < NbrOfDataToTransfer1) USART_SendData(USART1, TxBuffer1[TxCounter1++]);
/* Clear the USART1 transmit interrupt */ USART_ClearITPendingBit(USART1, USART_IT_TXE);
if(TxCounter1 == NbrOfDataToTransfer1) { /* Disable the USART1 Transmit interrupt */ USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } } else { /* Read one byte from the receive data register */ RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);
/* Clear the USART1 Receive interrupt */ USART_ClearITPendingBit(USART1, USART_IT_RXNE);
if(RxCounter1 == NbrOfDataToRead1) { /* Disable the USART1 Receive interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); } } }
//**********串口基本配置***********************************/ USART_InitStructure.USART_BaudRate = 9600; 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_InitStructure.USART_Clock = USART_Clock_Disable; USART_InitStructure.USART_CPOL = USART_CPOL_Low; USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; USART_InitStructure.USART_LastBit = USART_LastBit_Disable; /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /* Configure USART2 */ USART_Init(USART2, &USART_InitStructure); /* Enable USART1 Receive and Transmit interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable USART2 Receive and Transmit interrupt */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enable the USART1 */ USART_Cmd(USART1, ENABLE); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE);
//**********中断配置***********************************/ /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
|