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

发布时间:2024-08-14  

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 %drn', file, line) */

221   /* USER CODE END 6 */

222 

223 }

224 

225 #endif

226 

227 /**

228   * @}

文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

相关文章

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>