微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32+Keil 如何使用printf函数?

STM32+Keil 如何使用printf函数?

时间:11-10 来源:互联网 点击:
Keil不支持Host-semi机制,即不支持直接在IDE打印字符串。

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

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("\r\nSTM32l1xxFirmwareLibrarycompiledwithFULLASSERTfunction...\n\r");
  88. printf("...Run-timecheckingenabled\n\r");
  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_InitStruc

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top