当前位置:网站首页>07 printf重定向
07 printf重定向
2022-07-15 11:35:00 【haichuangdianzi】
在上个实验的基础上进行修改,直接打开keil工程,先软件进行配置。
- 打开“USART.c”文件,添加“#include<stdio.h>”头文件。

- 在这个文件最后添加如下程序。
/* USER CODE BEGIN 1 */
int fputc(int ch, FILE *stream)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch,1, 0xFFFF);
return ch;
}
/* USER CODE END 1 */
- 准备工作完成后回到主函数,再次添加“#include<stdio.h>”头文件。

- 在while程序中完成代码。
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
printf("Hello World! \r\n");
HAL_Delay(1000);
}
/* USER CODE END 3 */
最后输出结果。
注:很多地方讲 \n 可以换行,但是实测不行,而是通过 \r\n 来实现。
————————————————————————————————————
第二种法:
在USART.c文件添加如下程序。
/* USER CODE BEGIN 1 */
__align(8) char USART2_TxBuff[256];
void u2_printf(char* fmt,...)
{
unsigned int length=0;
va_list ap;
va_start(ap,fmt);
vsprintf(USART2_TxBuff,fmt,ap);
va_end(ap);
length=strlen((const char*)USART2_TxBuff);
HAL_UART_Transmit(&huart2, (uint8_t *)USART2_TxBuff, length, 0xffff);
while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)!=SET);
}
/* USER CODE END 1 */
同时在USART.c添加对应的头文件。
/* USER CODE BEGIN 0 */
#include "stdio.h"
#include "stdarg.h"
#include "string.h"
/* USER CODE END 0 */
在usart.h对应头文件进行声明。
/* USER CODE BEGIN Prototypes */
void u2_printf(char* fmt,...) ;
/* USER CODE END Prototypes */
接下来可以在主函数测试。
/* USER CODE BEGIN 3 */
u2_printf("Hello XP\r\n");
HAL_Delay(1000);
/* USER CODE END 3 */
边栏推荐
- 暑假学习计划【活动】
- Quick start to 3dsmax development
- Golang problem summary
- 云呐-动环监控系统实现无人化的快速故障处理
- Young people abandon new beauty collection stores
- Figure 1: four directions and three strategies for digital transformation of state-owned enterprises
- [Google] 再见 SharedPreferences 拥抱 Jetpack DataStore
- 2022 global developer salary PK: China ranks 19th, and using go language is the most profitable
- Kotlin Sealed 是什么?为什么 Google 都用
- 【招募】2022向光奖|年度ESG影响力投资奖正式启动!
猜你喜欢

Young people abandon new beauty collection stores

Dark clouds and chaos of bird's nest economy

How does the mobile phone control the LED display to send video?

The mystery of any type conversion

神奇宝贝 眼前一亮的 Jetpack + MVVM 极简实战

Tiktok launched "group purchase and distribution" to explore a new model of takeout

竟然如此简单,DataBinding 和 ViewBinding

2022-04-21 Unity入门8——音效系统

实验1.SQL Server的安全机制

Crmeb Pro v1.4 makes the user experience more brilliant!
随机推荐
云呐-机房动环监控扩容方案
Chapter 8 delegates, lambda expressions, and events
MySql中IGNORE、ON DUPLICATE KEY UPDATE、DELAYED
[flag] build a website around Li Jian that sorts and classifies by time axis and content (zero basis) (continuous update)
实验1.SQL Server的安全机制
Heap sort
想要白嫖正则是吧?这一次给你个够!
Golang problem summary
Landing DDD (7) - some misunderstandings in tactical design
2022-04-21 Unity基础2——MonoBehaviour的重要内容
【福利活动】给你的代码叠个 Buff!点击“茶”收好礼
Use iceberg in CDP to pressurize the data Lake warehouse
CDeC-Net代码实现
活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
龙蜥社区招募推广大使&体验官啦!| 人人都可以参与开源
Version announcement | Apache Doris 1.1 release version officially released!
How does the mobile phone control the LED display to send video?
[recruitment] 2022 Xiangguang award annual ESG influence investment award is officially launched!
推特大战马斯克:想走,哪那么容易
[benefit activity] stack a buff for your code! Click "tea" to receive the gift