Advertisement
Guest User

Untitled

a guest
Mar 4th, 2018
2,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.59 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : Main program body
  5.   ******************************************************************************
  6.   */
  7. /* Includes ------------------------------------------------------------------*/
  8. #include "main.h"
  9. #include "stm32f1xx_hal.h"
  10.  
  11. /* USER CODE BEGIN Includes */
  12.  
  13. /* USER CODE END Includes */
  14.  
  15. /* Private variables ---------------------------------------------------------*/
  16. ADC_HandleTypeDef hadc1;
  17. __IO uint32_t ADC_val = 1;
  18.  
  19. /* USER CODE BEGIN PV */
  20. /* Private variables ---------------------------------------------------------*/
  21.  
  22. /* USER CODE END PV */
  23.  
  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. static void MX_ADC1_Init(void);
  28. static void MX_NVIC_Init(void);
  29.  
  30. /* USER CODE BEGIN PFP */
  31. /* Private function prototypes -----------------------------------------------*/
  32.  
  33. /* USER CODE END PFP */
  34.  
  35. /* USER CODE BEGIN 0 */
  36.  
  37. /* USER CODE END 0 */
  38.  
  39. /**
  40.   * @brief  The application entry point.
  41.   *
  42.   * @retval None
  43.   */
  44. int main(void)
  45. {
  46.   HAL_Init();
  47.  
  48.   SystemClock_Config();
  49.  
  50.   MX_GPIO_Init();
  51.   MX_ADC1_Init();
  52.  
  53.   MX_NVIC_Init();
  54.  
  55.   HAL_ADC_Start_IT(&hadc1);
  56.    
  57.   while (1)
  58.   {
  59.         /*
  60.         if (HAL_ADC_PollForConversion(&hadc1, 10000) == HAL_OK)
  61.         {
  62.             ADC_val = HAL_ADC_GetValue(&hadc1);
  63.         }
  64.         */
  65.        
  66.         HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  67.         HAL_Delay(ADC_val/10);
  68.        
  69.         /*
  70.         if (HAL_GPIO_ReadPin(BTN1_GPIO_Port, BTN1_Pin)) {
  71.             HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  72.             HAL_Delay(ADC_val/10);
  73.         }
  74.         else {
  75.             HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
  76.         }
  77.         */
  78.   }
  79.   /* USER CODE END 3 */
  80.  
  81. }
  82.  
  83. /**
  84.   * @brief System Clock Configuration
  85.   * @retval None
  86.   */
  87. void SystemClock_Config(void)
  88. {
  89.  
  90.   RCC_OscInitTypeDef RCC_OscInitStruct;
  91.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  92.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  93.  
  94.     /**Initializes the CPU, AHB and APB busses clocks
  95.     */
  96.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  97.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  98.   RCC_OscInitStruct.HSICalibrationValue = 16;
  99.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  100.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  101.   {
  102.     _Error_Handler(__FILE__, __LINE__);
  103.   }
  104.  
  105.     /**Initializes the CPU, AHB and APB busses clocks
  106.     */
  107.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  108.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  109.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  110.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  111.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  112.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  113.  
  114.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  115.   {
  116.     _Error_Handler(__FILE__, __LINE__);
  117.   }
  118.  
  119.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  120.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  121.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  122.   {
  123.     _Error_Handler(__FILE__, __LINE__);
  124.   }
  125.  
  126.     /**Configure the Systick interrupt time
  127.     */
  128.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  129.  
  130.     /**Configure the Systick
  131.     */
  132.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  133.  
  134.   /* SysTick_IRQn interrupt configuration */
  135.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  136. }
  137.  
  138. /**
  139.   * @brief NVIC Configuration.
  140.   * @retval None
  141.   */
  142. static void MX_NVIC_Init(void)
  143. {
  144.   /* ADC1_2_IRQn interrupt configuration */
  145.   HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
  146.   HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
  147. }
  148.  
  149. /* ADC1 init function */
  150. static void MX_ADC1_Init(void)
  151. {
  152.  
  153.   ADC_ChannelConfTypeDef sConfig;
  154.  
  155.     /**Common config
  156.     */
  157.   hadc1.Instance = ADC1;
  158.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  159.   hadc1.Init.ContinuousConvMode = DISABLE;
  160.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  161.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  162.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  163.   hadc1.Init.NbrOfConversion = 1;
  164.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  165.   {
  166.     _Error_Handler(__FILE__, __LINE__);
  167.   }
  168.  
  169.     /**Configure Regular Channel
  170.     */
  171.   sConfig.Channel = ADC_CHANNEL_3;
  172.   sConfig.Rank = ADC_REGULAR_RANK_1;
  173.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  174.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  175.   {
  176.     _Error_Handler(__FILE__, __LINE__);
  177.   }
  178.  
  179. }
  180.  
  181. /** Configure pins as
  182.         * Analog
  183.         * Input
  184.         * Output
  185.         * EVENT_OUT
  186.         * EXTI
  187. */
  188. static void MX_GPIO_Init(void)
  189. {
  190.  
  191.   GPIO_InitTypeDef GPIO_InitStruct;
  192.  
  193.   /* GPIO Ports Clock Enable */
  194.   __HAL_RCC_GPIOA_CLK_ENABLE();
  195.  
  196.   /*Configure GPIO pin Output Level */
  197.   HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
  198.  
  199.   /*Configure GPIO pin : BTN1_Pin */
  200.   GPIO_InitStruct.Pin = BTN1_Pin;
  201.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  202.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  203.   HAL_GPIO_Init(BTN1_GPIO_Port, &GPIO_InitStruct);
  204.  
  205.   /*Configure GPIO pin : LED1_Pin */
  206.   GPIO_InitStruct.Pin = LED1_Pin;
  207.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  208.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  209.   HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
  210.  
  211. }
  212.  
  213. /* USER CODE BEGIN 4 */
  214.  
  215. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  216. {
  217.     ADC_val = HAL_ADC_GetValue(hadc);
  218. }
  219.  
  220. void ADC_IRQHandler()
  221. {
  222.     HAL_ADC_IRQHandler(&hadc1);
  223. }
  224.  
  225.  
  226. /* USER CODE END 4 */
  227.  
  228. /**
  229.   * @brief  This function is executed in case of error occurrence.
  230.   * @param  file: The file name as string.
  231.   * @param  line: The line in file as a number.
  232.   * @retval None
  233.   */
  234. void _Error_Handler(char *file, int line)
  235. {
  236.   /* USER CODE BEGIN Error_Handler_Debug */
  237.   /* User can add his own implementation to report the HAL error return state */
  238.   while(1)
  239.   {
  240.   }
  241.   /* USER CODE END Error_Handler_Debug */
  242. }
  243.  
  244. #ifdef  USE_FULL_ASSERT
  245. /**
  246.   * @brief  Reports the name of the source file and the source line number
  247.   *         where the assert_param error has occurred.
  248.   * @param  file: pointer to the source file name
  249.   * @param  line: assert_param error line source number
  250.   * @retval None
  251.   */
  252. void assert_failed(uint8_t* file, uint32_t line)
  253. {
  254.   /* USER CODE BEGIN 6 */
  255.   /* User can add his own implementation to report the file name and line number,
  256.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  257.   /* USER CODE END 6 */
  258. }
  259. #endif /* USE_FULL_ASSERT */
  260.  
  261. /**
  262.   * @}
  263.   */
  264.  
  265. /**
  266.   * @}
  267.   */
  268.  
  269. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement