修改HAL标准库用printf函数发送数据直接输出

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

直接看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",21,1000);
 98 printf("\n使用函数Printf函数发送数据\n");
 99 
100 char t;
101 while(1)
102 {
103     scanf("%c",&t);//接受的t的数值
104     if(t=='0')
105     {
106         HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,0);//给低电平,红灯亮
107     }
108     if(t=='1')
109     {
110         HAL_GPIO_WritePin (GPIOB,GPIO_PIN_5,1);//给高电平,红灯灭
111     }
112 }
113 
114 
115   /* USER CODE END 2 */
116 
117   /* Infinite loop */
118   /* USER CODE BEGIN WHILE */
119   while (1)
120   {
121   /* USER CODE END WHILE */
122 
123   /* USER CODE BEGIN 3 */
124 
125   }
126   /* USER CODE END 3 */
127 
128 }
129 
130 /** System Clock Configuration
131 */
132 void SystemClock_Config(void)
133 {
134 
135   RCC_OscInitTypeDef RCC_OscInitStruct;
136   RCC_ClkInitTypeDef RCC_ClkInitStruct;
137 
138     /**Initializes the CPU, AHB and APB busses clocks 
139     */
140   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
141   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
142   RCC_OscInitStruct.HSICalibrationValue = 16;
143   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
144   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
145   {
146     _Error_Handler(__FILE__, __LINE__);
147   }
148 
149     /**Initializes the CPU, AHB and APB busses clocks 
150     */
151   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
152                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
153   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
154   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
155   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
156   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
157 
158   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
159   {
160     _Error_Handler(__FILE__, __LINE__);
161   }
162 
163     /**Configure the Systick interrupt time 
164     */
165   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
166 
167     /**Configure the Systick 
168     */
169   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
170 
171   /* SysTick_IRQn interrupt configuration */
172   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
173 }
174 
175 /* USART1 init function */
176 static void MX_USART1_UART_Init(void)
177 {
178 
179   huart1.Instance = USART1;
180   huart1.Init.BaudRate = 9600;
181   huart1.Init.WordLength = UART_WORDLENGTH_8B;
182   huart1.Init.StopBits = UART_STOPBITS_1;
183   huart1.Init.Parity = UART_PARITY_NONE;
184   huart1.Init.Mode = UART_MODE_TX_RX;
185   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
186   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
187   if (HAL_UART_Init(&huart1) != HAL_OK)
188   {
189     _Error_Handler(__FILE__, __LINE__);
190   }
191 
192 }
193 
194 /** Configure pins as 
195         * Analog 
196         * Input 
197         * Output
198         * EVENT_OUT
199         * EXTI
200 */
201 static void MX_GPIO_Init(void)
202 {
203 
204   GPIO_InitTypeDef GPIO_InitStruct;
205 
206   /* GPIO Ports Clock Enable */
207   __HAL_RCC_GPIOA_CLK_ENABLE();
208   __HAL_RCC_GPIOB_CLK_ENABLE();
209 
210   /*Configure GPIO pin Output Level */
211   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
212 
213   /*Configure GPIO pin : PB5 */
214   GPIO_InitStruct.Pin = GPIO_PIN_5;
215   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
216   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
217   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
218 
219 }
220 
221 /* USER CODE BEGIN 4 */
222 
223 /* USER CODE END 4 */
224 
225 /**
226   * @brief  This function is executed in case of error occurrence.
227   * @param  None
228   * @retval None
229   */
230 void _Error_Handler(char * file, int line)
231 {
232   /* USER CODE BEGIN Error_Handler_Debug */
233   /* User can add his own implementation to report the HAL error return state */
234   while(1) 
235   {
236   }
237   /* USER CODE END Error_Handler_Debug */ 
238 }
239 
240 #ifdef USE_FULL_ASSERT
241 
242 /**
243    * @brief Reports the name of the source file and the source line number
244    * where the assert_param error has occurred.
245    * @param file: pointer to the source file name
246    * @param line: assert_param error line source number
247    * @retval None
248    */
249 void assert_failed(uint8_t* file, uint32_t line)
250 {
251   /* USER CODE BEGIN 6 */
252   /* User can add his own implementation to report the file name and line number,
253     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
254   /* USER CODE END 6 */
255 
256 }
257 
258 #endif
259 
260 /**
261   * @}
262   */ 
263 
264 /**
265   * @}
266 */ 
267 
268 /************************ (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,1,1000);//发送串口
14     return c;
15 }
16 
17 
18 
19 //重定向scanf函数到串口 意思就是说接受串口发过来的数据,其默认是接受控制台的数据
20 /*fgetc*/
21 int fgetc(FILE * F)    
22 {
23     HAL_UART_Receive (&huart1,&ch_r,1,0xffff);//接收
24     return ch_r;
25 }

 

posted @ 2017-11-22 19:53  徐景祥  阅读(7773)  评论(0编辑  收藏  举报