打印
[技术问答]

M0516的printf

[复制链接]
1328|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
billbillqaz|  楼主 | 2015-7-31 10:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
为什么我找不到新塘M0516的printf函数,在库文件中也没找到,printf相关的参数在哪里设置的?
沙发
springvirus| | 2015-7-31 10:26 | 只看该作者
Retargetting a C Library Function
Overview Following on from (Getting started with Cortex-M3 CMSIS programming >>). In this tutorial we will look at printing messages on a text console.
Background Information
In a typical C application messages are printed to onto the computer screen via printf(). However, in the case of an embedded device, the printf() functionality would have to be altered to redirect the characters from a computer screen to a console. Since there is no true console attached, STDOUT will be emulated using Cortex-M3 ITM (Instrumentation Trace Microcell). ITM is output-only, so STDIN will be emulated by a special protocol defined by CMSIS and hardwired into μVision. The main advantage of STDIN/STDOUT emulation is that it works in exactly the same way in the simulator as on every Cortex-M3 device.
To get the best out of this tutorial you may decide to download the following support files. In exchange, we will ask you to enter some personal details. On the registration form, you will be asked whether you want us to send you further information concerning other Doulos products and services in the subject area concerned.
Download the files for this tutorial >> Download the required software >> Actions
Invoke μVision and navigate to the \intro\session1 directory: File > Open.
Open the provided project file called session1.uproj.
Ensure that project session1 is active (highlighted). Otherwise right-click the project name and select: Set as Active Project.
Starting from the files provided for our previous tutorial, we have now added a printf() statement inside the loop to print the value of the variable .
int main(void) { int j = 0;
        unsigned char i =0;
        while (1) { j++;
                if (j==1000000) { j = 0;
                        if (i==0xFFFF) i = 0;
                        printf("Value of i: %d\n", i);
                        i++; } } } The provided file \intro\common\retarget.c has been added inside the User group. This file redefines functions used by printf() for outputting characters. The printf() function ultimately relies on the fputc() function to operate. The fputc() has been implemented using the CMSIS standard function ITM_SendChar().
int fputc(int ch, FILE *f) { return ITM_SendChar(ch); } In contrast the fgetc() function relies on the CMSIS standard function ITM_ReceiveChar().
int fgetc(FILE *f) { int r;
    /* if we just backspaced, then return the backspaced character */
    /* otherwise output the next character in the stream */
    if (backspace_called == 1)
    { backspace_called = 0; }
    else { do { r = ITM_ReceiveChar(); } while (r == -1);
        last_char_read = (unsigned char)r; #ifdef ECHO_FGETC ITM_SendChar(r); #endif }
    return last_char_read; } The declaration of the ITM_SendChar() can be found inside the core_cm3.h file and is as follows
static __INLINE uint32_t ITM_SendChar (uint32_t ch) To complete the separation from the standard I/O library we also have had to redefine __stdout and __stdin. These can be found inside the retarget.c file below the declaration of the __FILE structure.
FILE __stdin; FILE __stdout; You will have to rebuild the project and re-invoke the debugger (see previous tutorial)..
In order to watch the program output we will need a text console. If the Debug (printf) Viewer is not visible, it can be activated via the menu: View > Serial Windows > Debug (printf) Viewer.
Execute the program and observe the message printed on the terminal
This concludes this second part of the series. In the final tutorial >> we will look at proving that our program is working adequately by executing it on a hardware evaluation board.

使用特权

评论回复
板凳
09kk小熊| | 2015-7-31 10:27 | 只看该作者
它是c语言中产生格式化输出的函数(在 stdio.h 中定义)

使用特权

评论回复
地板
springvirus| | 2015-7-31 10:28 | 只看该作者
printf最终调用fputc,所以应该可以在工程里找到retarget.c,其中有fputc(),此函数中就使用你当前主控的UART

使用特权

评论回复
5
稳稳の幸福| | 2015-7-31 12:00 | 只看该作者
上面说的有道理,好像就是fputc是printf的子函数。

使用特权

评论回复
6
huangcunxiake| | 2016-8-6 08:35 | 只看该作者
在stdio.h里,不是单片机专属的,是C语言的。

使用特权

评论回复
7
gejigeji521| | 2016-8-6 11:55 | 只看该作者
你可以看看串口通信的例程,里面有怎么用printf,这个要包含头文件stdio.h

使用特权

评论回复
8
zhuotuzi| | 2016-8-6 15:29 | 只看该作者
重定向到新的函数实现格式化输出。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

112

主题

191

帖子

2

粉丝