本帖最后由 Eric@Wei 于 2017-12-29 11:00 编辑
USART_InitPara USART_InitStructure;
USART_DeInit( USART1 );
USART_InitStructure.USART_BRR = 115200;
USART_InitStructure.USART_WL = USART_WL_8B;
USART_InitStructure.USART_STBits = USART_STBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_RESET;
USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
GD_EVAL_COMInit(&USART_InitStructure);
/* Output a message on Hyperterminal using printf function */
printf("0x01");
void GD_EVAL_COMInit(USART_InitPara * USART_InitStruct)
{
GPIO_InitPara GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHBPeriphClock_Enable(EVAL_COM1_TX_GPIO_CLK | EVAL_COM1_RX_GPIO_CLK, ENABLE);
#if defined(GD32F130_150)
/* Enable USART clock */
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE);
#elif defined(GD32F170_190)
/* Enable USART clock */
RCC_APB1PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE);
#else
#error "Please define GD32F130_150 or GD32F170_190"
#endif
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(EVAL_COM1_TX_GPIO_PORT, GPIO_PINSOURCE9,GPIO_AF_1);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(EVAL_COM1_RX_GPIO_PORT, GPIO_PINSOURCE10,GPIO_AF_1);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_PIN_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_10MHZ;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_PIN_10;// ; EVAL_COM1_RX_PIN
GPIO_Init(EVAL_COM1_RX_GPIO_PORT, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, USART_InitStruct);
/* Enable USART */
USART_Enable(USART1 , ENABLE);
}
#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__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_DataSend(USART1, (uint8_t) ch );
/* Loop until transmit data register is empty */
while (USART_GetBitState(USART1 , USART_FLAG_TBE) == RESET)
{
}
return ch;
}
|