Advertisement
Guest User

lucas

a guest
Apr 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : 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. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f4xx_hal.h"
  41.  
  42. /* USER CODE BEGIN Includes */
  43.  
  44. /* USER CODE END Includes */
  45.  
  46. /* Private variables ---------------------------------------------------------*/
  47. TIM_HandleTypeDef htim10;
  48.  
  49. UART_HandleTypeDef huart2;
  50.  
  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53.  
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void MX_GPIO_Init(void);
  59. static void MX_USART2_UART_Init(void);
  60. static void MX_TIM10_Init(void);
  61.  
  62. /* USER CODE BEGIN PFP */
  63. /* Private function prototypes -----------------------------------------------*/
  64.  
  65. /* USER CODE END PFP */
  66.  
  67. /* USER CODE BEGIN 0 */
  68.  
  69. /* USER CODE END 0 */
  70.  
  71. /**
  72. * @brief The application entry point.
  73. *
  74. * @retval None
  75. */
  76. int main(void)
  77. {
  78. int secCount = 0;
  79. int minCount = 0;
  80. int hCount = 0;
  81. int nCount = 0;
  82. int dCount = 0;
  83. int aDigit[10];
  84. aDigit[0] = 0x3F;
  85. aDigit[1] = 0x06;
  86. aDigit[2] = 0x5B;
  87. aDigit[3] = 0x4F;
  88. aDigit[4] = 0x66;
  89. aDigit[5] = 0x6D;
  90. aDigit[6] = 0x7D;
  91. aDigit[7] = 0x07;
  92. aDigit[8] = 0x7F;
  93. aDigit[9] = 0x6F;
  94.  
  95. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  96. HAL_Init();
  97.  
  98. /* Configure the system clock */
  99. SystemClock_Config();
  100.  
  101. /* Initialize all configured peripherals */
  102. MX_GPIO_Init();
  103. MX_USART2_UART_Init();
  104. MX_TIM10_Init();
  105.  
  106. /* Infinite loop */
  107. while (1)
  108. {
  109. nCount++;
  110.  
  111. /* Update second counter */
  112. if (nCount > 1000) {
  113. secCount++;
  114. nCount = 0;
  115. }
  116.  
  117. /* Update minute counter */
  118. if (secCount > 59) {
  119. minCount++;
  120. secCount = 0;
  121. }
  122.  
  123. /* Update hour counter */
  124. if (minCount > 59) {
  125. hCount++;
  126. minCount = 0;
  127. }
  128.  
  129. /* Reset hour counter */
  130. if (hCount > 23) {
  131. hCount = 0;
  132. }
  133.  
  134. GPIOC->ODR = aDigit[(minCount / 10)%10];
  135. HAL_GPIO_WritePin(GPIOC, SEG_d4_Pin, 1);
  136. HAL_GPIO_WritePin(GPIOC, SEG_d3_Pin, 1);
  137. HAL_GPIO_WritePin(GPIOC, SEG_d2_Pin, 1);
  138. HAL_GPIO_WritePin(GPIOC, SEG_d1_Pin, 0);
  139.  
  140. GPIOC->ODR = aDigit[minCount % 9];
  141. HAL_GPIO_WritePin(GPIOC, SEG_d4_Pin, 1);
  142. HAL_GPIO_WritePin(GPIOC, SEG_d3_Pin, 1);
  143. HAL_GPIO_WritePin(GPIOC, SEG_d2_Pin, 0);
  144. HAL_GPIO_WritePin(GPIOC, SEG_d1_Pin, 1);
  145.  
  146. GPIOC->ODR = aDigit[(secCount / 10)%10];
  147. HAL_GPIO_WritePin(GPIOC, SEG_d4_Pin, 1);
  148. HAL_GPIO_WritePin(GPIOC, SEG_d3_Pin, 0);
  149. HAL_GPIO_WritePin(GPIOC, SEG_d2_Pin, 1);
  150. HAL_GPIO_WritePin(GPIOC, SEG_d1_Pin, 1);
  151.  
  152. GPIOC->ODR = aDigit[secCount % 9];
  153. HAL_GPIO_WritePin(GPIOC, SEG_d4_Pin, 0);
  154. HAL_GPIO_WritePin(GPIOC, SEG_d3_Pin, 1);
  155. HAL_GPIO_WritePin(GPIOC, SEG_d2_Pin, 1);
  156. HAL_GPIO_WritePin(GPIOC, SEG_d1_Pin, 1);
  157. }
  158. }
  159.  
  160. /**
  161. * @brief System Clock Configuration
  162. * @retval None
  163. */
  164. void SystemClock_Config(void)
  165. {
  166.  
  167. RCC_OscInitTypeDef RCC_OscInitStruct;
  168. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  169.  
  170. /**Configure the main internal regulator output voltage
  171. */
  172. __HAL_RCC_PWR_CLK_ENABLE();
  173.  
  174. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  175.  
  176. /**Initializes the CPU, AHB and APB busses clocks
  177. */
  178. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  179. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  180. RCC_OscInitStruct.HSICalibrationValue = 16;
  181. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  182. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  183. RCC_OscInitStruct.PLL.PLLM = 16;
  184. RCC_OscInitStruct.PLL.PLLN = 336;
  185. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  186. RCC_OscInitStruct.PLL.PLLQ = 7;
  187.  
  188. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  189. {
  190. _Error_Handler(__FILE__, __LINE__);
  191. }
  192.  
  193. /**Initializes the CPU, AHB and APB busses clocks
  194. */
  195. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  196. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  197. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  198. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  199. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  200. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  201.  
  202. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  203. {
  204. _Error_Handler(__FILE__, __LINE__);
  205. }
  206.  
  207. /**Configure the Systick interrupt time
  208. */
  209. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  210.  
  211. /**Configure the Systick
  212. */
  213. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  214.  
  215. /* SysTick_IRQn interrupt configuration */
  216. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  217. }
  218.  
  219. /* TIM10 init function */
  220. static void MX_TIM10_Init(void)
  221. {
  222.  
  223. htim10.Instance = TIM10;
  224. htim10.Init.Prescaler = 1343;
  225. htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
  226. htim10.Init.Period = 62499;
  227. htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  228.  
  229. if (HAL_TIM_Base_Init(&htim10) != HAL_OK)
  230. {
  231. _Error_Handler(__FILE__, __LINE__);
  232. }
  233.  
  234. }
  235.  
  236. /* USART2 init function */
  237. static void MX_USART2_UART_Init(void)
  238. {
  239.  
  240. huart2.Instance = USART2;
  241. huart2.Init.BaudRate = 115200;
  242. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  243. huart2.Init.StopBits = UART_STOPBITS_1;
  244. huart2.Init.Parity = UART_PARITY_NONE;
  245. huart2.Init.Mode = UART_MODE_TX_RX;
  246. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  247. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  248.  
  249. if (HAL_UART_Init(&huart2) != HAL_OK)
  250. {
  251. _Error_Handler(__FILE__, __LINE__);
  252. }
  253.  
  254. }
  255.  
  256. /** Configure pins as
  257. * Analog
  258. * Input
  259. * Output
  260. * EVENT_OUT
  261. * EXTI
  262. */
  263. static void MX_GPIO_Init(void)
  264. {
  265.  
  266. GPIO_InitTypeDef GPIO_InitStruct;
  267.  
  268. /* GPIO Ports Clock Enable */
  269. __HAL_RCC_GPIOC_CLK_ENABLE();
  270. __HAL_RCC_GPIOH_CLK_ENABLE();
  271. __HAL_RCC_GPIOA_CLK_ENABLE();
  272. __HAL_RCC_GPIOB_CLK_ENABLE();
  273.  
  274. /*Configure GPIO pin Output Level */
  275. HAL_GPIO_WritePin(GPIOC, SEG_a_Pin|SEG_b_Pin|SEG_c_Pin|SEG_d_Pin
  276. |SEG_e_Pin|SEG_f_Pin|SEG_g_Pin|SEG_dp_Pin
  277. |SEG_d1_Pin|SEG_d2_Pin|SEG_d3_Pin|SEG_d4_Pin, GPIO_PIN_RESET);
  278.  
  279. /*Configure GPIO pin Output Level */
  280. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  281.  
  282. /*Configure GPIO pin : B1_Pin */
  283. GPIO_InitStruct.Pin = B1_Pin;
  284. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  285. GPIO_InitStruct.Pull = GPIO_NOPULL;
  286. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  287.  
  288. /*Configure GPIO pins : SEG_a_Pin SEG_b_Pin SEG_c_Pin SEG_d_Pin
  289. SEG_e_Pin SEG_f_Pin SEG_g_Pin SEG_dp_Pin
  290. SEG_d1_Pin SEG_d2_Pin SEG_d3_Pin SEG_d4_Pin */
  291. GPIO_InitStruct.Pin = SEG_a_Pin|SEG_b_Pin|SEG_c_Pin|SEG_d_Pin
  292. |SEG_e_Pin|SEG_f_Pin|SEG_g_Pin|SEG_dp_Pin
  293. |SEG_d1_Pin|SEG_d2_Pin|SEG_d3_Pin|SEG_d4_Pin;
  294. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  295. GPIO_InitStruct.Pull = GPIO_NOPULL;
  296. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  297. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  298.  
  299. /*Configure GPIO pin : LD2_Pin */
  300. GPIO_InitStruct.Pin = LD2_Pin;
  301. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  302. GPIO_InitStruct.Pull = GPIO_NOPULL;
  303. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  304. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  305.  
  306. }
  307.  
  308. /* USER CODE BEGIN 4 */
  309.  
  310. /* USER CODE END 4 */
  311.  
  312. /**
  313. * @brief This function is executed in case of error occurrence.
  314. * @param file: The file name as string.
  315. * @param line: The line in file as a number.
  316. * @retval None
  317. */
  318. void _Error_Handler(char *file, int line)
  319. {
  320. /* USER CODE BEGIN Error_Handler_Debug */
  321. /* User can add his own implementation to report the HAL error return state */
  322. while(1)
  323. {
  324. }
  325. /* USER CODE END Error_Handler_Debug */
  326. }
  327.  
  328. #ifdef USE_FULL_ASSERT
  329. /**
  330. * @brief Reports the name of the source file and the source line number
  331. * where the assert_param error has occurred.
  332. * @param file: pointer to the source file name
  333. * @param line: assert_param error line source number
  334. * @retval None
  335. */
  336. void assert_failed(uint8_t* file, uint32_t line)
  337. {
  338. /* USER CODE BEGIN 6 */
  339. /* User can add his own implementation to report the file name and line number,
  340. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  341. /* USER CODE END 6 */
  342. }
  343. #endif /* USE_FULL_ASSERT */
  344.  
  345. /**
  346. * @}
  347. */
  348.  
  349. /**
  350. * @}
  351. */
  352.  
  353. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement