首先上我的代码:
GPIO:
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure USARTx_Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USARTx_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); }
void NVIC_Configuration(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=15; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); }
中断响应程序
void USART1_IRQHandler(void) { while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET) {} USART_SendData(USART1,USART_ReceiveData(USART1)); USART_ClearFlag(USART1,USART_FLAG_RXNE); }
RCC
void RCC_Configuration(void) { /* Enable GPIOx clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Enable USARTx clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); }
int main(void) { #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); 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_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); while (1) { printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r"); Delay(100000); } }
超级终端配置如下,收不到任何信息,求大神帮忙啊,搞了几天了:
----------------------------------------------------------------------------------------------------------------------
问题出现在两个地方:pringtf函数和线的连接
使用printf函数要重新定义fputc函数,printf会回调这个函数(放在main.c中就可以),注意这个函数要设置UART端口
fputc(int ch, FILE *f) { /* Write a character to the USART */ USART_SendData(USART1, (u8) ch); /* Loop until the end of transmission */ while(!(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == SET)) { } return ch; }
第二个问题是,TX和RX接反了,电脑的RX要接板子的TX,我接反了,真是惭愧,判断电脑上RX和TX的方法:RX的点压比TX低,usb转串口有四根线,电压关系:GND