小弟在项目中调用库函数printf,测试usart1,3,4都没问题,但是在用串口2的时候,调用fputc函数的时候进入了default_handler这里出不来了,不知道啥原因,求高手指点下。
static void Uart_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
//使能串口2,PA,AFIO总线
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_AFIO,
ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/* A2 USART2_Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;//GPIO_Mode_IN_FLOATING;//
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* A3 USART2_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 =9600;// 57600;//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);
}
/*****************************************/
int fputc(int ch, FILE *f)
{
FlagStatus sta;
USART2->DR = (u8) ch;
sta=USART_GetFlagStatus(USART2, USART_FLAG_TXE);
while(sta == RESET)
{ }
return ch;
}
配置代码和其它串口一样,没问题的。调用fputc的时候,运行到这里:sta=USART_GetFlagStatus(USART1, USART_FLAG_TXE);就出不来了,但是单步进去这个函数是没问题的,单步后退出,return ch后,就死在default_handler这里,不知道啥原因了。难道不能调用串口2的? |