使用printf从串口打印输出
- #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_RCC_Configuration
- * Description : open USART clock for GPIOA.
- * There is no need to open peripheral clock, if it is default
- * for some GPIO.If not, it is need to open.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_RCC_Configuration(void)
- {
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // start peripheral clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- }
- /*******************************************************************************
- * Function Name : USART_GPIO_Configuration
- * Description : initialize USART2 GPIO mode
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- /*******************************************************************************
- * Function Name : USART_Configuration
- * Description : initialize USART2
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStruct;
- USART_RCC_Configuration();
- USART_InitStruct.USART_BaudRate = 115200;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_Init(USART2, &USART_InitStruct);
- //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// enable receive interrupt
- USART_Cmd(USART2, ENABLE); // enable USART1
- USART_GPIO_Configuration();
- }
- /*******************************************************************************
- * Function Name : USART_Configuration
- * Description : initialize USART1 printf
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- PUTCHAR_PROTOTYPE
- {
- /* Place your implemetation of fputc here*/
- /* e.g. wirite a character to the USART */
- USART_SendData(USART2, (uint8_t)ch);
- /* Loop until the end of transmission */
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
- ;
- return ch;
- }
简单使用为
- /*******************************************************************************
- * Function Name : USART_Configuration
- * Description : initialize USART1
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- uint8_t USART_GetChar(void)
- {
- uint8_t ch=0;
- while(!(USART2->SR & USART_FLAG_RXNE))
- ;
- ch = USART_ReceiveData(USART2);
- return ch;
- }
- /*******************************************************************************
- * Function Name : USART_SendChar
- * Description : send char by USART2
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_SendChar(uint8_t data)
- {
- /* Loop until the end of transmission */
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
- ;
- USART_SendData(USART2, data);
- }