当前位置:网站首页>Using printf function in STM32
Using printf function in STM32
2022-08-05 06:56:00 【*Three bars of black radish*】
一、目的
利用printfThe function causes the output to be printed to the serial port.
二、工作原理
我们在CWhy can it be called in the languageprintfThe function prints data to the console,那是因为printf函数又调用了fputc()函数.So can we sayprintfThe reason why the function can print data to the console is almost allfput()The credit of the function?答案是显而易见的.因此,Here we just need to rewritefput函数即可.
三、代码展示
1、重写fput()函数
在这里我们需要注意的是,This function is self-invoking during actual code execution,We don't need to call it deliberately.
/****************************************************************************** * 函数功能 Equivalent to serial port sending data * 参数 * 返回值 * 注意 该函数是stdio.hThe default function in the file 当我们使用printffunction will call itself * 这里的printf函数跟C语言的printf用法一样 ******************************************************************************/
int fputc(int ch,FILE *p)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
2、GPIO初始化
/**************************************************************** * 函数功能 printf的初始化 * 参数 无 * 返回值 无 ****************************************************************/
void printf_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 打开端口时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* 配置GPIO的模式和IO口 */
//选择TX 串口输出PA9
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);
/* 初始化串口输入IO */
//选择RX 串口输入PA10
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
//模拟输入
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* USART串口初始化 */
//波特率设置为9600
USART_InitStructure.USART_BaudRate=9600;
//数据长8位
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
//1位停止位
USART_InitStructure.USART_StopBits=USART_StopBits_1;
//无效验
USART_InitStructure.USART_Parity=USART_Parity_No;
//Disable hardware flow
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
//开启发送和接受模式
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
/* 使能USART1 */
USART_Cmd(USART1, ENABLE);
//使能或者失能指定的USART中断 接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//清除USARTx的待处理标志位
USART_ClearFlag(USART1,USART_FLAG_TC);
}
3、一个简单的hello输出
int main()
{
printf_init();
while(1)
{
printf("hello!");
}
}
四、keil中的配置
In fact, using the above code directly cannot accomplish our original design purpose,Just so it doesn't output anything to the serial port,我们还需要在keilSmall and medium configuration.样例如下图:
五、总结
小编个人认为,在这个项目中,实际上的printfThe function is a serial output function,It's just that it is more convenient than we used to directly call the serial port send function、更加实用,无非是在GPIOThe definition and initialization are a little different.因此,我们完全可以将printfThe function is regarded as a serial port sending function.
边栏推荐
猜你喜欢
随机推荐
360度反馈调查表中的问题示范
【Go】IM系统Centrifugo
typescript64-映射类型
LaTeX image captioning text column automatic line wrapping
【2022 DSCTF决赛wp】
17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
typescript60-泛型工具类型(readonly)
MySQL的主从模式搭建
vscode notes
UDP广播
Detailed explanation of the construction process of Nacos cluster
LaTeX笔记
Some basic method records of commonly used languages in LeetCode
字体样式及其分类
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
2022杭电多校六 1007-Shinobu loves trip(同余方程)
h5页面回退到微信小程序并携带参数
Source code analysis of Nacos configuration service (full)
白鹭egret添加新页面教程,如何添加新页面
Nacos集群的搭建过程详解









