Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.67 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.  
  48. /* USER CODE BEGIN PV */
  49. /* Private variables ---------------------------------------------------------*/
  50.  
  51. /* USER CODE END PV */
  52.  
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. static void MX_GPIO_Init(void);
  56.  
  57. /* USER CODE BEGIN PFP */
  58. /* Private function prototypes -----------------------------------------------*/
  59.  
  60. /* USER CODE END PFP */
  61.  
  62. /* USER CODE BEGIN 0 */
  63. volatile int number = 0;
  64. /* USER CODE END 0 */
  65.  
  66. /**
  67.   * @brief  The application entry point.
  68.   *
  69.   * @retval None
  70.   */
  71.  
  72. int main(void)
  73. {
  74.   /* USER CODE BEGIN 1 */
  75.  
  76.   /* USER CODE END 1 */
  77.  
  78.   /* MCU Configuration----------------------------------------------------------*/
  79.  
  80.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  81.   HAL_Init();
  82.  
  83.   /* USER CODE BEGIN Init */
  84.  
  85.   /* USER CODE END Init */
  86.  
  87.   /* Configure the system clock */
  88.   SystemClock_Config();
  89.  
  90.   /* USER CODE BEGIN SysInit */
  91.  
  92.   /* USER CODE END SysInit */
  93.  
  94.   /* Initialize all configured peripherals */
  95.   MX_GPIO_Init();
  96.   /* USER CODE BEGIN 2 */
  97.  
  98.   /* USER CODE END 2 */
  99.  
  100.   /* Infinite loop */
  101.   /* USER CODE BEGIN WHILE */
  102.   for (int i = 0; i >= 0; i++)
  103.   {
  104.  
  105.       /* USER CODE END WHILE */
  106.                         //ZADANIE 1
  107.  
  108.                   //      if (i % 2 == 0) {
  109.                   //          HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
  110.                   //      }
  111.                   //      if (i % 3 == 0) {
  112.                   //          HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
  113.                   //      }
  114.                   //      if (i % 5 == 0) {
  115.                   //          HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
  116.                   //      }
  117.                   //      if (i % 7 == 0) {
  118.                   //          HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
  119.                   //      }
  120.                   //      for (int i = 0; i < 1000000; i++);
  121.  
  122.                         // ZADANIE 2
  123.  
  124. //                        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET ? 0 : 1);
  125. //                        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_SET ? 0 : 1);
  126. //                        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) == GPIO_PIN_SET ? 0 : 1);
  127.  
  128.                         // ZADANIE 3
  129.  
  130. //                        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) != GPIO_PIN_SET) {
  131. //                            HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
  132. //                            HAL_Delay(500);
  133. //                        }
  134. //                        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) != GPIO_PIN_SET) {
  135. //                      HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
  136. //                            HAL_Delay(500);
  137. //                  }
  138. //                        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) != GPIO_PIN_SET) {
  139. //                      HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
  140. //                            HAL_Delay(500);
  141. //                  }
  142.  
  143.                         // ZADANIE 4
  144.  
  145. //                        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET) {
  146. //                            number++;
  147. //                            if (number == 4) number = 0;
  148. //                        }
  149. //
  150. //                        switch (number) {
  151. //                        case 0:
  152. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  153. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
  154. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
  155. //                            break;
  156. //                        case 1:
  157. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  158. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
  159. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
  160. //                            break;
  161. //                        case 2:
  162. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  163. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
  164. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
  165. //                            break;
  166. //                        case 3:
  167. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  168. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
  169. //                            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
  170. //                            break;
  171. //                        }
  172. //
  173. //                        HAL_Delay(200);
  174.  
  175.                         // ZADANIE 5
  176.  
  177.                         if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET) continue;
  178.                         int counter;
  179.                         while (1) {
  180.                             HAL_Delay(100);
  181.                             if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET) break;
  182.                         }
  183.                         if (counter < 1000) {
  184.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  185.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  186.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  187.                         } else if (counter < 2000) {
  188.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  189.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  190.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  191.                         } else if (counter < 3000) {
  192.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  193.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  194.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  195.                         } else {
  196.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  197.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  198.                             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  199.                         }
  200.  
  201.  
  202.                     /* USER CODE BEGIN 3 */
  203.  
  204.   }
  205.   /* USER CODE END 3 */
  206.  
  207. }
  208.  
  209. /**
  210.   * @brief System Clock Configuration
  211.   * @retval None
  212.   */
  213. void SystemClock_Config(void)
  214. {
  215.  
  216.   RCC_OscInitTypeDef RCC_OscInitStruct;
  217.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  218.  
  219.     /**Configure the main internal regulator output voltage
  220.     */
  221.   __HAL_RCC_PWR_CLK_ENABLE();
  222.  
  223.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  224.  
  225.     /**Initializes the CPU, AHB and APB busses clocks
  226.     */
  227.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  228.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  229.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  230.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  231.   RCC_OscInitStruct.PLL.PLLM = 4;
  232.   RCC_OscInitStruct.PLL.PLLN = 168;
  233.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  234.   RCC_OscInitStruct.PLL.PLLQ = 4;
  235.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  236.   {
  237.     _Error_Handler(__FILE__, __LINE__);
  238.   }
  239.  
  240.     /**Initializes the CPU, AHB and APB busses clocks
  241.     */
  242.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  243.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  244.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  245.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  246.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  247.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  248.  
  249.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  250.   {
  251.     _Error_Handler(__FILE__, __LINE__);
  252.   }
  253.  
  254.     /**Configure the Systick interrupt time
  255.     */
  256.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  257.  
  258.     /**Configure the Systick
  259.     */
  260.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  261.  
  262.   /* SysTick_IRQn interrupt configuration */
  263.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  264. }
  265.  
  266. /** Configure pins as
  267.         * Analog
  268.         * Input
  269.         * Output
  270.         * EVENT_OUT
  271.         * EXTI
  272. */
  273. static void MX_GPIO_Init(void)
  274. {
  275.  
  276.   GPIO_InitTypeDef GPIO_InitStruct;
  277.  
  278.   /* GPIO Ports Clock Enable */
  279.   __HAL_RCC_GPIOH_CLK_ENABLE();
  280.   __HAL_RCC_GPIOA_CLK_ENABLE();
  281.   __HAL_RCC_GPIOD_CLK_ENABLE();
  282.  
  283.   /*Configure GPIO pin Output Level */
  284.   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  285.  
  286.   /*Configure GPIO pins : PA1 PA2 PA3 PA4 */
  287.   GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
  288.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  289.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  290.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  291.  
  292.   /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  293.   GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  294.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  295.   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  296.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  297.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  298.  
  299. }
  300.  
  301. /* USER CODE BEGIN 4 */
  302.  
  303. /* USER CODE END 4 */
  304.  
  305. /**
  306.   * @brief  This function is executed in case of error occurrence.
  307.   * @param  file: The file name as string.
  308.   * @param  line: The line in file as a number.
  309.   * @retval None
  310.   */
  311. void _Error_Handler(char *file, int line)
  312. {
  313.   /* USER CODE BEGIN Error_Handler_Debug */
  314.   /* User can add his own implementation to report the HAL error return state */
  315.   while(1)
  316.   {
  317.   }
  318.   /* USER CODE END Error_Handler_Debug */
  319. }
  320.  
  321. #ifdef  USE_FULL_ASSERT
  322. /**
  323.   * @brief  Reports the name of the source file and the source line number
  324.   *         where the assert_param error has occurred.
  325.   * @param  file: pointer to the source file name
  326.   * @param  line: assert_param error line source number
  327.   * @retval None
  328.   */
  329. void assert_failed(uint8_t* file, uint32_t line)
  330. {
  331.   /* USER CODE BEGIN 6 */
  332.   /* User can add his own implementation to report the file name and line number,
  333.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  334.   /* USER CODE END 6 */
  335. }
  336. #endif /* USE_FULL_ASSERT */
  337.  
  338. /**
  339.   * @}
  340.   */
  341.  
  342. /**
  343.   * @}
  344.   */
  345.  
  346. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement