当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
txt文件英语单词词频统计
(JLK105D)中山爆款LED恒流电源芯片方案
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
lingo入门——河北省第三届研究生建模竞赛B题
Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
LaTeX Notes
The cocos interview answers you are looking for are all here!
NB-IOT智能云家具项目系列实站
D45_Camera assembly Camera
在小程序中关于js数字精度丢失的解决办法
获取预训练模型的网络输入尺寸
自媒体人一般会从哪里找素材呢?
#Sealos#使用工具部署kubernetesV1.24.0
Some basic method records of commonly used languages in LeetCode
LabVIEW中如何实现任意形状的不规则按键
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
typescript61-泛型工具类型(pick)
文件内音频的时长统计并生成csv文件
D41_buffer pool
typescript64-映射类型









