您的位置 首页 新能源

STM32 printf函数详解

本实验所用的硬件:STM32F103RTB6实验所用的晶振:8M实验所用的ST官方库:35版C语言中的标准库中所用的标准输出函数,默认的输出设备是

本试验所用的硬件:STM32F103RTB6

试验所用的晶振: 8M

试验所用的ST官方库:3.5版

C语言中的规范库中所用的规范输出函数,默许的输出设备是显示器,要完成串口或LCD的输出,有必要从头界说规范库函数里与输出函数相关的函数。

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(“\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r”);

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 %d\r\n”, file, line) */

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

#endif

上边用赤色字体符号出来的,表明是要修正的,首要#include “stm32_eval.h”是官方供给的测验板上的头文件,而我们要移植到自己的试验板上,所以不能用,担任编译必定出过错,在这儿直接屏蔽就行了。STM_EVAL_COMInit(COM1, &USART_InitStructure);这个函数是官方为测验板写的一个规范初始化串口的函数,所以我们也不能用,当然你能够把这个函数复制到自己的main函数里,加以修正,这样装备起串口也就相对方便了,在这儿我们就不这样做了。由于我们是自己新建工程,所以不能调用官方测验板的初始化串口的函数,所以有必要从头写一个函数,按下面的办法进行:

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

这是规范例程里的,它完成了串口的初始化,下边是我自己写的函数,也是初始化串口,所以用下面的函数替换上面的函数就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的初始化。

接下来便是对USART_SendData(EVAL_COM1, (uint8_t) ch),while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)这两个函数的修正,其间EVAL_COM1是官方为自己的测验板写的库函数界说的USART1,所以这儿我们直接用USATT1替换EVAL_COM1就行了,做完这些后编译整个工程,整个工程就能够编译过去了,不会呈现过错,接下来的使命便是下载到试验板上做测验了。

2.下载到自己的试验板上,做测验

依据上面的修正,工程编译过去了,也没有呈现任何过错,但下载到试验板上后,发现printf函数底子打印不出来想要打印出来的信息,后来我把全部的printf函数都屏蔽了,然后自己调用USART_SendData()这个函数,看到底是装备有问题仍是其他问题,结果在串口调试助手里正常的打印出了想要的字符,到这儿,我们应该都能猜到不是程序的问题,而是软件的装备问题,通过在网上的各种查找后总算找到了答案。的确是装备问题,翻开keil软件的target

看到上面用红线标出来的方位了吧,默许情况下,keil软件是没有把Use MicroLIB这个选项勾上的,我们修正的程序之所以没有打印出东西,便是应该勾上这个选项,勾上后,在进行一次编译,然后下载到试验板上,全部就正确了。

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函数的这个进程,期望对我们有所协助!

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/xinnengyuan/262892.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部