新聞中心

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

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

        作者: 時間:2016-11-11 來源:網絡 收藏
        標準庫函數的默認輸出設備是顯示器,要實現在串口或LCD輸出,必須重定義標準庫函數里調用的與輸出設備相關的函數.

        例如: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()之類的函數,使用了半主機模式。使用標準庫會導致程序無法運行,以下是解決方法:

        方法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;



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 盖州市| 柳林县| 观塘区| 新源县| 图木舒克市| 昭觉县| 会昌县| 阿勒泰市| 蒙山县| 兰坪| 策勒县| 榆社县| 泌阳县| 珠海市| 陈巴尔虎旗| 平南县| 西和县| 屯留县| 上杭县| 青州市| 嫩江县| 永嘉县| 鄄城县| 马鞍山市| 漳州市| 和田市| 金坛市| 宜黄县| 宜君县| 仙居县| 山阴县| 兰考县| 江孜县| 沁阳市| 武威市| 曲沃县| 柘城县| 安西县| 湾仔区| 鄯善县| 班玛县|