最近做个项目需要用到两个串口,在调试过程中遇到问题,望广大好心的高手帮助
奇怪的现象:用到串口1时可以发送数据和接收数据(程序已用串口1验证过),修改为串口2后就只能发送数据不能接收数据!
代码:
int main(void)
{
vu16 i=500;
// gpio_initi();
NVIC_Configuration();
USART_Config();
/* start_gsm();
Delay(20000000);
USART_printf(USART2,"AT\r\n");
Delay(2000000);
USART_printf(USART2,"AT+CGPSPWR=1\r\n");
Delay(200000);
USART_printf(USART2,"AT+CGPSRST=1\r\n");
Delay(200000);
USART_printf(USART2,"AT+CGPSIPR=9600\r\n");*/
while(1)
{
i=500;
if(USART_GetFlagStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_SendData(USART2,USART_ReceiveData(USART2));
while(i--);
}
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级分组形式1,即抢占级占一位,优先级占3位
/****************UART2全局中断使能***********************/
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// NVIC_Configuration();
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
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;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
/* INIT USART1 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
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;
/* Configure the USART2 */
USART_Init(USART2, &USART_InitStructure);
/* INIT USART2 */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable the USART2 */
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_Cmd(USART2, ENABLE);
}
|