新闻中心

EEPW首页>嵌入式系统>设计应用> STM32+Keil 如何使用printf函数?

STM32+Keil 如何使用printf函数?

作者: 时间:2016-11-10 来源:网络 收藏
Keil不支持Host-semi机制,即不支持直接在IDE打印字符串。

那么只能通过程序向硬件串口发数据了,这样调用的时候用自定义的函数即可,也很方便,例如:

本文引用地址://m.amcfsurvey.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函

评论


技术专区

关闭