<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32+Keil 如何使用printf函數(shù)?

          STM32+Keil 如何使用printf函數(shù)?

          作者: 時間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
          Keil不支持Host-semi機(jī)制,即不支持直接在IDE打印字符串。

          那么只能通過程序向硬件串口發(fā)數(shù)據(jù)了,這樣調(diào)用的時候用自定義的函數(shù)即可,也很方便,例如:

          本文引用地址:http://www.ex-cimer.com/article/201611/317105.htm

          void send_char_to_usart(unsigned char c){}

          但是可否直接使用printf函數(shù)呢?畢竟人家都做好了,我們給他定一個打印輸出的接口就可以了,答案是肯定的,看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的數(shù)據(jù)打印到該串口上,因此你需要一個串口線與STM32和電腦相連,這樣就可以看到printf了。


          關(guān)鍵詞: STM32Keilprintf函

          評論


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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();