当前位置:网站首页>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.
边栏推荐
猜你喜欢
LaTeX 图片加标题 文本分栏自动换行
typescript60-泛型工具类型(readonly)
Quick Start to Drools Rule Engine (1)
typescript68-索引查询类型(查询多个)
Nacos集群搭建
VSCode编写OpenCV
LaTeX使用frame制作PPT图片没有标号
Get the network input dimensions of the pretrained model
Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
花花省V5淘宝客APP源码无加密社交电商自营商城系统带抖音接口
随机推荐
铠侠携手Aerospike提升数据库应用性能
lingo入门——河北省第三届研究生建模竞赛B题
Nacos集群搭建
技术分析模式(九)三重顶部和底部
Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
DevExpress中针对指定列进行百分比转换
淘宝宝贝页面制作
scikit-image图像处理笔记
Detailed explanation of the construction process of Nacos cluster
Redis
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
如何将.asd恢复为Word文档
typescript62-泛型工具类型(record)
Redis进阶
VSCode编写OpenCV
Some basic method records of commonly used languages in LeetCode
typescript66-分析partial的实现
AH8669-AC380/VAC220V转降5V12V24V500MA内电源芯片IC方案
scikit-image image processing notes
ndk编译so库