主函数文件,请直接关注自己写上去的代码:
直接看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)
相关文章