主函数文件,请直接关注自己写上去的代码:

直接看43行代码:#include "stdio.h"//要添加这个头文件

还有97行到112行:实现用HAL库函数和printf函数发送数据

  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. ** This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * COPYRIGHT(c) 2017 STMicroelectronics
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"
  41.  
  42. /* USER CODE BEGIN Includes */
  43. #include "stdio.h"//添加这个头文件
  44. /* USER CODE END Includes */
  45.  
  46. /* Private variables ---------------------------------------------------------*/
  47. UART_HandleTypeDef huart1;
  48.  
  49. /* USER CODE BEGIN PV */
  50. /* Private variables ---------------------------------------------------------*/
  51.  
  52. /* USER CODE END PV */
  53.  
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. static void MX_GPIO_Init(void);
  57. static void MX_USART1_UART_Init(void);
  58.  
  59. /* USER CODE BEGIN PFP */
  60. /* Private function prototypes -----------------------------------------------*/
  61.  
  62. /* USER CODE END PFP */
  63.  
  64. /* USER CODE BEGIN 0 */
  65.  
  66. /* USER CODE END 0 */
  67.  
  68. int main(void)
  69. {
  70.  
  71. /* USER CODE BEGIN 1 */
  72.  
  73. /* USER CODE END 1 */
  74.  
  75. /* MCU Configuration----------------------------------------------------------*/
  76.  
  77. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78. HAL_Init();
  79.  
  80. /* USER CODE BEGIN Init */
  81.  
  82. /* USER CODE END Init */
  83.  
  84. /* Configure the system clock */
  85. SystemClock_Config();
  86.  
  87. /* USER CODE BEGIN SysInit */
  88.  
  89. /* USER CODE END SysInit */
  90.  
  91. /* Initialize all configured peripherals */
  92. MX_GPIO_Init();
  93. MX_USART1_UART_Init();
  94.  
  95. /* USER CODE BEGIN 2 */
  96.  
  97. HAL_UART_Transmit (&huart1,"使用HAL库函数发送数据\n",,);
  98. printf("\n使用函数Printf函数发送数据\n");
  99.  
  100. char t;
  101. while()
  102. {
  103. scanf("%c",&t);//接受的t的数值
  104. if(t=='')
  105. {
  106. HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,);//给低电平,红灯亮
  107. }
  108. if(t=='')
  109. {
  110. HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,);//给高电平,红灯灭
  111. }
  112. }
  113.  
  114. /* USER CODE END 2 */
  115.  
  116. /* Infinite loop */
  117. /* USER CODE BEGIN WHILE */
  118. while ()
  119. {
  120. /* USER CODE END WHILE */
  121.  
  122. /* USER CODE BEGIN 3 */
  123.  
  124. }
  125. /* USER CODE END 3 */
  126.  
  127. }
  128.  
  129. /** System Clock Configuration
  130. */
  131. void SystemClock_Config(void)
  132. {
  133.  
  134. RCC_OscInitTypeDef RCC_OscInitStruct;
  135. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  136.  
  137. /**Initializes the CPU, AHB and APB busses clocks
  138. */
  139. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  140. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  141. RCC_OscInitStruct.HSICalibrationValue = ;
  142. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  143. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  144. {
  145. _Error_Handler(__FILE__, __LINE__);
  146. }
  147.  
  148. /**Initializes the CPU, AHB and APB busses clocks
  149. */
  150. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  151. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  152. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  153. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  154. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  155. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  156.  
  157. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  158. {
  159. _Error_Handler(__FILE__, __LINE__);
  160. }
  161.  
  162. /**Configure the Systick interrupt time
  163. */
  164. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/);
  165.  
  166. /**Configure the Systick
  167. */
  168. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  169.  
  170. /* SysTick_IRQn interrupt configuration */
  171. HAL_NVIC_SetPriority(SysTick_IRQn, , );
  172. }
  173.  
  174. /* USART1 init function */
  175. static void MX_USART1_UART_Init(void)
  176. {
  177.  
  178. huart1.Instance = USART1;
  179. huart1.Init.BaudRate = ;
  180. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  181. huart1.Init.StopBits = UART_STOPBITS_1;
  182. huart1.Init.Parity = UART_PARITY_NONE;
  183. huart1.Init.Mode = UART_MODE_TX_RX;
  184. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  185. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  186. if (HAL_UART_Init(&huart1) != HAL_OK)
  187. {
  188. _Error_Handler(__FILE__, __LINE__);
  189. }
  190.  
  191. }
  192.  
  193. /** Configure pins as
  194. * Analog
  195. * Input
  196. * Output
  197. * EVENT_OUT
  198. * EXTI
  199. */
  200. static void MX_GPIO_Init(void)
  201. {
  202.  
  203. GPIO_InitTypeDef GPIO_InitStruct;
  204.  
  205. /* GPIO Ports Clock Enable */
  206. __HAL_RCC_GPIOA_CLK_ENABLE();
  207. __HAL_RCC_GPIOB_CLK_ENABLE();
  208.  
  209. /*Configure GPIO pin Output Level */
  210. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
  211.  
  212. /*Configure GPIO pin : PB5 */
  213. GPIO_InitStruct.Pin = GPIO_PIN_5;
  214. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  215. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  216. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  217.  
  218. }
  219.  
  220. /* USER CODE BEGIN 4 */
  221.  
  222. /* USER CODE END 4 */
  223.  
  224. /**
  225. * @brief This function is executed in case of error occurrence.
  226. * @param None
  227. * @retval None
  228. */
  229. void _Error_Handler(char * file, int line)
  230. {
  231. /* USER CODE BEGIN Error_Handler_Debug */
  232. /* User can add his own implementation to report the HAL error return state */
  233. while()
  234. {
  235. }
  236. /* USER CODE END Error_Handler_Debug */
  237. }
  238.  
  239. #ifdef USE_FULL_ASSERT
  240.  
  241. /**
  242. * @brief Reports the name of the source file and the source line number
  243. * where the assert_param error has occurred.
  244. * @param file: pointer to the source file name
  245. * @param line: assert_param error line source number
  246. * @retval None
  247. */
  248. void assert_failed(uint8_t* file, uint32_t line)
  249. {
  250. /* USER CODE BEGIN 6 */
  251. /* User can add his own implementation to report the file name and line number,
  252. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  253. /* USER CODE END 6 */
  254.  
  255. }
  256.  
  257. #endif
  258.  
  259. /**
  260. * @}
  261. */
  262.  
  263. /**
  264. * @}
  265. */
  266.  
  267. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

