新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32+Keil 如何使用printf函數?

        STM32+Keil 如何使用printf函數?

        作者: 時間:2016-11-10 來源:網絡 收藏
        Keil不支持Host-semi機制,即不支持直接在IDE打印字符串。

        那么只能通過程序向硬件串口發數據了,這樣調用的時候用自定義的函數即可,也很方便,例如:

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

        void send_char_to_usart(unsigned char c){}

        但是可否直接使用printf函數呢?畢竟人家都做好了,我們給他定一個打印輸出的接口就可以了,答案是肯定的,看ST的官方源碼:

        1. /**
        2. ******************************************************************************
        3. *@fileLib_DEBUG/Lib_DEBUG_Example/main.c
        4. *@authorMCDApplicationTeam
        5. *@versionV1.1.1
        6. *@date13-April-2012
        7. *@briefMainprogrambody
        8. ******************************************************************************
        9. *@attention
        10. *
        11. *

          COPYRIGHT2012STMicroelectronics

        12. *
        13. *LicensedunderMCD-STLibertySWLicenseAgreementV2,(the"License");
        14. *YoumaynotusethisfileexceptincompliancewiththeLicense.
        15. *YoumayobtainacopyoftheLicenseat:
        16. *
        17. *http://www.st.com/software_license_agreement_liberty_v2
        18. *
        19. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
        20. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
        21. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
        22. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
        23. *limitationsundertheLicense.
        24. *
        25. ******************************************************************************
        26. */
        27. /*Includes------------------------------------------------------------------*/
        28. #include"stm32l1xx.h"
        29. #include"stm32l1xx_ip_dbg.h"
        30. #include
        31. #ifdefUSE_STM32L152D_EVAL
        32. #include"stm32l152d_eval.h"
        33. #else
        34. #include"stm32l152_eval.h"
        35. #endif
        36. /**@addtogroupSTM32L1xx_StdPeriph_Examples
        37. *@{
        38. */
        39. /**@addtogroupLib_DEBUG_Example
        40. *@{
        41. */
        42. /*Privatetypedef-----------------------------------------------------------*/
        43. /*Privatedefine------------------------------------------------------------*/
        44. /*Privatemacro-------------------------------------------------------------*/
        45. /*Privatevariables---------------------------------------------------------*/
        46. USART_InitTypeDefUSART_InitStructure;
        47. /*Privatefunctionprototypes-----------------------------------------------*/
        48. #ifdef__GNUC__
        49. /*WithGCC/RAISONANCE,smallprintf(optionLDLinker->Libraries->Smallprintf
        50. settoYes)calls__io_putchar()*/
        51. #definePUTCHAR_PROTOTYPEint__io_putchar(intch)
        52. #else
        53. #definePUTCHAR_PROTOTYPEintfputc(intch,FILE*f)
        54. #endif/*__GNUC__*/
        55. /*Privatefunctions---------------------------------------------------------*/
        56. /**
        57. *@briefMainprogram
        58. *@paramNone
        59. *@retvalNone
        60. */
        61. intmain(void)
        62. {
        63. /*!
        64. thisisdonethroughSystemInit()functionwhichiscalledfromstartup
        65. file(startup_stm32l1xx_xx.s)beforetobranchtoapplicationmain.
        66. ToreconfigurethedefaultsettingofSystemInit()function,referto
        67. system_stm32l1xx.cfile
        68. */
        69. GPIO_InitTypeDefGPIOA_InitStructure;
        70. /*USARTxconfiguredasfollow:
        71. -BaudRate=115200baud
        72. -WordLength=8Bits
        73. -OneStopBit
        74. -Noparity
        75. -Hardwareflowcontroldisabled(RTSandCTSsignals)
        76. -Receiveandtransmitenabled
        77. */
        78. USART_InitStructure.USART_BaudRate=115200;
        79. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
        80. USART_InitStructure.USART_StopBits=USART_StopBits_1;
        81. USART_InitStructure.USART_Parity=USART_Parity_No;
        82. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        83. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
        84. STM_EVAL_COMInit(COM1,&USART_InitStructure);
        85. /*Initializeallperipheralspointers*/
        86. IP_Debug();
        87. printf("rnSTM32l1xxFirmwareLibrarycompiledwithFULLASSERTfunction...nr");
        88. printf("...Run-timecheckingenablednr");
        89. /*Simulatewrongparameterpassedtolibraryfunction---------------------*/
        90. /*ToenableSPI1clock,RCC_APB2PeriphClockCmdfunctionmustbeusedand
        91. notRCC_APB1PeriphClockCmd*/
        92. RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
        93. /*SomememberofGPIOA_InitStructurestructurearenotinitialized*/
        94. GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_6;
        95. GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
        96. /*GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_40MHz;*/
        97. GPIOA_InitStructure.GPIO_OType=GPIO_OType_PP;
        98. GPIOA_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
        99. GPIO_Init(GPIOA,&GPIOA_InitStructure);
        100. while(1)
        101. {
        102. }
        103. }
        104. #ifdefUSE_FULL_ASSERT
        105. /**
        106. *@briefReportsthenameofthesourcefileandthesourcelinenumber
        107. *wheretheassert_paramerrorhasoccurred.
        108. *@paramfile:pointertothesourcefilename
        109. *@paramline:assert_paramerrorlinesourcenumber
        110. *@retvalNone
        111. */
        112. voidassert_failed(uint8_t*file,uint32_tline)
        113. {
        114. /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber*/
        115. printf("nrWrongparametervaluedetectedonrn");
        116. printf("file%srn",file);
        117. printf("line%drn",line);
        118. /*Infiniteloop*/
        119. /*while(1)
        120. {
        121. }*/
        122. }
        123. #endif
        124. /**
        125. *@briefRetargetstheClibraryprintffunctiontotheUSART.
        126. *@paramNone
        127. *@retvalNone
        128. */
        129. PUTCHAR_PROTOTYPE
        130. {
        131. /*Placeyourimplementationoffputchere*/
        132. /*e.g.writeacharactertotheUSART*/
        133. USART_SendData(EVAL_COM1,(uint8_t)ch);
        134. /*Loopuntiltheendoftransmission*/
        135. while(USART_GetFlagStatus(EVAL_COM1,USART_FLAG_TC)==RESET)
        136. {
        137. }
        138. returnch;
        139. }
        140. /**
        141. *@}
        142. */
        143. /**
        144. *@}
        145. */
        146. /************************(C)COPYRIGHTSTMicroelectronics*****ENDOFFILE****/

        該例子就是把COM1作為輸出口,把printf的數據打印到該串口上,因此你需要一個串口線與STM32和電腦相連,這樣就可以看到printf了。


        關鍵詞: STM32Keilprintf函

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 土默特右旗| 都昌县| 延寿县| 乃东县| 金昌市| 绿春县| 黄梅县| 柞水县| 沅陵县| 搜索| 荣昌县| 临沭县| 清流县| 宜兰县| 淳安县| 洪江市| 诏安县| 潜江市| 全椒县| 津市市| 云和县| 宁津县| 富阳市| 泊头市| 保亭| 弥渡县| 卫辉市| 靖州| 玛沁县| 乌兰察布市| 伊春市| 尤溪县| 澄江县| 会同县| 吉木乃县| 德格县| 象山县| 肇东市| 江阴市| 白朗县| 九寨沟县|