用STM32CudeMX 点亮红色LED灯(软件操作步骤为主)

1、收获如何使用软件STM32CudeMX配置代码

 

 

 

 

 

 注意:自己要敲的代码,直接看102行,其他是软件配置的。!!!

  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 
 44 /* USER CODE END Includes */
 45 
 46 /* Private variables ---------------------------------------------------------*/
 47 
 48 /* USER CODE BEGIN PV */
 49 /* Private variables ---------------------------------------------------------*/
 50 
 51 /* USER CODE END PV */
 52 
 53 /* Private function prototypes -----------------------------------------------*/
 54 void SystemClock_Config(void);
 55 static void MX_GPIO_Init(void);
 56 
 57 /* USER CODE BEGIN PFP */
 58 /* Private function prototypes -----------------------------------------------*/
 59 
 60 /* USER CODE END PFP */
 61 
 62 /* USER CODE BEGIN 0 */
 63 
 64 /* USER CODE END 0 */
 65 
 66 int main(void)
 67 {
 68 
 69   /* USER CODE BEGIN 1 */
 70 
 71   /* USER CODE END 1 */
 72 
 73   /* MCU Configuration----------------------------------------------------------*/
 74 
 75   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 76   HAL_Init();
 77 
 78   /* USER CODE BEGIN Init */
 79 
 80   /* USER CODE END Init */
 81 
 82   /* Configure the system clock */
 83   SystemClock_Config();
 84 
 85   /* USER CODE BEGIN SysInit */
 86 
 87   /* USER CODE END SysInit */
 88 
 89   /* Initialize all configured peripherals */
 90   MX_GPIO_Init();
 91 
 92   /* USER CODE BEGIN 2 */
 93 
 94   /* USER CODE END 2 */
 95 
 96   /* Infinite loop */
 97   /* USER CODE BEGIN WHILE */
 98     
 99     /*下面一行代码需要自己敲的*/
100     
101     //意思是给PB5端口低电平,记住这个函数哦
102         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET);    
103     
104     
105     
106   while (1)
107   {
108   /* USER CODE END WHILE */
109 
110   /* USER CODE BEGIN 3 */
111 
112   }
113   /* USER CODE END 3 */
114 
115 }
116 
117 /** System Clock Configuration
118 */
119 void SystemClock_Config(void)
120 {
121 
122   RCC_OscInitTypeDef RCC_OscInitStruct;
123   RCC_ClkInitTypeDef RCC_ClkInitStruct;
124 
125     /**Initializes the CPU, AHB and APB busses clocks 
126     */
127   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
128   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
129   RCC_OscInitStruct.HSICalibrationValue = 16;
130   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
131   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
132   {
133     _Error_Handler(__FILE__, __LINE__);
134   }
135 
136     /**Initializes the CPU, AHB and APB busses clocks 
137     */
138   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
139                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
140   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
141   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
142   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
143   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
144 
145   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
146   {
147     _Error_Handler(__FILE__, __LINE__);
148   }
149 
150     /**Configure the Systick interrupt time 
151     */
152   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
153 
154     /**Configure the Systick 
155     */
156   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
157 
158   /* SysTick_IRQn interrupt configuration */
159   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
160 }
161 
162 /** Configure pins as 
163         * Analog 
164         * Input 
165         * Output
166         * EVENT_OUT
167         * EXTI
168 */
169 static void MX_GPIO_Init(void)
170 {
171 
172   GPIO_InitTypeDef GPIO_InitStruct;
173 
174   /* GPIO Ports Clock Enable */
175   __HAL_RCC_GPIOB_CLK_ENABLE();
176 
177   /*Configure GPIO pin Output Level */
178   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
179 
180   /*Configure GPIO pin : PB5 */
181   GPIO_InitStruct.Pin = GPIO_PIN_5;
182   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
183   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
184   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
185 
186 }
187 
188 /* USER CODE BEGIN 4 */
189 
190 /* USER CODE END 4 */
191 
192 /**
193   * @brief  This function is executed in case of error occurrence.
194   * @param  None
195   * @retval None
196   */
197 void _Error_Handler(char * file, int line)
198 {
199   /* USER CODE BEGIN Error_Handler_Debug */
200   /* User can add his own implementation to report the HAL error return state */
201   while(1) 
202   {
203   }
204   /* USER CODE END Error_Handler_Debug */ 
205 }
206 
207 #ifdef USE_FULL_ASSERT
208 
209 /**
210    * @brief Reports the name of the source file and the source line number
211    * where the assert_param error has occurred.
212    * @param file: pointer to the source file name
213    * @param line: assert_param error line source number
214    * @retval None
215    */
216 void assert_failed(uint8_t* file, uint32_t line)
217 {
218   /* USER CODE BEGIN 6 */
219   /* User can add his own implementation to report the file name and line number,
220     ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
221   /* USER CODE END 6 */
222 
223 }
224 
225 #endif
226 
227 /**
228   * @}
229   */ 
230 
231 /**
232   * @}
233 */ 
234 
235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 感谢您的浏览!!!如有问题请留言。后期继续出其他的,请尽请关注。

原文地址:https://www.cnblogs.com/kinson/p/7840836.html