打印
[STM32F0]

如何在 stm32cube 工程基础上使用 printf 函数?

[复制链接]
6561|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 拨云人 于 2015-5-5 11:43 编辑

之前用的ST官方外设库来开发时,使用 printf 没有问题,但是转到 stm32cube 上就出问题了呢,我的步骤是这样的:


1.  使用 STM32Cube_FW_F0_V1.x.x 自动生成了一段初始化 UART 的MDK 工程。
2.  在  main.c 文件中 添加如下代码(已省却注释):
#include "stdio.h"
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* Buffer used for transmission */
uint8_t aTxBuffer[] = "send by HAL_UART_Transmit() ";

PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(&huart1, (uint8_t*)ch, 1, 0xFFFF);   
    return ch;   
}

3.  然后在主函数中使用 printf(“hello...\n”) 发现打印出来的错误.
4.  直接使用 HAL_UART_Transmit(&huart1 , (uint8_t *)&aTxBuffer, sizeof(aTxBuffer), 0xFFFF);   打印正确.

请问有没有人遇到过这样的情况,使用STM32Cube的时候该如何才能使用 printf 函数呢 ?




沙发
xiaocui0475| | 2015-5-5 11:56 | 只看该作者
具體錯誤內容是什麼? 我在主函數前面添加如下代碼,  正常
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"

/* USER CODE BEGIN Includes */
#include "stdio.h"
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}

使用特权

评论回复
板凳
拨云人|  楼主 | 2015-5-5 12:32 | 只看该作者
xiaocui0475 发表于 2015-5-5 11:56
具體錯誤內容是什麼? 我在主函數前面添加如下代碼,  正常
/* Includes -------------------------------- ...

我在主函数中这样输出:
int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
   
  HAL_UART_Transmit(&huart1, (uint8_t*)aTxBuffer, sizeof(aTxBuffer), 0xFFFF);
   
   printf("hello              \n");
   
  while (1)
  {  
      
  }
}

360截图20150505122939789.jpg (68.56 KB )

360截图20150505122939789.jpg

使用特权

评论回复
地板
拨云人|  楼主 | 2015-5-5 12:34 | 只看该作者
xiaocui0475 发表于 2015-5-5 11:56
具體錯誤內容是什麼? 我在主函數前面添加如下代碼,  正常
/* Includes -------------------------------- ...

可以看到 在 printf("hello              \n"); 函数之前的 HAL_UART_Transmit(&huart1, (uint8_t*)aTxBuffer, sizeof(aTxBuffer), 0xFFFF); 是被正确执行了的,并且得到了预期的打印效果,但是 printf("hello              \n"); 打印出来的是乱码.

使用特权

评论回复
5
晓枫VS枯叶| | 2015-5-5 16:30 | 只看该作者
PUTCHAR_PROTOTYPE
{      
        while((USART1->ISR&0X40)==0);//循环发送,直到发送完毕   
    USART1->TDR = (uint8_t) ch;      
        return ch;
}

直接使用这个!

使用特权

评论回复
6
ningwuli| | 2016-4-20 08:47 | 只看该作者
不管用啊

使用特权

评论回复
7
LUOYANGL| | 2017-2-9 12:18 | 只看该作者
一、移植printf()函数,重定向C库函数printf到USART1

int fputc(int ch, FILE *f)

{

      HAL_UART_Transmit(&huart1, (uint8_t *)&ch,1, 0xFFFF);

      return ch;

}
二、移植scanf()函数,重定向C库函数scanf到USART1

int fgetc(FILE *f)

{

      uint8_t  ch;

HAL_UART_Receive(&huart1,(uint8_t *)&ch, 1, 0xFFFF);

return  ch;

}

我用的这个,可以用

使用特权

评论回复
8
LUOYANGL| | 2017-2-9 12:19 | 只看该作者
LUOYANGL 发表于 2017-2-9 12:18
一、移植printf()函数,重定向C库函数printf到USART1

int fputc(int ch, FILE *f)

我放在main函数前面的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

34

帖子

0

粉丝