当前位置:网站首页>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

图像处理、分析与机器视觉一书纠错笔记

淘宝客APP带自营商城本地生活CPS外卖优惠电影票话费更新渠道跟单生活特权V3

MySql面试题总结

前置++和后置++的区别

golang-条件语句

Nacos cluster construction

MySQL:基础部分

Collision, character controller, Cloth components (cloth), joints in the Unity physics engine

Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
随机推荐
共享内存+inotify机制实现多进程低延迟数据共享
单片机原理与应用复习
单片机期末复习大题
typescript68-索引查询类型(查询多个)
自营商城提高用户留存小技巧,商城对接小游戏分享
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
D41_buffer pool
2022杭电多校六 1006-Maex (树形DP)
typescript66-分析partial的实现
typescript61-泛型工具类型(pick)
Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
numpy.random使用文档
Email management Filter emails
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
【C语言】结构体变量数据通过 void* 传入到函数中
The NDK compiler so libraries
After docker is deployed, mysql cannot connect
基于KECA-IGWO-KELM的间歇过程故障诊断方法
亚马逊美国站:马术头盔CPC认证标准要求