6步教你在STM32程序中添加 printf函数

来源:本站
导读:目前正在解读《6步教你在STM32程序中添加 printf函数》的相关信息,《6步教你在STM32程序中添加 printf函数》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《6步教你在STM32程序中添加 printf函数》的详细说明。
简介:6步教你在STM32程序中添加 printf函数

前提是你有一个完整的keil工程 比如ADC的

调试的时候很多时候用到串口 这里教你怎么样使用Printf 函数

在程序中添加Printf

1,

#include <stdio.h>

2,

/* Private functions ---------------------------------------------------------*/ 下添加

void USART_Configuration(void);

#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__ */

3,添加如下2个函数 usart配置 和 重定向 C库的printf函数

void USART_Configuration()

{

/* USARTx configured as follow:

- BaudRate = 9600 baud

- Word Length = 8 Bits

- One Stop Bit

- No parity

- Hardware flow control disabled (RTS and CTS signals)

- Receive and transmit enabled

*/

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure USART1 Tx (PA9) as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Rx (PA10) as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;

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

/* Enable USART1 */

USART_Cmd(USART1, ENABLE);

}

/**

* @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;

}

4,

void RCC_Configuration(void) 添加

/* Enable GPIOA and USART1 clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);

5,

STM32F10x.CONF.H

去掉/* #include "stm32f10x_usart.h" */ 的注释

6,

在Main()中添加

void USART_Configuration()

然后就可以在main()调用

printf("The is a example!" );

printf("%s%c%c%c%c%c%s",

"#**",

Value/256,Value%256,

'&',

Value_2/256,Value_2%256,

"**%");

之类的输出函数

提醒:《6步教你在STM32程序中添加 printf函数》最后刷新时间 2024-03-14 01:14:03,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《6步教你在STM32程序中添加 printf函数》该内容的真实性请自行鉴别。