串口1自收发—中断方式
来源:互联网 发布:淘宝确认收货后换货 编辑:程序博客网 时间:2024/05/22 03:35
/******************************************************************************//* STM32F10x Peripherals Interrupt Handlers *//******************************************************************************//** * @brief This function handles USART1 global interrupt request. * @param None * @retval None */void USART1_IRQHandler(void){uint8_t rxBuffer; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { /* Read one byte from the receive data register */ //RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1); rxBuffer=USART_ReceiveData(USART1); //读下来立即写出去 USART_SendData(USART1, rxBuffer); /*发送一个字符函数*/ //if(RxCounter1 == NbrOfDataToRead1) //{ /* Disable the USART1 Receive interrupt */ //USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //} } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { /* Write one byte to the transmit data register */ USART_SendData(USART1, TxBuffer1[TxCounter1++]); if(TxCounter1 == NbrOfDataToTransfer1) { /* Disable the USART1 Transmit interrupt */ USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } }}//////////////////////////////////////int main(void){ /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*//* USART1 and USART2 configuration ------------------------------------------------------*/ /* USART1 and USART2 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为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; /*发送和接收*/ /*配置串口1 */ USART_Init(USART1, &USART_InitStructure); /*配置串口2*/ //USART_Init(USART2, &USART_InitStructure); /*使能串口1的发送和接收中断*/ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TXE, ENABLE); /*使能串口2的发送和接收中断*/ //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* 使能串口1 */ USART_Cmd(USART1, ENABLE); /* 使能串口2 */ //USART_Cmd(USART2, ENABLE); /* Wait until end of transmission from USART1 to USART2 */ //while(RxCounter2 < RxBufferSize2) //{ //} /* Wait until end of transmission from USART2 to USART1 */ //while(RxCounter1 < RxBufferSize1) //{ //} /* Check the received data with the send ones */ //TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1); /* TransferStatus1 = PASSED, if the data transmitted from USART2 and received by USART1 are the same */ /* TransferStatus1 = FAILED, if the data transmitted from USART2 and received by USART1 are different */ //TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2); /* TransferStatus2 = PASSED, if the data transmitted from USART1 and received by USART2 are the same */ /* TransferStatus2 = FAILED, if the data transmitted from USART1 and received by USART2 are different */ while (1) { /* if(TransferStatus1 == PASSED) { GPIO_ResetBits(GPIO_LED,DS1_PIN);//点亮DS1,串口1接收的数据与串口2发送的数据相同 } else if(TransferStatus1 == FAILED) { GPIO_ResetBits(GPIO_LED,DS2_PIN);//点亮DS2,串口1接收的数据与串口2发送的数据不相同 } if(TransferStatus2 == PASSED) { GPIO_ResetBits(GPIO_LED,DS3_PIN);//点亮DS3,串口2接收的数据与串口1发送的数据相同 } else if(TransferStatus2 == FAILED) { GPIO_ResetBits(GPIO_LED,DS3_PIN);//点亮DS3,串口2接收的数据与串口1发送的数据不相同 } */ }}