/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

DAC_HandleTypeDef hdac;

TIM_HandleTypeDef htim3;

/* USER CODE BEGIN PV */

uint32_t adc_buffer[2];
uint32_t adc_getValue_1, adc_getValue_2;
float readValue_1, readValue_2;

uint32_t data_1, data_2;
float change_1 = 1, change_2 = 1;

int k = 0;
float va[200] = {2012.5,2067.8554,2123.1554,2178.3447,2233.368,2288.1702,2342.6966,2396.8925,2450.7036,2504.0763,2556.9569,2609.2926,2661.0311,2712.1205,2762.5097,2812.1483,2860.9866,2908.9758,2956.0677,3002.2153,3047.3723,3091.4937,3134.5352,3176.4537,3217.2073,3256.7553,3295.058,3332.0771,3367.7755,3402.1175,3435.0689,3466.5964,3496.6687,3525.2557,3552.3286,3577.8605,3601.8257,3624.2003,3644.9619,3664.0898,3681.5647,3697.3691,3711.4873,3723.9052,3734.6103,3743.5918,3750.8409,3756.3502,3760.1142,3762.1292,3762.3931,3760.9058,3757.6686,3752.6848,3745.9594,3737.4992,3727.3125,3715.4097,3701.8025,3686.5047,3669.5315,3650.8999,3630.6286,3608.7379,3585.2496,3560.1873,3533.576,3505.4425,3475.8148,3444.7225,3412.1969,3378.2705,3342.9772,3306.3523,3268.4325,3229.2558,3188.8613,3147.2895,3104.5819,3060.7815,3015.9318,2970.078,2923.2658,2875.542,2826.9546,2777.552,2727.3837,2676.5,2624.9517,2572.7905,2520.0686,2466.8386,2413.154,2359.0683,2304.6359,2249.911,2194.9486,2139.8035,2084.531,2029.1865,1973.8252,1918.5027,1863.2742,1808.1951,1753.3205,1698.7052,1644.404,1590.4712,1536.9607,1483.9262,1431.4207,1379.4967,1328.2063,1277.6007,1227.7306,1178.6459,1130.3957,1083.0284,1036.5913,991.1309,946.6927,903.3211,861.0597,819.9506,780.035,741.3529,703.943,667.8427,633.0882,599.7142,567.7541,537.2399,508.2022,480.67,454.6709,430.2309,407.3745,386.1245,366.5021,348.5271,332.2175,317.5895,304.6577,293.4352,283.9332,276.1611,270.1268,265.8363,263.2938,262.502,263.4616,266.1717,270.6295,276.8306,284.7688,294.4361,305.8228,318.9177,333.7075,350.1774,368.3111,388.0902,409.4951,432.5043,457.0947,483.2419,510.9195,540.1,570.754,602.8509,636.3587,671.2437,707.4711,745.0045,783.8065,823.8381,865.0594,907.4291,950.9047,995.4428,1040.9988,1087.527,1134.981,1183.3133,1232.4753,1282.4181,1333.0915,1384.4449,1436.4268,1488.9853,1542.0677,1595.6209,1649.5914,1703.9251,1758.5675,1813.4642,1868.56,1923.7999,1979.1285,2034.4906};
float vb[200] = {496.9555,470.0362,444.6606,420.8541,398.6406,378.0422,359.0796,341.7718,326.136,312.188,299.9417,289.4094,280.6015,273.5269,268.1927,264.6043,262.7651,262.6772,264.3404,267.7533,272.9123,279.8123,288.4464,298.8059,310.8806,324.6582,340.1251,357.2657,376.0629,396.4978,418.5501,442.1976,467.4167,494.1821,522.4671,552.2433,583.4809,616.1488,650.2141,685.6428,722.3995,760.4473,799.7481,840.2628,881.9506,924.7698,968.6777,1013.6303,1059.5825,1106.4884,1154.3011,1202.9727,1252.4544,1302.6969,1353.6497,1405.2618,1457.4818,1510.2572,1563.5352,1617.2626,1671.3855,1725.8498,1780.601,1835.5843,1890.7446,1946.0268,2001.3756,2056.7354,2112.051,2167.267,2222.3281,2277.1791,2331.7653,2386.032,2439.9248,2493.3898,2546.3736,2598.823,2650.6857,2701.9096,2752.4436,2802.237,2851.2401,2899.4037,2946.6797,2993.0208,3038.3806,3082.7136,3125.9756,3168.1231,3209.1142,3248.9076,3287.4636,3324.7436,3360.7103,3395.3277,3428.5612,3460.3774,3490.7446,3519.6323,3547.0117,3572.8553,3597.1373,3619.8334,3640.9208,3660.3785,3678.187,3694.3284,3708.7866,3721.5472,3732.5973,3741.926,3749.5238,3755.3831,3759.4982,3761.8649,3762.4807,3761.3452,3758.4594,3753.8262,3747.4503,3739.338,3729.4975,3717.9385,3704.6728,3689.7135,3673.0756,3654.7759,3634.8325,3613.2654,3590.0963,3565.3483,3539.0462,3511.2163,3481.8864,3451.086,3418.8459,3385.1982,3350.1768,3313.8165,3276.1539,3237.2266,3197.0736,3155.7351,3113.2524,3069.668,3025.0256,2979.3699,2932.7465,2885.2021,2836.7843,2787.5416,2737.5232,2686.7791,2635.3603,2583.3181,2530.7045,2477.5724,2423.9748,2369.9654,2315.5983,2260.9278,2206.0087,2150.8959,2095.6446,2040.3101,1984.9477,1929.613,1874.3612,1819.2476,1764.3274,1709.6557,1655.287,1601.2758,1547.6762,1494.5417,1441.9257,1389.8807,1338.4588,1287.7115,1237.6896,1188.4431,1140.0214,1092.4728,1045.845,1000.1847,955.5375,911.9481,869.4602,828.1162,787.9576,749.0245,711.3559,674.9895,639.9617,606.3076,574.0608,543.2536,513.9169,486.0799};

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_DAC_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){

    if (htim->Instance==TIM3){
		
			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
			
			float K1 = change_1*va[k];
			float K2 = change_2*vb[k];
			
			data_1 = (uint32_t) K1;
			data_2 = (uint32_t) K2;
			
			k++;
			if(k == 200){k = 0;}
			
			HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R,data_1);
			HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R,data_2);
		
		}

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){

	if(hadc->Instance == ADC1){
		adc_getValue_1 = adc_buffer[0];
		adc_getValue_2 = adc_buffer[1];
	}
	
}

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_DAC_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_buffer, 2);
	HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
	HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

		readValue_1 = (float) adc_getValue_1;
		readValue_2 = (float) adc_getValue_2;
		
		change_1 = readValue_1/4095;
		change_2 = readValue_2/4095;
		
		HAL_Delay(1);
		
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 2;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
  */
  sConfig.Channel = ADC_CHANNEL_14;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
  */
  sConfig.Channel = ADC_CHANNEL_15;
  sConfig.Rank = 2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

/**
  * @brief DAC Initialization Function
  * @param None
  * @retval None
  */
static void MX_DAC_Init(void)
{

  /* USER CODE BEGIN DAC_Init 0 */

  /* USER CODE END DAC_Init 0 */

  DAC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN DAC_Init 1 */

  /* USER CODE END DAC_Init 1 */
  /** DAC Initialization 
  */
  hdac.Instance = DAC;
  if (HAL_DAC_Init(&hdac) != HAL_OK)
  {
    Error_Handler();
  }
  /** DAC channel OUT1 config 
  */
  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /** DAC channel OUT2 config 
  */
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DAC_Init 2 */

  /* USER CODE END DAC_Init 2 */

}

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 84;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 4;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}

/** 
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void) 
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA2_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA2_Stream0_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);

  /*Configure GPIO pin : PB1 */
  GPIO_InitStruct.Pin = GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
