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

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

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

 /**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
** This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h" /* USER CODE BEGIN Includes */
#include "stdio.h"//添加这个头文件
/* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ int main(void)
{ /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */
SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Transmit (&huart1,"使用HAL库函数发送数据\n",,);
printf("\n使用函数Printf函数发送数据\n"); char t;
while()
{
scanf("%c",&t);//接受的t的数值
if(t=='')
{
HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,);//给低电平,红灯亮
}
if(t=='')
{
HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,);//给高电平,红灯灭
}
} /* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
while ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ }
/* USER CODE END 3 */ } /** System Clock Configuration
*/
void SystemClock_Config(void)
{ RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct; /**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = ;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
} /**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
} /**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/); /**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, , );
} /* USART1 init function */
static void MX_USART1_UART_Init(void)
{ huart1.Instance = USART1;
huart1.Init.BaudRate = ;
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(__FILE__, __LINE__);
} } /** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{ GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); /*Configure GPIO pin : PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void _Error_Handler(char * file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while()
{
}
/* USER CODE END Error_Handler_Debug */
} #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 CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */ } #endif /**
* @}
*/ /**
* @}
*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

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

 #include "stdio.h"
#include "stm32f1xx_hal.h" //这个变量是其他.c文件定义的
extern UART_HandleTypeDef huart1;
uint8_t ch;
uint8_t ch_r;
//重写这个函数,重定向printf函数到串口,意思就是说printf直接输出到串口,其默认输出到控制台的
/*fputc*/
int fputc(int c, FILE * f)
{
ch=c;
HAL_UART_Transmit(&huart1,&ch,,);//发送串口
return c;
} //重定向scanf函数到串口 意思就是说接受串口发过来的数据,其默认是接受控制台的数据
/*fgetc*/
int fgetc(FILE * F)
{
HAL_UART_Receive (&huart1,&ch_r,,0xffff);//接收
return ch_r;
}

修改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. python面向对象三大特性之封装

    一. 概述 定义:隐藏对象的属性和实现细节,仅对外提供公共访问方式 封装的原则:把不需要对外提供的内容都隐藏起来,提供公共的方法访问这些隐藏属性 二.封装手段 使用双下划线将属性和方法隐藏起来 cla ...

  2. Two Sum【LeetCode】

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. POJ 1064 Cable master (二分法+精度控制)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65358   Accepted: 13453 De ...

  4. github.com加速节点

    github.com加速节点 192.30.253.118 github.com192.30.253.119 github.com93.46.8.89 github.com

  5. Leetcode刷题第20天

    一.找树左下角的值 题目:513. Find Bottom Left Tree Value C++ Soution 1: /** * Definition for a binary tree node ...

  6. 【专栏学习】APM——异步编程模型(.NET不推荐)

    (1)learning hard C#学习笔记 异步1:<learning hard C#学习笔记>读书笔记(20)异步编程 (2)<C# 4.0 图解教程> 22.4 异步编 ...

  7. 执行Spark运行在yarn上的命令报错 spark-shell --master yarn-client

    1.执行Spark运行在yarn上的命令报错 spark-shell --master yarn-client,错误如下所示: // :: ERROR SparkContext: Error init ...

  8. JDK8 新特性流式数据处理

    https://blog.csdn.net/canot/article/details/52957262

  9. [转] js中的钩子机制(hook)

    什么是钩子机制?使用钩子机制有什么好处? 钩子机制也叫hook机制,或者你可以把它理解成一种匹配机制,就是我们在代码中设置一些钩子,然后程序执行时自动去匹配这些钩子:这样做的好处就是提高了程序的执行效 ...

  10. C#操作windows服务

    本文主要说明了Windows服务的安装.卸载.启动.停止.获取服务安装路径.获取服务状态.检查服务是否存在.获取服务版本. 我们需要引用命名空间using System.Configuration.I ...