中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 瑞薩RA0單片機連載——實現(xiàn)串口重定向之printf

        瑞薩RA0單片機連載——實現(xiàn)串口重定向之printf

        作者:lulugl 時間:2024-11-22 來源:EEPW 收藏

        1   目的

        本文引用地址:http://www.antipu.com.cn/article/202411/464858.htm

        的調試中,我們日常的日志輸出,通常是通過串口來實現(xiàn),而通過串口重定向來實現(xiàn)又是常規(guī)的操作之一。這次我在前面的基礎之上增加了 的面向對象的增加這項功能。

        2   實現(xiàn)步驟

        1.在工程中添加.c 函數(shù),并把他加入到libs的分組之中。

        1732237973236708.png

        2.在工程的設置中,打開Use MincroLIB庫。

        1732238008386481.png

        3.在.c中,添加對輸入輸出的系統(tǒng)頭文件<stdio.h>的引用,當然由于我需要調用驅動庫要添加<devices.h>以及<hal_data.h>的引用。

        4.重寫fputc輸出,在此函數(shù)中,我先查找Log 這個串口的驅動,如果查找到,則使用他的write 進行串口輸出,代碼如下:

        view plaincopy to clipboardprint?

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

        2. {

        3. (void)f;

        4. struct UartDev *pLogDevice = UartDeviceFind

        (“Log”);

        5. pLogDevice->Write(pLogDevice, (unsigned

        char*)&ch, 1);

        6. return ch;

        7. }

        5.重寫scanf 函數(shù),在這個函數(shù)中,我也一樣先查找以”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(1,BSP_DELAY_

        UNITS_SECONDS);

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

        26.

        27. }

        28. }

        測試結果如下:

        1732238117760102.png

        3   總結

        使用面向對象之編程,可以實現(xiàn)代碼的快遞移植,當然重寫printf 也是非常之簡單。

        (本文來源于《EEPW》2024年11期)



        評論


        相關推薦

        技術專區(qū)

        關閉