本实验所用的硬件:
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);这个函数是官方为测试板写的一个标准初始化串口的函数,所以咱们也不能用,当