Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1.  
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2018 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f3xx_hal.h"
  42.  
  43. /* USER CODE BEGIN Includes */
  44. #include "String.h"
  45. /* USER CODE END Includes */
  46.  
  47. /* Private variables ---------------------------------------------------------*/
  48.  
  49. /* USER CODE BEGIN PV */
  50. /* Private variables ---------------------------------------------------------*/
  51. uint8_t a;
  52. uint8_t b;
  53. uint8_t c;
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void MX_GPIO_Init(void);
  59.  
  60. /* USER CODE BEGIN PFP */
  61. /* Private function prototypes -----------------------------------------------*/
  62.  
  63. /* USER CODE END PFP */
  64.  
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70. * @brief The application entry point.
  71. *
  72. * @retval None
  73. */
  74. int main(void)
  75. {
  76. /* USER CODE BEGIN 1 */
  77.  
  78. /* USER CODE END 1 */
  79.  
  80. /* MCU Configuration----------------------------------------------------------*/
  81.  
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84.  
  85. /* USER CODE BEGIN Init */
  86.  
  87. /* USER CODE END Init */
  88.  
  89. /* Configure the system clock */
  90. SystemClock_Config();
  91.  
  92. /* USER CODE BEGIN SysInit */
  93.  
  94. /* USER CODE END SysInit */
  95.  
  96. /* Initialize all configured peripherals */
  97. MX_GPIO_Init();
  98. /* USER CODE BEGIN 2 */
  99.  
  100. /* USER CODE END 2 */
  101.  
  102. /* Infinite loop */
  103. /* USER CODE BEGIN WHILE */
  104. while (1)
  105. {
  106.  
  107. /* USER CODE END WHILE */
  108.  
  109. /* USER CODE BEGIN 3 */
  110.  
  111.  
  112. while(1){
  113. a++;
  114. b--;
  115. HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_7);
  116. HAL_Delay(50);
  117. HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_4);
  118. HAL_Delay(50);
  119. HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_3);
  120. HAL_Delay(50);
  121.  
  122. if(a==255 || b==0){ break;}
  123. while(1){
  124. a--;
  125. b++;
  126. HAL_Delay(50);
  127. if(a==0 || b==255)
  128. break;
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. }
  136. /* USER CODE END 3 */
  137.  
  138. }
  139.  
  140. /**
  141. * @brief System Clock Configuration
  142. * @retval None
  143. */
  144. void SystemClock_Config(void)
  145. {
  146.  
  147. RCC_OscInitTypeDef RCC_OscInitStruct;
  148. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  149.  
  150. /**Initializes the CPU, AHB and APB busses clocks
  151. */
  152. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  153. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  154. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  155. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  156. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  157. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  158. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  159. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  160. {
  161. _Error_Handler(__FILE__, __LINE__);
  162. }
  163.  
  164. /**Initializes the CPU, AHB and APB busses clocks
  165. */
  166. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  167. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  168. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  169. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  170. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  171. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  172.  
  173. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  174. {
  175. _Error_Handler(__FILE__, __LINE__);
  176. }
  177.  
  178. /**Configure the Systick interrupt time
  179. */
  180. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  181.  
  182. /**Configure the Systick
  183. */
  184. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  185.  
  186. /* SysTick_IRQn interrupt configuration */
  187. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  188. }
  189.  
  190. /** Configure pins as
  191. * Analog
  192. * Input
  193. * Output
  194. * EVENT_OUT
  195. * EXTI
  196. PA5 ------> SPI1_SCK
  197. PA6 ------> SPI1_MISO
  198. PA7 ------> SPI1_MOSI
  199. PA11 ------> USB_DM
  200. PA12 ------> USB_DP
  201. PB6 ------> I2C1_SCL
  202. PB7 ------> I2C1_SDA
  203. */
  204. static void MX_GPIO_Init(void)
  205. {
  206.  
  207. GPIO_InitTypeDef GPIO_InitStruct;
  208.  
  209. /* GPIO Ports Clock Enable */
  210. __HAL_RCC_GPIOE_CLK_ENABLE();
  211. __HAL_RCC_GPIOC_CLK_ENABLE();
  212. __HAL_RCC_GPIOF_CLK_ENABLE();
  213. __HAL_RCC_GPIOA_CLK_ENABLE();
  214. __HAL_RCC_GPIOD_CLK_ENABLE();
  215. __HAL_RCC_GPIOB_CLK_ENABLE();
  216.  
  217. /*Configure GPIO pin Output Level */
  218. HAL_GPIO_WritePin(GPIOE, CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  219. |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  220. |LD6_Pin, GPIO_PIN_RESET);
  221.  
  222. /*Configure GPIO pin Output Level */
  223. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3|GPIO_PIN_4|BLUE_LED_Pin, GPIO_PIN_RESET);
  224.  
  225. /*Configure GPIO pins : DRDY_Pin MEMS_INT3_Pin MEMS_INT4_Pin MEMS_INT1_Pin
  226. MEMS_INT2_Pin */
  227. GPIO_InitStruct.Pin = DRDY_Pin|MEMS_INT3_Pin|MEMS_INT4_Pin|MEMS_INT1_Pin
  228. |MEMS_INT2_Pin;
  229. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  230. GPIO_InitStruct.Pull = GPIO_NOPULL;
  231. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  232.  
  233. /*Configure GPIO pins : CS_I2C_SPI_Pin LD4_Pin LD3_Pin LD5_Pin
  234. LD7_Pin LD9_Pin LD10_Pin LD8_Pin
  235. LD6_Pin */
  236. GPIO_InitStruct.Pin = CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  237. |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  238. |LD6_Pin;
  239. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  240. GPIO_InitStruct.Pull = GPIO_NOPULL;
  241. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  242. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  243.  
  244. /*Configure GPIO pin : B1_Pin */
  245. GPIO_InitStruct.Pin = B1_Pin;
  246. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  247. GPIO_InitStruct.Pull = GPIO_NOPULL;
  248. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  249.  
  250. /*Configure GPIO pins : SPI1_SCK_Pin SPI1_MISO_Pin SPI1_MISOA7_Pin */
  251. GPIO_InitStruct.Pin = SPI1_SCK_Pin|SPI1_MISO_Pin|SPI1_MISOA7_Pin;
  252. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  253. GPIO_InitStruct.Pull = GPIO_NOPULL;
  254. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  255. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  256. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  257.  
  258. /*Configure GPIO pins : DM_Pin DP_Pin */
  259. GPIO_InitStruct.Pin = DM_Pin|DP_Pin;
  260. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  261. GPIO_InitStruct.Pull = GPIO_NOPULL;
  262. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  263. GPIO_InitStruct.Alternate = GPIO_AF14_USB;
  264. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  265.  
  266. /*Configure GPIO pins : PD3 PD4 BLUE_LED_Pin */
  267. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|BLUE_LED_Pin;
  268. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  269. GPIO_InitStruct.Pull = GPIO_NOPULL;
  270. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  271. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  272.  
  273. /*Configure GPIO pins : I2C1_SCL_Pin I2C1_SDA_Pin */
  274. GPIO_InitStruct.Pin = I2C1_SCL_Pin|I2C1_SDA_Pin;
  275. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  276. GPIO_InitStruct.Pull = GPIO_PULLUP;
  277. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  278. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  279. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  280.  
  281. }
  282.  
  283. /* USER CODE BEGIN 4 */
  284.  
  285. /* USER CODE END 4 */
  286.  
  287. /**
  288. * @brief This function is executed in case of error occurrence.
  289. * @param file: The file name as string.
  290. * @param line: The line in file as a number.
  291. * @retval None
  292. */
  293. void _Error_Handler(char *file, int line)
  294. {
  295. /* USER CODE BEGIN Error_Handler_Debug */
  296. /* User can add his own implementation to report the HAL error return state */
  297. while(1)
  298. {
  299. }
  300. /* USER CODE END Error_Handler_Debug */
  301. }
  302.  
  303. #ifdef USE_FULL_ASSERT
  304. /**
  305. * @brief Reports the name of the source file and the source line number
  306. * where the assert_param error has occurred.
  307. * @param file: pointer to the source file name
  308. * @param line: assert_param error line source number
  309. * @retval None
  310. */
  311. void assert_failed(uint8_t* file, uint32_t line)
  312. {
  313. /* USER CODE BEGIN 6 */
  314. /* User can add his own implementation to report the file name and line number,
  315. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  316. /* USER CODE END 6 */
  317. }
  318. #endif /* USE_FULL_ASSERT */
  319.  
  320. /**
  321. * @}
  322. */
  323.  
  324. /**
  325. * @}
  326. */
  327.  
  328. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement