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

[复制链接]
7071|7
 楼主| 拨云人 发表于 2015-5-5 11:14 | 显示全部楼层 |阅读模式
本帖最后由 拨云人 于 2015-5-5 11:43 编辑

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


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

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

  12. PUTCHAR_PROTOTYPE
  13. {
  14.     HAL_UART_Transmit(&huart1, (uint8_t*)ch, 1, 0xFFFF);   
  15.     return ch;   
  16. }

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 -------------------------------- ...

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

  5.   /* Configure the system clock */
  6.   SystemClock_Config();

  7.   /* Initialize all configured peripherals */
  8.   MX_GPIO_Init();
  9.   MX_USART1_UART_Init();
  10.    
  11.   HAL_UART_Transmit(&huart1, (uint8_t*)aTxBuffer, sizeof(aTxBuffer), 0xFFFF);
  12.    
  13.    printf("hello              \n");
  14.    
  15.   while (1)
  16.   {  
  17.       
  18.   }
  19. }
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"); 打印出来的是乱码.
晓枫VS枯叶 发表于 2015-5-5 16:30 | 显示全部楼层
  1. PUTCHAR_PROTOTYPE
  2. {      
  3.         while((USART1->ISR&0X40)==0);//循环发送,直到发送完毕   
  4.     USART1->TDR = (uint8_t) ch;      
  5.         return ch;
  6. }

直接使用这个!
ningwuli 发表于 2016-4-20 08:47 | 显示全部楼层
不管用啊
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;

}

我用的这个,可以用
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

粉丝
快速回复 在线客服 返回列表 返回顶部