STM32+Keil 如何使用printf函数?

本文介绍如何在STM32微控制器上使用printf函数通过串口输出信息,提供了配置USART和重定向printf输出的具体实现方法。

【lanmanck原创】

Keil不支持Host-semi机制,即不支持直接在IDE打印字符串。

那么只能通过程序向硬件串口发数据了,这样调用的时候用自定义的函数即可,也很方便,例如:

void send_char_to_usart(unsigned char c){}

但是可否直接使用printf函数呢?毕竟人家都做好了,我们给他定一个打印输出的接口就可以了,答案是肯定的,看ST的官方源码:

/**
  ******************************************************************************
  * @file    Lib_DEBUG/Lib_DEBUG_Example/main.c 
  * @author  MCD Application Team
  * @version V1.1.1
  * @date    13-April-2012
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx.h"
#include "stm32l1xx_ip_dbg.h"
#include <stdio.h>

#ifdef USE_STM32L152D_EVAL
#include "stm32l152d_eval.h"
#else
#include "stm32l152_eval.h"
#endif


/** @addtogroup STM32L1xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup Lib_DEBUG_Example
  * @{
  */ 

/* 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 /* __GNUC__ */

/* 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_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */     
       
  GPIO_InitTypeDef GPIOA_InitStructure;

  /* 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);
 
  /* Initialize all peripherals pointers */
  IP_Debug();
  
  printf("\r\n STM32l1xx Firmware Library compiled with FULL ASSERT function... \n\r");
  printf("...Run-time checking enabled  \n\r");

  /* Simulate wrong parameter passed to library function ---------------------*/
  /* To enable SPI1 clock, RCC_APB2PeriphClockCmd function must be used and
     not RCC_APB1PeriphClockCmd */
  RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  
  /* Some member of GPIOA_InitStructure structure are not initialized */
  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)
  {
  }

}

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

  printf("\n\r Wrong parameter value detected on\r\n");
  printf("       file  %s\r\n", file);
  printf("       line  %d\r\n", line);
    
  /* Infinite loop */
  /* while (1)
  {
  } */
}
#endif

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

/**
  * @}
  */ 

/**
  * @}
  */ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

该例子就是把COM1作为输出口,把printf的数据打印到该串口上,因此你需要一个串口线与STM32和电脑相连,这样就可以看到printf了。


### STM32 HAL库下将 `printf` 函数重定向到串口的教程 在 KeilSTM32 HAL 库环境下,将 `printf` 函数重定向到串口是一种常见的调试手段。以下是实现这一功能的具体方法: #### 1. 配置 STM32CubeMX 使用 STM32CubeMX 配置项目时,需要确保以下设置正确: - 选择一个 USART 或 UART 外设(如 USART1)。 - 在引脚配置中,将 TX 和 RX 引脚分配给相应的 GPIO 引脚[^1]。 - 确保时钟树配置正确,并启用相关外设的时钟。 #### 2. 初始化代码生成 在生成初始化代码后,检查 `MX_USART1_UART_Init()` 函数是否已正确配置串口参数(波特率、数据位、停止位等)。如果未配置,可以在 `stm32fxxx_hal_msp.c` 文件中调整默认值。 #### 3. 编写 `fputc` 函数 为了使 `printf` 函数能够通过串口输出数据,需要重写标准库中的 `fputc` 函数。该函数通常位于项目的源文件中(如 `main.c` 或单独的文件中)。以下是实现代码: ```c #include "stdio.h" int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 将字符发送到USART1 return ch; } ``` 此函数的作用是将每个字符通过 `HAL_UART_Transmit` 函数发送到指定的串口(此处为 `huart1`)。注意,`huart1` 是由 STM32CubeMX 自动生成的句柄名称,具体名称可能因项目而异[^4]。 #### 4. 测试 `printf` 功能 在主函数中调用 `printf` 函数以验证其是否正常工作。例如: ```c int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); while (1) { printf("Hello World from STM32!\r\n"); // 输出测试信息 HAL_Delay(1000); } } ``` 运行程序后,打开串口调试助手(如 SecureCRT 或 Putty),设置正确的波特率和端口号,即可看到串口输出的信息[^5]。 #### 5. 可选:实现 `fgetc` 函数 如果需要从串口接收数据,可以实现 `fgetc` 函数: ```c int fgetc(FILE *f) { uint8_t ch = 0; HAL_UART_Receive(&huart1, &ch, 1, HAL_MAX_DELAY); // 从USART1接收字符 return ch; } ``` 此函数允许通过 `scanf` 或其他输入函数从串口读取数据。 #### 注意事项 - 确保在 Keil 中启用了 `MicroLIB` 支持。如果没有启用,可能会导致链接错误或功能异常[^2]。 - 如果串口无法正常收发数据,请检查硬件连接是否正确(如 RX 和 TX 是否接反)[^3]。 - 调试时建议使用较慢的波特率(如 9600),以减少误码率。 ### 示例代码 以下是一个完整的示例代码,展示了如何实现 `printf` 重定向并测试其功能: ```c #include "main.h" #include "stdio.h" void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); while (1) { printf("STM32 is running...\r\n"); HAL_Delay(1000); } } static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值