Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.10 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23. #include "adc.h"
  24. #include "dma.h"
  25. #include "usart.h"
  26. #include "usb_otg.h"
  27. #include "gpio.h"
  28.  
  29. /* Private includes ----------------------------------------------------------*/
  30. /* USER CODE BEGIN Includes */
  31.  
  32. /* USER CODE END Includes */
  33.  
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* USER CODE BEGIN PTD */
  36.  
  37. /* USER CODE END PTD */
  38.  
  39. /* Private define ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41.  
  42. /* USER CODE END PD */
  43.  
  44. /* Private macro -------------------------------------------------------------*/
  45. /* USER CODE BEGIN PM */
  46.  
  47. /* USER CODE END PM */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50.  
  51. /* USER CODE BEGIN PV */
  52. uint16_t value;
  53. char buffer[40];
  54. uint16_t ADC_DATA[2];
  55. uint8_t size;
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. /* USER CODE BEGIN PFP */
  61. void LEDs(int voltage_mV);
  62. /* USER CODE END PFP */
  63.  
  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70.   * @brief  The application entry point.
  71.   * @retval int
  72.   */
  73. int main(void)
  74. {
  75.   /* USER CODE BEGIN 1 */
  76.  
  77.   /* USER CODE END 1 */
  78.  
  79.  
  80.   /* MCU Configuration--------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_DMA_Init();
  99.   MX_ADC1_Init();
  100.   MX_USART3_UART_Init();
  101.   MX_USB_OTG_FS_PCD_Init();
  102.   /* USER CODE BEGIN 2 */
  103.   HAL_ADC_Start(&hadc1);
  104.  
  105.   HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC_DATA, 2);
  106.   /* USER CODE END 2 */
  107.  
  108.   /* Infinite loop */
  109.   /* USER CODE BEGIN WHILE */
  110.   while (1)
  111.   {
  112.     /* USER CODE END WHILE */
  113.       HAL_Delay(200);
  114.  
  115.     /* USER CODE BEGIN 3 */
  116.   /* USER CODE END 3 */
  117.   }
  118. }
  119.  
  120. /**
  121.   * @brief System Clock Configuration
  122.   * @retval None
  123.   */
  124. void SystemClock_Config(void)
  125. {
  126.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  129.  
  130.   /** Configure LSE Drive Capability
  131.   */
  132.   HAL_PWR_EnableBkUpAccess();
  133.   /** Configure the main internal regulator output voltage
  134.   */
  135.   __HAL_RCC_PWR_CLK_ENABLE();
  136.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  137.   /** Initializes the CPU, AHB and APB busses clocks
  138.   */
  139.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  140.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  141.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  142.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  143.   RCC_OscInitStruct.PLL.PLLM = 4;
  144.   RCC_OscInitStruct.PLL.PLLN = 72;
  145.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  146.   RCC_OscInitStruct.PLL.PLLQ = 3;
  147.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  148.   {
  149.     Error_Handler();
  150.   }
  151.   /** Initializes the CPU, AHB and APB busses clocks
  152.   */
  153.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  154.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  155.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  156.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  157.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  158.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  159.  
  160.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  161.   {
  162.     Error_Handler();
  163.   }
  164.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_CLK48;
  165.   PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  166.   PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
  167.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  168.   {
  169.     Error_Handler();
  170.   }
  171. }
  172.  
  173. /* USER CODE BEGIN 4 */
  174. void LEDs(int voltage_mV)
  175. {
  176.  
  177.        if(voltage_mV > 1500)
  178.        {
  179.            HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, 1);
  180.            HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 1);
  181.            HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, 1);
  182.        }
  183.        else if(voltage_mV > 1000)
  184.        {
  185.            HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, 1);
  186.            HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 1);
  187.            HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, 0);
  188.        }
  189.        else if(value > 500)
  190.            {
  191.                HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, 1);
  192.                HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 0);
  193.                HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, 0);
  194.            }
  195.        else
  196.            {
  197.                HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, 0);
  198.                HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 0);
  199.                HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, 0);
  200.            }
  201. }
  202. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  203. {
  204.     if(GPIO_Pin ==USER_Btn_Pin){
  205.         HAL_ADC_Start_IT(&hadc1);
  206.     }
  207. }
  208.  
  209. void HAL_ADC_ConvCpltCallback (ADC_HandleTypeDef * hadc)
  210. {
  211.     value = ADC_DATA[0] * 0.825;
  212.     float adc_voltage_temp  = ((float)(ADC_DATA[1] / 4095.0) *3.3f);
  213.     float adc_temp = (((adc_voltage_temp - 0.76)/0.0025) + 25.0);
  214.     float ADC_temp_SWV = (uint32_t)(100.0*adc_temp);
  215.     float a = 0;
  216. }
  217.  
  218.  
  219. /* USER CODE END 4 */
  220.  
  221. /**
  222.   * @brief  This function is executed in case of error occurrence.
  223.   * @retval None
  224.   */
  225. void Error_Handler(void)
  226. {
  227.   /* USER CODE BEGIN Error_Handler_Debug */
  228.   /* User can add his own implementation to report the HAL error return state */
  229.  
  230.   /* USER CODE END Error_Handler_Debug */
  231. }
  232.  
  233. #ifdef  USE_FULL_ASSERT
  234. /**
  235.   * @brief  Reports the name of the source file and the source line number
  236.   *         where the assert_param error has occurred.
  237.   * @param  file: pointer to the source file name
  238.   * @param  line: assert_param error line source number
  239.   * @retval None
  240.   */
  241. void assert_failed(uint8_t *file, uint32_t line)
  242. {
  243.   /* USER CODE BEGIN 6 */
  244.   /* User can add his own implementation to report the file name and line number,
  245.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  246.   /* USER CODE END 6 */
  247. }
  248. #endif /* USE_FULL_ASSERT */
  249.  
  250. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement