STM32串口打印printf

来源:互联网 发布:asp和php的区别 编辑:程序博客网 时间:2024/05/16 09:24

配置好USART1之后,如果想使用printf函数进行打印,则需要加入如下代码:

/*******************************************************************************

函数名:fputc
输  入:
输  出:
功能说明:
重定义putc函数,这样可以使用printf函数从串口1打印输出
*/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);


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


return ch;
}


/*******************************************************************************
函数名:fputc
输  入:
输  出:
功能说明:
重定义getc函数,这样可以使用scanff函数从串口1输入数据
*/
int fgetc(FILE *f)
{
/* 等待串口1输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}


return (int)USART_ReceiveData(USART1);

}


调用方法:

              (1)、打印字符串:printf("hello world!\n");

              (2)、打印数字或变量:printf("a = %d\n", a);

              (3)、对于scanf函数,我们使用时直接用串口中断接受就可以,这里不再说明。


实际调试图片:

                         

0 0
原创粉丝点击