- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef VECT_TAB_RAM
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /*UART2*/
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /*timer2*/
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- //使能串口1,PA,AFIO总线
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO ,ENABLE );
- RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 ,ENABLE);
- /* A9 USART1_Tx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* A10 USART1_Rx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX
- 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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- // USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- // USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- // USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- //
- // USART_ClockInit(USART2, &USART_ClockInitStructure);
- USART_Init(USART2, &USART_InitStructure);
- /* Enable the USARTx */
- USART_Cmd(USART2, ENABLE);
- //串口1使用接收中断
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- }
- void USART2_IRQHandler(void)
- {
- u16 i;
- if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET)
- {
- i = USART_ReceiveData(USART2);
- USART_SendData(USART2,i);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
- {
- }
- }
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- USART_ClearITPendingBit(USART2, USART_IT_RXNE);
- }
- //溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题
- if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
- {
- USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR
- USART_ReceiveData(USART2); //读DR
- }
- }
|