1 / * ********* a serial initialization function ********* * / 2 // bound: baud . 3 void My_USART1_Init (U32 bound) . 4 { . 5 // definition of the structure variables 6 GPIO_InitTypeDef GPIO_InitStructure; . 7 USART_InitTypeDef USART_InitStructure; . 8 NVIC_InitTypeDef NVIC_InitStructure; . 9 RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, the eNABLE); // enable clock GPIOA 10 RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, the eNABLE); // enable USART1 clock (serial 1 and 6 are only hung in on APB2 and) . 11 // the corresponding pin multiplexed and mapped 12 GPIO_PinAFConfig (with GPIOA, GPIO_PinSource9, GPIO_AF_USART1); 13 is GPIO_PinAFConfig (with GPIOA, GPIO_PinSource10, GPIO_AF_USART1); 14 // the GPIO port mode 15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // GPIOA9 with GPIOA10 16 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // complex with function . 17 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // speed 50MHz 18 is GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push-pull output multiplexing . 19 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // pull 20GPIO_Init (with GPIOA, & GPIO_InitStructure); // initialize PA9, PA10 21 is // port initialization parameters 22 is USART_InitStructure.USART_BaudRate = bound; // generally set to 9600; 23 is USART_InitStructure.USART_WordLength = USART_WordLength_8b; // word length is 8 bits 24 USART_InitStructure. = USART_StopBits_1 USART_StopBits; // a stop bit 25 USART_InitStructure.USART_Parity = USART_Parity_No; // no parity bit 26 is USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // no hardware flow control 27= USART_Mode_Rx USART_InitStructure.USART_Mode | USART_Mode_Tx; // transceiver 28 USART_Init (the USART1, & USART_InitStructure); // initialize the serial port. 1 29 30 USART_Cmd (the USART1, the ENABLE); // enable serial port. 1 31 is USART_ClearFlag (the USART1, USART_FLAG_TC); // Clear completion of data transmission identifier 32 ; USART_ITConfig (the USART1, USART_IT_RXNE, the eNABLE) // turn receive interrupt is generated (typically enable communication between PC and this can only line) when data in the shift register accepts 33 // enable interrupts if necessary initialization NVIC 34 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // the USART1 interrupt configuration 35 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = . 3 ; // preemption priority. 3 36 NVIC_InitStructure.NVIC_IRQChannelSubPriority = . 3 ; // response priority. 3 37 [ NVIC_InitStructure.NVIC_IRQChannelCmd = the ENABLE; // the IRQ enable passage 38 is NVIC_Init (& NVIC_InitStructure); // the specified VIC parameter initialization register, 39 } 40 41 is / * ********* interrupt handler ********* * / 42 is void USART1_IRQHandler ( void ) // port 1 interrupt service routine 43 { 44 is IF (! USART_GetITStatus (USARTy, USART_IT_RXNE) = the RESET) // if the data register 45 { 46 / * read a byte from the receive register * / 47 RxBuffer1 [RxCounter1 ++] = USART_ReceiveData (the USART1); 48 } 49 IF (! USART_GetITStatus (USARTy, USART_IT_TXE) = the RESET) 50 { 51 is USART_SendData (USARTy, TxBuffer1 [TxCounter1 ++ ]); 52 is } 53 is }
The main function:
. 1 int main ( void ) 2 { . 3 NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2); // set the system interrupt priority packets 2 . 4 My_USART1_Init (); . 5 the While ( . 1 ); . 6 }