关于 USB UART1的疑惑

2019-07-20 18:42发布

现在我学习FT232 USB UART,我能按照原子哥的stm32f4串口的程序来写吗?那个程序是USART,适用FT232的UART吗?
我已经写了一个了,用串口调试助手试过了,没有接收信号,就是不知道是什么原因。是不是USART跟UART的程序不同?

#include "usart.h"
#include "stm32f4xx.h"
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configure Open_USART
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void USART_Configuration(void)
{                                                                                                
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);


        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);


  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

  /*
  *  Open_USART_TX -> PA2 , Open_USART_RX -PA3
  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

/*
                    USARTx configured as follow:
         - BaudRate = 115200 baud  
                 - Word Length = 8 Bits
         - One Stop Bit
         - No parity
         - Hardware flow control disabled (RTS and CTS signals)
         - Receive and transmit   
*/

  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(USART2, &USART_InitStructure);
  /* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
     Open_USART transmit data register is empty */
  USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);

  USART_Cmd(USART2, ENABLE);

}

void USART_NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  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);
}

/* Use no semihosting */
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{  
        int handle;
};
FILE __stdout;

_sys_exit(int x)
{
        x = x;
}
#endif

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART2, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
  {}

  return ch;
}


主函数为
#include "stm32f4xx.h"
#include "usart.h"

int main(void)
{
        uint32_t i;
        USART_Configuration();
        USART_NVIC_Config();
        i=0x1ffffff;
        while (1)
        {
                printf(" Welcome to WaveShare STM32F4 series MCU Board Open407Z ");
                while(i--);
                i=0x1ffffff;
        }
}


将程序下载到STM32F407的开发板中,然后连接TX-A2,RX-A3,串口调试助手没有接受。。。
求解!!!!
十分感谢!!!!




友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。