<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > STM32 printf函數詳解

STM32 printf函數詳解

作者: 時(shí)間:2016-11-21 來(lái)源:網(wǎng)絡(luò ) 收藏
本實(shí)驗所用的硬件:STM32F103RTB6

實(shí)驗所用的晶振: 8M

本文引用地址:http://dyxdggzs.com/article/201611/319241.htm

實(shí)驗所用的ST官方庫:3.5版

C語(yǔ)言中的標準庫中所用的標準輸出函數,默認的輸出設備是顯示器,要實(shí)現串口或LCD的輸出,必須重新定義標準庫函數里與輸出函數相關(guān)的函數。

1.下面首先介紹怎么根據官方3.5庫里面的標準例程“printf”修改成自己的“printf”工程:

下邊是官方提供的例程:

//Includes ------------------------------------------------------------------
#include "stm32f10x.h"
#include "stm32_eval.h"
#include

//@addtogroup STM32F10x_StdPeriph_Examples
// @{
//

//@addtogroup USART_Printf
//@{
//

//Private typedef -----------------------------------------------------------
//Private define ------------------------------------------------------------
//Private macro -------------------------------------------------------------
//Private variables ---------------------------------------------------------
USART_InitTypeDef USART_InitStructure;

//Private function prototypes -----------------------------------------------

#ifdef __GNUC__
//With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
// set to Yes) calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

// Private functions ---------------------------------------------------------


// @brief Main program
//@param None
//@retval None


int main(void)
{
//At this stage the microcontroller clock setting is already configured,
//this is done through SystemInit() function which is called from startup
//file (startup_stm32f10x_xx.s) before to branch to application main.
//To reconfigure the default setting of SystemInit() function, refer to
//system_stm32f10x.c file


//USARTx configured as follow:
// - BaudRate = 115200 baud
// - Word Length = 8 Bits
// - One Stop Bit
// - No parity
// - Hardware flow control disabled (RTS and CTS signals)
// - Receive and transmit enabled
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);

// Output a message on Hyperterminal using printf function
printf("nrUSART Printf Example: retarget the C library printf function to the USARTnr");

while (1)
{
}
}
// @brief Retargets the C library printf function to the USART.
// @param None
//@retval None
//
PUTCHAR_PROTOTYPE
{
// Place your implementation of fputc here
// e.g. write a character to the USART
USART_SendData(EVAL_COM1, (uint8_t) ch);

// Loop until the end of transmission
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}

return ch;
}

#ifdef USE_FULL_ASSERT

//@brief Reports the name of the source file and the source line number
// where the assert_param error has occurred.
//@param file: pointer to the source file name
// @param line: assert_param error line source number
// @retval None
//
void assert_failed(uint8_t* file, uint32_t line)
{
//User can add his own implementation to report the file name and line number,
// ex: printf("Wrong parameters value: file %s on line %drn", file, line) */

// Infinite loop */
while (1)
{
}
}

#endif

上邊用紅色字體標記出來(lái)的,表示是要修改的,首先#include "stm32_eval.h"是官方提供的測試板上的頭文件,而咱們要移植到自己的實(shí)驗板上,所以不能用,負責編譯肯定出錯誤,在這里直接屏蔽就行了。STM_EVAL_COMInit(COM1, &USART_InitStructure);這個(gè)函數是官方為測試板寫(xiě)的一個(gè)標準初始化串口的函數,所以咱們也不能用,當然你可以把這個(gè)函數復制到自己的main函數里,加以修改,這樣配置起串口也就相對方便了,在這里咱們就不這樣做了。因為咱們是自己新建工程,所以不能調用官方測試板的初始化串口的函數,所以必須重新寫(xiě)一個(gè)函數,按下面的方法進(jìn)行:

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);

這是標準例程里的,它實(shí)現了串口的初始化,下邊是我自己寫(xiě)的函數,也是初始化串口,所以用下面的函數替換上面的函數就ok了,函數為:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽輸出-TX
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入-RX
GPIO_Init(GPIOA, &GPIO_InitStructure);
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;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);

上面程序就完成了對stm32f103rbt6串口1的初始化。

接下來(lái)就是對USART_SendData(EVAL_COM1, (uint8_t) ch),while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)這兩個(gè)函數的修改,其中EVAL_COM1是官方為自己的測試板寫(xiě)的庫函數定義的USART1,所以這里咱們直接用USATT1替換EVAL_COM1就行了,做完這些后編譯整個(gè)工程,整個(gè)工程就可以編譯過(guò)去了,不會(huì )出現錯誤,接下來(lái)的任務(wù)就是下載到實(shí)驗板上做測試了。

2.下載到自己的實(shí)驗板上,做測試

根據上面的修改,工程編譯過(guò)去了,也沒(méi)有出現任何錯誤,但下載到實(shí)驗板上后,發(fā)現printf函數根本打印不出來(lái)想要打印出來(lái)的信息,后來(lái)我把所有的printf函數都屏蔽了,然后自己調用USART_SendData()這個(gè)函數,看到底是配置有問(wèn)題還是其他問(wèn)題,結果在串口調試助手里正常的打印出了想要的字符,到這里,大家應該都能猜到不是程序的問(wèn)題,而是軟件的配置問(wèn)題,經(jīng)過(guò)在網(wǎng)上的各種搜索后終于找到了答案。的確是配置問(wèn)題,打開(kāi)keil軟件的target



看到上面用紅線(xiàn)標出來(lái)的位置了吧,默認情況下,keil軟件是沒(méi)有把Use MicroLIB這個(gè)選項勾上的,咱們修改的程序之所以沒(méi)有打印出東西,就是應該勾上這個(gè)選項,勾上后,在進(jìn)行一次編譯,然后下載到實(shí)驗板上,一切就正確了。

3.對標準printf函數的修改如下:

//Private function prototypes -----------------------------------------------

#ifdef __GNUC__
// With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
// set to Yes) calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif // __GNUC__


// @brief Retargets the C library printf function to the USART.
//@param None
// @retval None
//
PUTCHAR_PROTOTYPE
{
//Place your implementation of fputc here
// e.g. write a character to the USART
USART_SendData(USART1, (uint8_t) ch);

// Loop until the end of transmission
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}

return ch;
}

這一段函數就是把printf函數輸出到串口,需要將fputc輸出給串口,也就是重新定義。

以上就是對printf函數在調試STM32配置成串口printf函數的這個(gè)過(guò)程,希望對大家有所幫助!



關(guān)鍵詞: STM32printf函

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>