STM32+Keil 如何使用printf函數(shù)?
那么只能通過(guò)程序向硬件串口發(fā)數(shù)據(jù)了,這樣調(diào)用的時(shí)候用自定義的函數(shù)即可,也很方便,例如:
本文引用地址:http://www.ex-cimer.com/article/201611/317105.htmvoid send_char_to_usart(unsigned char c){}
但是可否直接使用printf函數(shù)呢?畢竟人家都做好了,我們給他定一個(gè)打印輸出的接口就可以了,答案是肯定的,看ST的官方源碼:
- /**
- ******************************************************************************
- *@fileLib_DEBUG/Lib_DEBUG_Example/main.c
- *@authorMCDApplicationTeam
- *@versionV1.1.1
- *@date13-April-2012
- *@briefMainprogrambody
- ******************************************************************************
- *@attention
- *
- *
COPYRIGHT2012STMicroelectronics - *
- *LicensedunderMCD-STLibertySWLicenseAgreementV2,(the"License");
- *YoumaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat:
- *
- *http://www.st.com/software_license_agreement_liberty_v2
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- *
- ******************************************************************************
- */
- /*Includes------------------------------------------------------------------*/
- #include"stm32l1xx.h"
- #include"stm32l1xx_ip_dbg.h"
- #include
- #ifdefUSE_STM32L152D_EVAL
- #include"stm32l152d_eval.h"
- #else
- #include"stm32l152_eval.h"
- #endif
- /**@addtogroupSTM32L1xx_StdPeriph_Examples
- *@{
- */
- /**@addtogroupLib_DEBUG_Example
- *@{
- */
- /*Privatetypedef-----------------------------------------------------------*/
- /*Privatedefine------------------------------------------------------------*/
- /*Privatemacro-------------------------------------------------------------*/
- /*Privatevariables---------------------------------------------------------*/
- USART_InitTypeDefUSART_InitStructure;
- /*Privatefunctionprototypes-----------------------------------------------*/
- #ifdef__GNUC__
- /*WithGCC/RAISONANCE,smallprintf(optionLDLinker->Libraries->Smallprintf
- settoYes)calls__io_putchar()*/
- #definePUTCHAR_PROTOTYPEint__io_putchar(intch)
- #else
- #definePUTCHAR_PROTOTYPEintfputc(intch,FILE*f)
- #endif/*__GNUC__*/
- /*Privatefunctions---------------------------------------------------------*/
- /**
- *@briefMainprogram
- *@paramNone
- *@retvalNone
- */
- intmain(void)
- {
- /*!
- thisisdonethroughSystemInit()functionwhichiscalledfromstartup
- file(startup_stm32l1xx_xx.s)beforetobranchtoapplicationmain.
- ToreconfigurethedefaultsettingofSystemInit()function,referto
- system_stm32l1xx.cfile
- */
- GPIO_InitTypeDefGPIOA_InitStructure;
- /*USARTxconfiguredasfollow:
- -BaudRate=115200baud
- -WordLength=8Bits
- -OneStopBit
- -Noparity
- -Hardwareflowcontroldisabled(RTSandCTSsignals)
- -Receiveandtransmitenabled
- */
- 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;
- STM_EVAL_COMInit(COM1,&USART_InitStructure);
- /*Initializeallperipheralspointers*/
- IP_Debug();
- printf("rnSTM32l1xxFirmwareLibrarycompiledwithFULLASSERTfunction...nr");
- printf("...Run-timecheckingenablednr");
- /*Simulatewrongparameterpassedtolibraryfunction---------------------*/
- /*ToenableSPI1clock,RCC_APB2PeriphClockCmdfunctionmustbeusedand
- notRCC_APB1PeriphClockCmd*/
- RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
- /*SomememberofGPIOA_InitStructurestructurearenotinitialized*/
- GPIOA_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIOA_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
- /*GPIOA_InitStructure.GPIO_Speed=GPIO_Speed_40MHz;*/
- GPIOA_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIOA_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA,&GPIOA_InitStructure);
- while(1)
- {
- }
- }
- #ifdefUSE_FULL_ASSERT
- /**
- *@briefReportsthenameofthesourcefileandthesourcelinenumber
- *wheretheassert_paramerrorhasoccurred.
- *@paramfile:pointertothesourcefilename
- *@paramline:assert_paramerrorlinesourcenumber
- *@retvalNone
- */
- voidassert_failed(uint8_t*file,uint32_tline)
- {
- /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber*/
- printf("nrWrongparametervaluedetectedonrn");
- printf("file%srn",file);
- printf("line%drn",line);
- /*Infiniteloop*/
- /*while(1)
- {
- }*/
- }
- #endif
- /**
- *@briefRetargetstheClibraryprintffunctiontotheUSART.
- *@paramNone
- *@retvalNone
- */
- PUTCHAR_PROTOTYPE
- {
- /*Placeyourimplementationoffputchere*/
- /*e.g.writeacharactertotheUSART*/
- USART_SendData(EVAL_COM1,(uint8_t)ch);
- /*Loopuntiltheendoftransmission*/
- while(USART_GetFlagStatus(EVAL_COM1,USART_FLAG_TC)==RESET)
- {
- }
- returnch;
- }
- /**
- *@}
- */
- /**
- *@}
- */
- /************************(C)COPYRIGHTSTMicroelectronics*****ENDOFFILE****/
該例子就是把COM1作為輸出口,把printf的數(shù)據(jù)打印到該串口上,因此你需要一個(gè)串口線(xiàn)與STM32和電腦相連,這樣就可以看到printf了。
評(píng)論