2687|1

255

帖子

0

TA的资源

一粒金砂(高级)

楼主
 

STM32 如何从新定向printf到串口 (ST例程) [复制链接]

  1. /**
  2.   ******************************************************************************
  3.   * @file    USART/Printf/main.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    08-April-2011
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************
  20.   */

  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x.h"
  23. #include "stm32_eval.h"
  24. #include <stdio.h>

  25. /** @addtogroup STM32F10x_StdPeriph_Examples
  26.   * @{
  27.   */

  28. /** @addtogroup USART_Printf
  29.   * @{
  30.   */

  31. /* Private typedef -----------------------------------------------------------*/
  32. /* Private define ------------------------------------------------------------*/
  33. /* Private macro -------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. USART_InitTypeDef USART_InitStructure;
  36. int16_t        i=100;
  37. int8_t  str[]="HELLO 北京";
  38. float   f=3.14;
  39. /* Private function prototypes -----------------------------------------------*/

  40. #ifdef __GNUC__
  41.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  42.      set to 'Yes') calls __io_putchar() */
  43.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  44. #else
  45.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  46. #endif /* __GNUC__ */
  47.   
  48. /* Private functions ---------------------------------------------------------*/

  49. /**
  50.   * @brief  Main program
  51.   * @param  None
  52.   * @retval None
  53.   */
  54. int main(void)
  55. {
  56.   /*!< At this stage the microcontroller clock setting is already configured,
  57.        this is done through SystemInit() function which is called from startup
  58.        file (startup_stm32f10x_xx.s) before to branch to application main.
  59.        To reconfigure the default setting of SystemInit() function, refer to
  60.        system_stm32f10x.c file
  61.      */     
  62.       
  63.   /* USARTx configured as follow:
  64.         - BaudRate = 115200 baud  
  65.         - Word Length = 8 Bits
  66.         - One Stop Bit
  67.         - No parity
  68.         - Hardware flow control disabled (RTS and CTS signals)
  69.         - Receive and transmit enabled
  70.   */
  71.   USART_InitStructure.USART_BaudRate = 19200;
  72.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  73.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  74.   USART_InitStructure.USART_Parity = USART_Parity_No;
  75.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  76.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  77.   STM_EVAL_COMInit(COM1, &USART_InitStructure);

  78.   /* Output a message on Hyperterminal using printf function */
  79.   printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
  80.         printf("\n\r%d\n\r",i);
  81.         printf("\n\r%s\n\r",str);
  82.         printf("\n\r%3.2f\n\r",f);  //显示 小数点后两位
  83.   while (1)
  84.   {
  85.   }
  86. }

  87. /**
  88.   * @brief  Retargets the C library printf function to the USART.
  89.   * @param  None
  90.   * @retval None
  91.   */
  92. PUTCHAR_PROTOTYPE
  93. {
  94.   /* Place your implementation of fputc here */
  95.   /* e.g. write a character to the USART */
  96.   USART_SendData(EVAL_COM1, (uint8_t) ch);

  97.   /* Loop until the end of transmission */
  98.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  99.   {}

  100.   return ch;
  101. }

  102. #ifdef  USE_FULL_ASSERT

  103. /**
  104.   * @brief  Reports the name of the source file and the source line number
  105.   *         where the assert_param error has occurred.
  106.   * @param  file: pointer to the source file name
  107.   * @param  line: assert_param error line source number
  108.   * @retval None
  109.   */
  110. void assert_failed(uint8_t* file, uint32_t line)
  111. {
  112.   /* User can add his own implementation to report the file name and line number,
  113.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  114.   /* Infinite loop */
  115.   while (1)
  116.   {
  117.   }
  118. }

  119. #endif

  120. /**
  121.   * @}
  122.   */

  123. /**
  124.   * @}
  125.   */

  126. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码
此帖出自stm32/stm8论坛
点赞 关注
 

回复
举报

255

帖子

0

TA的资源

一粒金砂(高级)

沙发
 
芯片型号:STM32F103RC
串口:      USART1

无标题.png (25.87 KB, 下载次数: 0)

实验效果

实验效果
此帖出自stm32/stm8论坛
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表