Advertisement
Guest User

Untitled

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