新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 printf問題

        STM32 printf問題

        作者: 時間:2016-12-03 來源:網絡 收藏
        STM32 printf問題
        怎么老是出現idenfier "FILE is Undefined",問題出在哪啊?望指教,謝謝!
        我程序代碼配置如下:
        #include "stm32f10x.h"
        #include "stm32_eval.h"
        #include

        #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__ */

        int main(void)
        {
        SystemInit();
        /* Enable GPIOA and USART1 clocks */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);

        USART1_GPIO_Configuration();
        USART1_Configuration();

        printf("The is a example!" );

        }


        /*********************************配置USART1的GPIO口*************************************/
        void USART1_GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        // Configure USART1_Tx as alternate function push-pull
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        // Configure USART1_Rx as input floating
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        }
        /************************************配置串口1參數***********************************/

        void USART1_Configuration(void)
        {

        USART_InitTypeDef USART_InitStructure;

        USART_InitStructure.USART_BaudRate =115200;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No ;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1, ENABLE);
        }


        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.仍然使用標準庫,在主程序添加下面代碼:

        #pragma import(__use_no_semihosting)
        _sys_exit(int x)
        {
        x = x;
        }
        struct __FILE
        {
        int handle;
        /* Whatever you require here. If the only file you are using is */
        /* standard output using printf() for debugging, no file handling */
        /* is required. */
        };
        /* FILE is typedef’ d in stdio.h. */
        FILE __stdout;

        這個方法確實解決問題。

        首先要加上這句:#include
        然后加入以下代碼:
        ///////使用標準printf的串口字節發送重定義///////////
        //第一步:
        #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__ */
        //第二步:
        #pragma import(__use_no_semihosting)
        _sys_exit(int x)
        {
        x = x;
        }
        struct __FILE
        {
        int handle;
        /* Whatever you require here. If the only file you are using is */
        /* standard output using printf() for debugging, no file handling */
        /* is required. */
        };
        /* FILE is typedef’ d in stdio.h. */
        FILE __stdout;
        //第三步:
        //默認使用COM1
        /**
        * @briefRetargets the C library printf function to the USART.
        * @paramNone
        * @retval None
        */
        PUTCHAR_PROTOTYPE
        {
        /* Place your implementation of fputc here */
        /* e.g. write a character to the USART */

        /* Loop until the end of transmission */
        while (USART_GetFlagStatus(USART_COM1, USART_FLAG_TC) == RESET)
        {}
        USART_SendData(USART_COM1, (uint8_t) ch);
        return ch;
        }
        ////////////////////////////////////////////////////


        關鍵詞: STM32printf問

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 通州区| 博湖县| 温泉县| 延寿县| 家居| 香河县| 合水县| 万州区| 获嘉县| 黄大仙区| 肥城市| 新竹市| 朝阳区| 象山县| 洛阳市| 浠水县| 纳雍县| 班戈县| 阳曲县| 阿拉善左旗| 黄梅县| 金寨县| 阿尔山市| 昔阳县| 玛沁县| 潼关县| 梓潼县| 项城市| 中阳县| 仁布县| 鲁甸县| 阿拉善右旗| 沁源县| 定日县| 常德市| 阿图什市| 高邮市| 十堰市| 年辖:市辖区| 安远县| 会东县|