Advertisement
JachyHm

MyFirstSTM32F407VET6 code!

Sep 6th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.88 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 "stm32f4xx_hal.h"
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. TIM_HandleTypeDef htim3;
  45.  
  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. static void MX_GPIO_Init(void);
  49. static void MX_TIM3_Init(void);
  50.  
  51. int stavLED1 = 0;
  52. int stavLED2 = 1;
  53. int key0 = 0;
  54. int key1 = 0;
  55.  
  56. void toggleLEDs() {
  57.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, stavLED1);
  58.     stavLED1 = !stavLED1;
  59.  
  60.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, stavLED2);
  61.     stavLED2 =!stavLED2;
  62. }
  63.  
  64. void delay_ms (uint32_t  dly)
  65. {
  66.     volatile  uint32_t  i,j;
  67.     for (i = 0; i < dly; i++)
  68.     {
  69.         for (j = 0; j < 1000; j++)
  70.         {
  71.         }
  72.     }
  73. }
  74. /**
  75.   * @brief  The application entry point.
  76.   *
  77.   * @retval None
  78.   */
  79. int main(void)
  80. {
  81.   /* MCU Configuration----------------------------------------------------------*/
  82.  
  83.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  84.   HAL_Init();
  85.  
  86.   /* Configure the system clock */
  87.   SystemClock_Config();
  88.  
  89.   /* Initialize all configured peripherals */
  90.   MX_GPIO_Init();
  91.   MX_TIM3_Init();
  92.   /* USER CODE BEGIN 2 */
  93.  
  94.   /*##-1- Start the TIM Base generation in interrupt mode ####################*/
  95.   //HAL_TIM_Base_Start_IT(&htim3);
  96.  
  97.   /* USER CODE END 2 */
  98.   while (1)
  99.   {
  100.       key0 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3);
  101.       key1 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4);
  102.  
  103.       if (key0 == 0 && key1 == 0) {
  104.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
  105.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  106.       } else if (key0 == 0) {
  107.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
  108.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  109.       } else if (key1 == 0) {
  110.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
  111.             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
  112.       } else {
  113.           toggleLEDs();
  114.           HAL_Delay(500);
  115.       }
  116.   }
  117.  
  118. }
  119.  
  120.  
  121. /**
  122.   * @brief System Clock Configuration
  123.   * @retval None
  124.   */
  125. void SystemClock_Config(void)
  126. {
  127.  
  128.   RCC_OscInitTypeDef RCC_OscInitStruct;
  129.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  130.  
  131.     /**Configure the main internal regulator output voltage
  132.     */
  133.   __HAL_RCC_PWR_CLK_ENABLE();
  134.  
  135.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  136.  
  137.     /**Initializes the CPU, AHB and APB busses clocks
  138.     */
  139.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  140.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  141.   RCC_OscInitStruct.HSICalibrationValue = 16;
  142.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  143.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  144.   RCC_OscInitStruct.PLL.PLLM = 16;
  145.   RCC_OscInitStruct.PLL.PLLN = 336;
  146.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  147.   RCC_OscInitStruct.PLL.PLLQ = 4;
  148.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  149.   {
  150.     _Error_Handler(__FILE__, __LINE__);
  151.   }
  152.  
  153.     /**Initializes the CPU, AHB and APB busses clocks
  154.     */
  155.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  156.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  157.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  158.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  159.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  160.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  161.  
  162.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  163.   {
  164.     _Error_Handler(__FILE__, __LINE__);
  165.   }
  166.  
  167.     /**Configure the Systick interrupt time
  168.     */
  169.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  170.  
  171.     /**Configure the Systick
  172.     */
  173.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  174.  
  175.   /* SysTick_IRQn interrupt configuration */
  176.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  177. }
  178.  
  179. /* TIM3 init function */
  180. static void MX_TIM3_Init(void)
  181. {
  182.  
  183.   TIM_ClockConfigTypeDef sClockSourceConfig;
  184.   TIM_MasterConfigTypeDef sMasterConfig;
  185.  
  186.   htim3.Instance = TIM3;
  187.   htim3.Init.Prescaler = 0;
  188.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  189.   htim3.Init.Period = 0;
  190.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  191.   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  192.   {
  193.     _Error_Handler(__FILE__, __LINE__);
  194.   }
  195.  
  196.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  197.   if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  198.   {
  199.     _Error_Handler(__FILE__, __LINE__);
  200.   }
  201.  
  202.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  203.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  204.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  205.   {
  206.     _Error_Handler(__FILE__, __LINE__);
  207.   }
  208.  
  209. }
  210.  
  211. /** Configure pins as
  212.         * Analog
  213.         * Input
  214.         * Output
  215.         * EVENT_OUT
  216.         * EXTI
  217. */
  218. static void MX_GPIO_Init(void)
  219. {
  220.  
  221.   GPIO_InitTypeDef GPIO_InitStruct;
  222.  
  223.   /* GPIO Ports Clock Enable */
  224.   __HAL_RCC_GPIOA_CLK_ENABLE();
  225.   __HAL_RCC_GPIOE_CLK_ENABLE();
  226.  
  227.   /*Configure GPIO pin Output Level */
  228.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  229.   HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);
  230.  
  231.   /*Configure GPIO pins : PA6 PA7 */
  232.   GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  233.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  234.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  235.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  236.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  237.  
  238.   GPIO_InitTypeDef GPIO_InitStruct2;
  239.   /*Configure GPIO pins : PE3 PE4 */
  240.   GPIO_InitStruct2.Pin = GPIO_PIN_3|GPIO_PIN_4;
  241.   GPIO_InitStruct2.Mode = GPIO_MODE_INPUT;
  242.   GPIO_InitStruct2.Pull = GPIO_PULLUP;
  243.   GPIO_InitStruct2.Speed = GPIO_SPEED_FREQ_LOW;
  244.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct2);
  245.  
  246. }
  247. /* USER CODE BEGIN 4 */
  248. /**
  249.   * @brief  Period elapsed callback in non blocking mode
  250.   * @param  htim: TIM handle
  251.   * @retval None
  252.   */
  253. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  254. {
  255.     if (htim->Instance == htim3.Instance)
  256.     {
  257.  
  258.     }
  259. }
  260. /* USER CODE END 4 */
  261.  
  262.  
  263. /**
  264.   * @brief  This function is executed in case of error occurrence.
  265.   * @param  file: The file name as string.
  266.   * @param  line: The line in file as a number.
  267.   * @retval None
  268.   */
  269. void _Error_Handler(char *file, int line)
  270. {
  271.   while(1)
  272.   {
  273.   }
  274. }
  275.  
  276. #ifdef  USE_FULL_ASSERT
  277. /**
  278.   * @brief  Reports the name of the source file and the source line number
  279.   *         where the assert_param error has occurred.
  280.   * @param  file: pointer to the source file name
  281.   * @param  line: assert_param error line source number
  282.   * @retval None
  283.   */
  284. void assert_failed(uint8_t* file, uint32_t line)
  285. {
  286.     printf("Chyba: soubor %s na radku %d\r\n", file, line)
  287. }
  288. #endif
  289.  
  290. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement