Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  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) 2018 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.  
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f4xx_hal.h"
  42.  
  43. /* USER CODE BEGIN Includes */
  44.  
  45. /* USER CODE END Includes */
  46.  
  47. /* Private variables ---------------------------------------------------------*/
  48. TIM_HandleTypeDef htim9;
  49.  
  50. /* USER CODE BEGIN PV */
  51. /* Private variables ---------------------------------------------------------*/
  52. static int counter, sekundnik;
  53. int licznik=0;
  54. int temp = 0;
  55. /* USER CODE END PV */
  56.  
  57. /* Private function prototypes -----------------------------------------------*/
  58. void SystemClock_Config(void);
  59. static void MX_GPIO_Init(void);
  60. static void MX_TIM9_Init(void);
  61.  
  62. /* USER CODE BEGIN PFP */
  63. /* Private function prototypes -----------------------------------------------*/
  64. void HAL_SYSTICK_Callback()
  65. {
  66. counter++;
  67. if(counter==1000)
  68. {
  69. sekundnik++;
  70. counter = 0;
  71. }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. /* USER CODE END PFP */
  79.  
  80. /* USER CODE BEGIN 0 */
  81. void ZerujLicznik()
  82. {
  83. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7), GPIO_PIN_RESET);
  84. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
  85. }
  86.  
  87. void WyswietlCyfre(int n, int wys)
  88. {
  89. switch(n)
  90. {
  91. case 0:
  92. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_5), GPIO_PIN_SET);
  93. break;
  94.  
  95. case 1:
  96. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_4|GPIO_PIN_3), GPIO_PIN_SET);
  97. break;
  98.  
  99. case 2:
  100. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_2|GPIO_PIN_1), GPIO_PIN_SET);
  101. break;
  102.  
  103. case 3:
  104. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_0), GPIO_PIN_SET);
  105. break;
  106.  
  107. case 4:
  108. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_5|GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_3), GPIO_PIN_SET);
  109. break;
  110.  
  111. case 5:
  112. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_0|GPIO_PIN_3|GPIO_PIN_1), GPIO_PIN_SET);
  113. break;
  114.  
  115. case 6:
  116. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_5|GPIO_PIN_0), GPIO_PIN_SET);
  117. break;
  118.  
  119. case 7:
  120. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_3), GPIO_PIN_SET);
  121. break;
  122.  
  123. case 8:
  124. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_5|GPIO_PIN_0), GPIO_PIN_SET);
  125. break;
  126.  
  127. case 9:
  128. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_0), GPIO_PIN_SET);
  129. break;
  130.  
  131. default:
  132. HAL_GPIO_WritePin(GPIOC, (GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_2|GPIO_PIN_1|GPIO_PIN_0), GPIO_PIN_SET);
  133. break;
  134. }
  135.  
  136. switch(wys)
  137. {
  138. case 1:
  139. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_RESET);
  140. break;
  141. case 2:
  142. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);
  143. break;
  144. case 3:
  145. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
  146. break;
  147. case 4:
  148. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
  149. break;
  150. default:
  151. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8, GPIO_PIN_SET);
  152. break;
  153. }
  154. }
  155.  
  156. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  157. {
  158. if(htim->Instance == TIM9){
  159.  
  160. if(licznik>=4)
  161. {
  162. temp = sekundnik;
  163. licznik = 1;
  164. }
  165. WyswietlCyfre(temp%10,licznik);
  166. temp /= 10;
  167. ZerujLicznik();
  168. licznik++;
  169. }
  170. }
  171.  
  172. /* USER CODE END 0 */
  173.  
  174. int main(void)
  175. {
  176.  
  177. /* USER CODE BEGIN 1 */
  178.  
  179. /* USER CODE END 1 */
  180.  
  181. /* MCU Configuration----------------------------------------------------------*/
  182.  
  183. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  184. HAL_Init();
  185.  
  186. /* USER CODE BEGIN Init */
  187.  
  188. /* USER CODE END Init */
  189.  
  190. /* Configure the system clock */
  191. SystemClock_Config();
  192.  
  193. /* USER CODE BEGIN SysInit */
  194.  
  195. /* USER CODE END SysInit */
  196.  
  197. /* Initialize all configured peripherals */
  198. MX_GPIO_Init();
  199. MX_TIM9_Init();
  200.  
  201. /* USER CODE BEGIN 2 */
  202.  
  203. /* USER CODE END 2 */
  204.  
  205. /* Infinite loop */
  206. /* USER CODE BEGIN WHILE */
  207. while (1)
  208. {
  209. /* USER CODE END WHILE */
  210.  
  211. /* USER CODE BEGIN 3 */
  212.  
  213. if(!(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)))
  214. HAL_GPIO_WritePin(GPIOB, LedM3_Pin|LedM2_Pin|LedM1_Pin, GPIO_PIN_RESET);
  215. else
  216. HAL_GPIO_WritePin(GPIOB, LedM3_Pin|LedM2_Pin|LedM1_Pin, GPIO_PIN_SET);
  217.  
  218.  
  219.  
  220.  
  221. }
  222. /* USER CODE END 3 */
  223.  
  224. }
  225.  
  226. /** System Clock Configuration
  227. */
  228. void SystemClock_Config(void)
  229. {
  230.  
  231. RCC_OscInitTypeDef RCC_OscInitStruct;
  232. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  233.  
  234. /**Configure the main internal regulator output voltage
  235. */
  236. __HAL_RCC_PWR_CLK_ENABLE();
  237.  
  238. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  239.  
  240. /**Initializes the CPU, AHB and APB busses clocks
  241. */
  242. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  243. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  244. RCC_OscInitStruct.HSICalibrationValue = 16;
  245. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  246. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  247. RCC_OscInitStruct.PLL.PLLM = 8;
  248. RCC_OscInitStruct.PLL.PLLN = 100;
  249. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  250. RCC_OscInitStruct.PLL.PLLQ = 4;
  251. RCC_OscInitStruct.PLL.PLLR = 2;
  252. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  253. {
  254. _Error_Handler(__FILE__, __LINE__);
  255. }
  256.  
  257. /**Initializes the CPU, AHB and APB busses clocks
  258. */
  259. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  260. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  261. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  262. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  263. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  264. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  265.  
  266. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  267. {
  268. _Error_Handler(__FILE__, __LINE__);
  269. }
  270.  
  271. /**Configure the Systick interrupt time
  272. */
  273. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  274.  
  275. /**Configure the Systick
  276. */
  277. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  278.  
  279. /* SysTick_IRQn interrupt configuration */
  280. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  281. }
  282.  
  283. /* TIM9 init function */
  284. static void MX_TIM9_Init(void)
  285. {
  286.  
  287. TIM_ClockConfigTypeDef sClockSourceConfig;
  288.  
  289. htim9.Instance = TIM9;
  290. htim9.Init.Prescaler = 0;
  291. htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
  292. htim9.Init.Period = 65535;
  293. htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
  294. if (HAL_TIM_Base_Init(&htim9) != HAL_OK)
  295. {
  296. _Error_Handler(__FILE__, __LINE__);
  297. }
  298.  
  299. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  300. if (HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig) != HAL_OK)
  301. {
  302. _Error_Handler(__FILE__, __LINE__);
  303. }
  304.  
  305. }
  306.  
  307. /** Configure pins as
  308. * Analog
  309. * Input
  310. * Output
  311. * EVENT_OUT
  312. * EXTI
  313. PA2 ------> USART2_TX
  314. PA3 ------> USART2_RX
  315. */
  316. static void MX_GPIO_Init(void)
  317. {
  318.  
  319. GPIO_InitTypeDef GPIO_InitStruct;
  320.  
  321. /* GPIO Ports Clock Enable */
  322. __HAL_RCC_GPIOC_CLK_ENABLE();
  323. __HAL_RCC_GPIOH_CLK_ENABLE();
  324. __HAL_RCC_GPIOA_CLK_ENABLE();
  325. __HAL_RCC_GPIOB_CLK_ENABLE();
  326.  
  327. /*Configure GPIO pin Output Level */
  328. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  329. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  330. |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_RESET);
  331.  
  332. /*Configure GPIO pin Output Level */
  333. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  334.  
  335. /*Configure GPIO pin Output Level */
  336. HAL_GPIO_WritePin(GPIOB, LedM3_Pin|LedM2_Pin|LedM1_Pin, GPIO_PIN_RESET);
  337.  
  338. /*Configure GPIO pin Output Level */
  339. HAL_GPIO_WritePin(LedP_GPIO_Port, LedP_Pin, GPIO_PIN_SET);
  340.  
  341. /*Configure GPIO pin : B1_Pin */
  342. GPIO_InitStruct.Pin = B1_Pin;
  343. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  344. GPIO_InitStruct.Pull = GPIO_NOPULL;
  345. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  346.  
  347. /*Configure GPIO pins : PC0 PC1 PC2 PC3
  348. PC4 PC5 PC6 PC7
  349. PC8 PC9 PC10 PC11 */
  350. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  351. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  352. |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
  353. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  354. GPIO_InitStruct.Pull = GPIO_NOPULL;
  355. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  356. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  357.  
  358. /*Configure GPIO pins : USART_TX_Pin USART_RX_Pin */
  359. GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;
  360. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  361. GPIO_InitStruct.Pull = GPIO_NOPULL;
  362. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  363. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  364. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  365.  
  366. /*Configure GPIO pin : LD2_Pin */
  367. GPIO_InitStruct.Pin = LD2_Pin;
  368. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  369. GPIO_InitStruct.Pull = GPIO_NOPULL;
  370. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  371. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  372.  
  373. /*Configure GPIO pins : LedM3_Pin LedM2_Pin LedM1_Pin LedP_Pin */
  374. GPIO_InitStruct.Pin = LedM3_Pin|LedM2_Pin|LedM1_Pin|LedP_Pin;
  375. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  376. GPIO_InitStruct.Pull = GPIO_NOPULL;
  377. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  378. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  379.  
  380. }
  381.  
  382. /* USER CODE BEGIN 4 */
  383.  
  384. /* USER CODE END 4 */
  385.  
  386. /**
  387. * @brief This function is executed in case of error occurrence.
  388. * @param None
  389. * @retval None
  390. */
  391. void _Error_Handler(char * file, int line)
  392. {
  393. /* USER CODE BEGIN Error_Handler_Debug */
  394. /* User can add his own implementation to report the HAL error return state */
  395. while(1)
  396. {
  397. }
  398. /* USER CODE END Error_Handler_Debug */
  399. }
  400.  
  401. #ifdef USE_FULL_ASSERT
  402.  
  403. /**
  404. * @brief Reports the name of the source file and the source line number
  405. * where the assert_param error has occurred.
  406. * @param file: pointer to the source file name
  407. * @param line: assert_param error line source number
  408. * @retval None
  409. */
  410. void assert_failed(uint8_t* file, uint32_t line)
  411. {
  412. /* USER CODE BEGIN 6 */
  413. /* User can add his own implementation to report the file name and line number,
  414. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  415. /* USER CODE END 6 */
  416.  
  417. }
  418.  
  419. #endif
  420.  
  421. /**
  422. * @}
  423. */
  424.  
  425. /**
  426. * @}
  427. */
  428.  
  429. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement