当前位置:网站首页>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.
边栏推荐
- Get the network input dimensions of the pretrained model
- 【考研结束第一天,过于空虚,想对自己进行总结一下】
- vscode笔记
- 盒子模型小练习
- DevExpress中针对指定列进行百分比转换
- Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
- The NDK compiler so libraries
- typescript66-分析partial的实现
- Tips for formatting code indentation
- Redis
猜你喜欢
随机推荐
八大排序之快速排序
Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
typescript66-分析partial的实现
【JVM调优】Xms和Xmx为什么要保持一致
如何将.asd恢复为Word文档
邮件管理 过滤邮件
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
AH8669-AC380/VAC220V转降5V12V24V500MA内电源芯片IC方案
D41_buffer pool
文件内音频的时长统计并生成csv文件
自媒体人一般会从哪里找素材呢?
农场游戏果园系统+牧场养殖系统+广告联盟模式流量主游戏小程序APP V1
NACOS配置中心设置配置文件
Late night drinking, 50 classic SQL questions, really fragrant~
Nacos集群的搭建过程详解
h5页面回退到微信小程序并携带参数
numpy.random usage documentation
技术分析模式(九)三重顶部和底部
亚马逊美国站:马术头盔CPC认证标准要求
【FAQ】What is Canon CCAPI









