当前位置:   article > 正文

嵌入式开发仿真软件——proteus8.15_嵌入式仿真软件

嵌入式仿真软件

 

  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2017 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. /* USER CODE END Includes */
  37. /* Private variables ---------------------------------------------------------*/
  38. TIM_HandleTypeDef htim1;
  39. /* USER CODE BEGIN PV */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE END PV */
  42. /* Private function prototypes -----------------------------------------------*/
  43. void SystemClock_Config(void);
  44. void Error_Handler(void);
  45. static void MX_GPIO_Init(void);
  46. static void MX_TIM1_Init(void);
  47. /* USER CODE BEGIN PFP */
  48. /* Private function prototypes -----------------------------------------------*/
  49. /* USER CODE END PFP */
  50. /* USER CODE BEGIN 0 */
  51. /* USER CODE END 0 */
  52. int main(void)
  53. {
  54. /* USER CODE BEGIN 1 */
  55. /* USER CODE END 1 */
  56. /* MCU Configuration----------------------------------------------------------*/
  57. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58. HAL_Init();
  59. /* Configure the system clock */
  60. SystemClock_Config();
  61. /* Initialize all configured peripherals */
  62. MX_GPIO_Init();
  63. MX_TIM1_Init();
  64. /* USER CODE BEGIN 2 */
  65. HAL_TIM_Base_Start_IT(&htim1);
  66. /* USER CODE END 2 */
  67. /* Infinite loop */
  68. /* USER CODE BEGIN WHILE */
  69. while (1)
  70. {
  71. /* USER CODE END WHILE */
  72. /* USER CODE BEGIN 3 */
  73. }
  74. /* USER CODE END 3 */
  75. }
  76. /** System Clock Configuration
  77. */
  78. void SystemClock_Config(void)
  79. {
  80. RCC_OscInitTypeDef RCC_OscInitStruct;
  81. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  82. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  83. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  84. RCC_OscInitStruct.HSICalibrationValue = 16;
  85. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  86. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  87. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  88. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  89. {
  90. Error_Handler();
  91. }
  92. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  93. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  94. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  95. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  96. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  97. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  98. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  99. {
  100. Error_Handler();
  101. }
  102. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  103. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  104. /* SysTick_IRQn interrupt configuration */
  105. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  106. }
  107. /* TIM1 init function */
  108. static void MX_TIM1_Init(void)
  109. {
  110. TIM_ClockConfigTypeDef sClockSourceConfig;
  111. TIM_MasterConfigTypeDef sMasterConfig;
  112. htim1.Instance = TIM1;
  113. htim1.Init.Prescaler = 64000;
  114. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  115. htim1.Init.Period = 1;
  116. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  117. htim1.Init.RepetitionCounter = 0;
  118. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  119. {
  120. Error_Handler();
  121. }
  122. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  123. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  128. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  129. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  130. {
  131. Error_Handler();
  132. }
  133. }
  134. /** Configure pins as
  135. * Analog
  136. * Input
  137. * Output
  138. * EVENT_OUT
  139. * EXTI
  140. * Free pins are configured automatically as Analog (this feature is enabled through
  141. * the Code Generation settings)
  142. */
  143. static void MX_GPIO_Init(void)
  144. {
  145. GPIO_InitTypeDef GPIO_InitStruct;
  146. /* GPIO Ports Clock Enable */
  147. __HAL_RCC_GPIOC_CLK_ENABLE();
  148. __HAL_RCC_GPIOD_CLK_ENABLE();
  149. __HAL_RCC_GPIOA_CLK_ENABLE();
  150. __HAL_RCC_GPIOB_CLK_ENABLE();
  151. /*Configure GPIO pins : PC13 PC14 PC15 PC0
  152. PC1 PC2 PC3 PC4
  153. PC5 PC6 PC7 PC8
  154. PC9 PC10 PC11 PC12 */
  155. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0
  156. |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
  157. |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
  158. |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
  159. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  160. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  161. /*Configure GPIO pins : PD0 PD1 PD2 */
  162. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
  163. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  164. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  165. /*Configure GPIO pins : PA0 PA1 PA2 PA3
  166. PA4 PA6 PA7 PA8
  167. PA9 PA10 PA11 PA12
  168. PA13 PA14 PA15 */
  169. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  170. |GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
  171. |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
  172. |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  173. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  174. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  175. /*Configure GPIO pin : Ld2_Pin */
  176. GPIO_InitStruct.Pin = Ld2_Pin;
  177. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  178. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  179. HAL_GPIO_Init(Ld2_GPIO_Port, &GPIO_InitStruct);
  180. /*Configure GPIO pins : PB0 PB1 PB2 PB10
  181. PB11 PB12 PB13 PB14
  182. PB15 PB3 PB4 PB5
  183. PB6 PB7 PB8 PB9 */
  184. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
  185. |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
  186. |GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
  187. |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
  188. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  189. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  190. /*Configure peripheral I/O remapping */
  191. __HAL_AFIO_REMAP_PD01_ENABLE();
  192. /*Configure GPIO pin Output Level */
  193. HAL_GPIO_WritePin(Ld2_GPIO_Port, Ld2_Pin, GPIO_PIN_RESET);
  194. }
  195. /* USER CODE BEGIN 4 */
  196. /* USER CODE END 4 */
  197. /**
  198. * @brief This function is executed in case of error occurrence.
  199. * @param None
  200. * @retval None
  201. */
  202. void Error_Handler(void)
  203. {
  204. /* USER CODE BEGIN Error_Handler */
  205. /* User can add his own implementation to report the HAL error return state */
  206. while(1)
  207. {
  208. }
  209. /* USER CODE END Error_Handler */
  210. }
  211. #ifdef USE_FULL_ASSERT
  212. /**
  213. * @brief Reports the name of the source file and the source line number
  214. * where the assert_param error has occurred.
  215. * @param file: pointer to the source file name
  216. * @param line: assert_param error line source number
  217. * @retval None
  218. */
  219. void assert_failed(uint8_t* file, uint32_t line)
  220. {
  221. /* USER CODE BEGIN 6 */
  222. /* User can add his own implementation to report the file name and line number,
  223. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  224. /* USER CODE END 6 */
  225. }
  226. #endif
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @}
  232. */
  233. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/940015
推荐阅读
相关标签
  

闽ICP备14008679号