新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32程序添加printf后無法運行的解決方法

        STM32程序添加printf后無法運行的解決方法

        作者: 時間:2016-11-11 來源:網(wǎng)絡 收藏
        標準庫函數(shù)的默認輸出設備是顯示器,要實現(xiàn)在串口或LCD輸出,必須重定義標準庫函數(shù)里調(diào)用的與輸出設備相關(guān)的函數(shù).

        例如:printf輸出到串口,需要將fputc里面的輸出指向串口(重定向),方法如下:

        本文引用地址:http://www.104case.com/article/201611/316689.htm

        #ifdef __GNUC__
        /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
        set to Yes) calls __io_putchar() */
        #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
        #else
        #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
        #endif /* __GNUC__ */

        PUTCHAR_PROTOTYPE
        {
        /* Place your implementation of fputc here */
        /* e.g. write a character to the USART */
        USART_SendData(USART1, (uint8_t) ch);
        /* Loop until the end of transmission */
        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
        return ch;
        }

        因printf()之類的函數(shù),使用了半主機模式。使用標準庫會導致程序無法運行,以下是解決方法:

        方法1.使用微庫,因為使用微庫的話,不會使用半主機模式.

        方法2.仍然使用標準庫,在主程序添加下面代碼:

        #pragmaimport(__use_no_semihosting)
        _sys_exit(intx)
        {
        x=x;
        }
        struct__FILE
        {
        inthandle;
        /*Whateveryourequirehere.Iftheonlyfileyouareusingis*/
        /*standardoutputusingprintf()fordebugging,nofilehandling*/
        /*isrequired.*/
        };
        /*FILEistypedef’dinstdio.h.*/
        FILE__stdout;



        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 嵊泗县| 工布江达县| 麻栗坡县| 教育| 兰考县| 景洪市| 广安市| 奎屯市| 涡阳县| 西平县| 阳西县| 墨竹工卡县| 壶关县| 柳州市| 宜春市| 尖扎县| 孙吴县| 玉林市| 辽宁省| 大悟县| 洛扎县| 兴和县| 德清县| 普格县| 西乡县| 花莲市| 乌拉特后旗| 兴仁县| 六盘水市| 瑞丽市| 错那县| 金阳县| 南部县| 内江市| 临高县| 新安县| 正蓝旗| 松潘县| 偃师市| 台东县| 辰溪县|