新建一个文件,随便命名,这是核心内容,修改标准库函数的,有点难理解!!!。

  1. #include "stdio.h"
  2. #include "stm32f1xx_hal.h"
  3.  
  4. //这个变量是其他.c文件定义的
  5. extern UART_HandleTypeDef huart1;
  6. uint8_t ch;
  7. uint8_t ch_r;
  8. //重写这个函数,重定向printf函数到串口,意思就是说printf直接输出到串口,其默认输出到控制台的
  9. /*fputc*/
  10. int fputc(int c, FILE * f)
  11. {
  12. ch=c;
  13. HAL_UART_Transmit(&huart1,&ch,,);//发送串口
  14. return c;
  15. }
  16.  
  17. //重定向scanf函数到串口 意思就是说接受串口发过来的数据,其默认是接受控制台的数据
  18. /*fgetc*/
  19. int fgetc(FILE * F)
  20. {
  21. HAL_UART_Receive (&huart1,&ch_r,,0xffff);//接收
  22. return ch_r;
  23. }

修改HAL标准库用printf函数发送数据直接输出的更多相关文章

  1. 修改Java标准库源码

    以下是摘抄,实际操作没有测试   先前我曾提到,原本想借由“改动Java标准库源码”来测知Class object的生成,但由于其ctor原始设计为private,也就是说不可能透过这个管道生成Cla ...

  2. 为什么 Go 标准库中有些函数只有签名,没有函数体?

    如果你看过 Go 语言标准库,应该有见到过,有一些函数只有签名,没有函数体.你有没有感觉到很奇怪?这到底是怎么回事?我们自己可以这么做吗?本文就来解密它. 首先,函数肯定得有实现,没有函数体,一定是在 ...

  3. python标准库总的random函数用法

    Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等.random中的一些重要函数的用法:1 ).random() 返回0& ...

  4. golang标准库中有些函数只有签名没有函数体是怎么回事?

  5. Python 通过pickle标准库加载和保存数据对象

    import pickle with open('mydata.pickle','wb') as mysavedata: pickle.dump([1,2,'three'], mysavedata) ...

  6. STM32 的 printf() 函数串口重定向(HAL库标准库都适用)

    1.建立工程 2.核心:添加新文件usar_fputc.c (名字随便自己命名),把文件添加到项目中去 #include "stdio.h" #include "stm3 ...

  7. C++ Pirmer : 第十四章 : 重载运算符与类型转换之函数调用运算符与标准库的定义的函数对象

    函数调用运算符 struct test { int operator()(int val) const { return (i > 0 ? i : -i); } }; 所谓的函数调用就是一个类重 ...

  8. Lua 中的string库(字符串函数库)总结

    (字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...

  9. STM32 printf()函数和scanf()函数重定向到串口

    STM32 printf()函数和scanf()函数重定向到串口 printf()函数和scanf()函数重定向 在学习STM32的时候,常常需要用串口来测试代码的正确与否,这时候就要要用到print ...

随机推荐

  1. Allegro PCB Design GXL (legacy) 将指定的层导出为DXF

    Allegro PCB Design GXL (legacy) version 16.6-2015 1.菜单:Display > Color/Visibility... 2.打开Color Di ...

  2. ajax-简单参数方法实现阴影效果

    注: 简单参数 (按照参数的数量和位置传递参数) 使用时按照位置.数量传递 shadow.js函数 //简单参数实现方式/** slices:阴影* opacity:透明度* zIndex:层级* * ...

  3. 论文阅读笔记二十:LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation(CVPR2017)

    源文网址:https://arxiv.org/abs/1707.03718 tensorflow代码:https://github.com/luofan18/linknet-tensorflow 基于 ...

  4. mybatis 遍历map;

    mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...

  5. Oracle数据库表索引失效,解决办法:修改Oracle数据库优化器模式

    ALTER SYSTEM SET OPTIMIZER_MODE=RULE scope=both; 其他可以选择的模式还有ALL_ROWS/CHOOSE/FIRST_ROWS/ALL_ROWS. 应用系 ...

  6. linux系统(CentOS7)虚拟机上安装oracle 11g,解决oracle图形界面卡住无法点击next问题

    https://www.cnblogs.com/nichoc/p/6416475.html

  7. java运算符-算数、赋值、比较

    1.算术运算符 运算符是用来计算数据的符号.数据可以是常量,也可以是变量.被运算符操作的数我们称为操作数. 运算符 运算规则 范例 结果 + 正号 +3 3 + 加 2+3 5 + 连接字符串 “中” ...

  8. 【bzoj2131】免费的馅饼 dp+树状数组

    题解: 昨天好像做了个几乎一模一样的题目 按照ti排序 |p[i]-p[j]|<=2*(t[i]-t[j]) 然后去绝对值变为三维偏序 发现后两个式子可以推出ti<tj 所以就变成二维偏序 ...

  9. Oier们的镜子(mirror)

    题解: 这题真是把我坑的很惨.. 题目看了很久才看懂.. 然后刚开始又没看见每个只能匹配一个这种条件 #include <bits/stdc++.h> using namespace st ...

  10. 3897: Power

    题解: 首先很贪心的选择 有最大的我们一定会用最大的 然后可以将序列分割.. 就变成了一道模拟题了.. 每个状态记录(h,t,h-have,t-need) 注意一下细节就可以了 代码: #includ ...