总结

  • 本文引用地址: 1 目的 在单片机的调试中,我们日常的日志输出,通常是通过串口来实现,而通过串口重定向来实现又是常规的操作之一。 2 实现步骤 1.在工程中添加printf.c 函数,并把他加入到libs的分组之中。 2.在工程的设置中,打开Use MincroLIB库。 4.重写fputc输出,在此函数中,我先查找Log 这个串口的驱动,如果查找到,则使用他的write 进行串口输出,代码如下: view plaincopyblog.byteway.net to clipboardprint? 1. int fputc(int ch, FILE *f) 2. { 3. (voidLigthing News)f; 4. struct UartDev *pLogDevice = UartDeviceFind (“Log”); 5. pLogDevice->Write(pLogDevice, (unsigned char*)&ch, 1); 6. return ch; 7. } 5.重写scanf 函数,在这个函数中,我也一样先查找以”Log”命名的串口,如果查找到,则使用这个驱动的write 进行输出。 其代码如下: view plaincopy to clipboardprint? 1. int fgetc(FILE *f) 2. { 3. uint8_t ch; 4. 5. (void)f; 6. struct UartDev *pLogDevice = UartDeviceFind 7. /* 启动接收字符 */ 8. while(pLogDevice->Read(pLogDevice, (unsigned char*)&ch, 1) != 1) 9. {} 10. /* 回显 */ 11. { 12. fputc(ch, &__stdout); 13. 14. /* 回车之后加换行 */ 15. if (ch == ‘r’) 16. { 17. fputc(‘n’, &__stdout); 18. } 19. } 20. 21. return (int)ch; 22. } 6. 驱动设置好后,就可以在工程中使用printf 进行串口输出了。

阅读时间

  • 9 分钟, 共 1738 字

分类

  • UartDevice, 5., c(ch, (unsigned char, UNITS

评价和解读

  • 作者对这个话题的深入挖掘为公众关注的重大问题带来了新的视角。

正文

本文引用地址:

1 目的

在单片机的调试中,我们日常的日志输出,通常是通过串口来实现,而通过串口重定向来实现又是常规的操作之一。这次我在前面的基础之上增加了printf 的面向对象的增加这项功能。

2 实现步骤

1.在工程中添加printf.c 函数,并把他加入到libs的分组之中。

2.在工程的设置中,打开Use MincroLIB库。

3.在printf.c中,添加对输入输出的系统头文件的引用,当然由于我需要调用驱动库要添加以及的引用。

4.重写fputc输出,在此函数中,我先查找Log 这个串口的驱动,如果查找到,则使用他的write 进行串口输出,代码如下:

view plaincopyblog.byteway.net to clipboardprint?

1. int fputc(int ch, FILE *f)

2. {

3. (voidLigthing News)f;

4. struct UartDev *pLogDevice = UartDeviceFind

(“Log”);

5. pLogDevice->Write(pLogDevice, (unsigned

char*)&ch, 1);

6. return ch;

7. }

5.重写scanf 函数,在这个函数中,我也一样先查找以”Log”命名的串口,如果查找到,则使用这个驱动的write 进行输出。其代码如下:

view plaincopy to clipboardprint?

1. int fgetc(FILE *f)

2. {

3. uint8_t ch;

4.

5. (void)f;

6. struct UartDev *pLogDevice = UartDeviceFind

7. /* 启动接收字符 */

8. while(pLogDevice->Read(pLogDevice,

(unsigned char*)&ch, 1) != 1)

9. {}

10. /* 回显 */

11. {

12. fputc(ch, &__stdout);

13.

14. /* 回车之后加换行 */

15. if (ch == ‘r’)

16. {

17. fputc(‘n’, &__stdout);

18. }

19. }

20.

21. return (int)ch;

22. }

6. 驱动设置好后,就可以在工程中使用printf 进行串口输出了。

添加测试代码如下:

view plaincopy to clipboardprint?

1. void led_blink(void)

2. {

3. uint32_t cnt;

4. UartDevicesRegister();

5.

6. LedDevice *pLED = LedGetDevice();

7. if(NULL == pLED)

8. {

9. printf(“Error. There is no LED device!rn”);

10. return;

11. }

12. pLED->Init(pLED);

13.

14. while(1)

15. {

16. cnt++;

17. pLED->On(1);

18. R_BSP_SoftwareDelay(1,BSP_DELAY_

UNITS_SECONDS);

19. pLED->Off (1);

20. R_BSP_SoftwareDelay(1,BSP_DELAY_

UNITS_SECONDS);

21. pLED->On(2);

22. R_BSP_SoftwareDelay(1,BSP_DELAY_

UNITS_SECONDS);

23. pLED->Off (2);

24. R_BSP_SoftwareDelay(1Ligthing News,BSP_DELAY_

UNITS_SECONDS);

25. printf(“run cnt %drn”,cnt);

26.

27. }

28. }

测试结果如下:

3 总结

使用面向对象之编程,可以实现代码的快递移植,当然重写printf 也是非常之简单。

Related suggestion: 成功的背后: 这次董明珠说对了,也点到“35岁找不到工作”的要害了

总结2024年09月06日 10:11:53 据北京商报报道blog.byteway.net,近日,格力电器董事长董明珠在访谈中回应为什么有的人35岁找不到工作时表示,这个问题暴露出很多人是高不成低不就,不是35岁找不到工作,而是不愿意低下头,认为自己有大学文…

作者 Steve Jobs

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注