STM32 printf的重定向

2021-07-25 22:55:00 字數 1295 閱讀 4438

stm32串列埠通訊中使用printf傳送資料配置方法(開發環境 keil rvmdk)

標籤:  stm32  串列埠通訊  printf方法  2011-06-29 23:29 

在stm32串列埠通訊程式中使用printf傳送資料,非常的方便。可在剛開始使用的時候總是遇到問題,常見的是硬體訪真時無法進入main主函式,其實只要簡單的配置一下就可以了。

下面就說一下使用printf需要做哪些配置。

有兩種配置方法:

一、對工程屬性進行配置,詳細步驟如下

1、首先要在你的main 檔案中 包含「stdio.h」 (標準輸入輸出標頭檔案)。

2、在main檔案中重定義函式    如下:

// 傳送資料

int fputc(int ch, file *f)

// 接收資料

int getkey (void)  

這樣在使用printf時就會呼叫自定義的fputc函式,來傳送字元。

3、在工程屬性的 「target" -> "code generation" 選項中勾選 "use microlib"」 

microlib 是預設c的備份庫,關於它可以到網上查詢詳細資料。

至此完成配置,在工程中可以隨意使用printf向串列埠傳送資料了。

二、第二種方法是在工程中新增「regtarge.c」檔案

1、在main檔案中包含 「stdio.h」 檔案

2、在工程中建立乙個檔案儲存為 regtarge.c , 然後將其新增工程中

在檔案中輸入如下內容(直接複製即可)

#include

#include

#pragma import(__use_no_semihosting_swi)

extern int  sendchar(int ch); // 宣告外部函式,在main檔案中定義

extern int  getkey(void);

struct __file ;

file __stdout;

file __stdin;

int fputc(int ch, file *f)

int fgetc(file *f)

void _ttywrch(int ch)

int ferror(file *f)

void _sys_exit(int return_code)

3、在main檔案中新增定義以下兩個函式

int sendchar (int ch)  

int getkey (void)  

至此完成配置,可以在main檔案中隨意使用 printf 。

STM32 printf 重定向問題

1 declaration conflicts with target of using declaration already in scope 不能使用.cpp檔案,若要使用printf重定向,那就使用.c 檔案 解決方法 1 在main檔案中包含 stdio.h 檔案 2 在工程中建立乙個檔案...

stm32 printf函式重定向

include stm32f10x.h include stdio.h void rcc configuration void 首先設定系統時鐘為8mhz void gpio configuration void void usart configuration void function name...

STM32 printf重定向及輸出指定長度字串

注意 printf是在stdio.h中宣告的 include ifdef gnuc with gcc raisonance,small printf option ld linker libraries small printf set to yes calls io putchar define ...