Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.85 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 "dac.h"
  25. #include "dma.h"
  26. #include "tim.h"
  27. #include "usart.h"
  28. #include "usb_otg.h"
  29. #include "gpio.h"
  30.  
  31. /* Private includes ----------------------------------------------------------*/
  32. /* USER CODE BEGIN Includes */
  33. #include "math.h"
  34. #include "skrypt.h"
  35. /* USER CODE END Includes */
  36.  
  37. /* Private typedef -----------------------------------------------------------*/
  38. /* USER CODE BEGIN PTD */
  39.  
  40. /* USER CODE END PTD */
  41.  
  42. /* Private define ------------------------------------------------------------*/
  43. /* USER CODE BEGIN PD */
  44.  
  45. /* USER CODE END PD */
  46.  
  47. /* Private macro -------------------------------------------------------------*/
  48. /* USER CODE BEGIN PM */
  49.  
  50. /* USER CODE END PM */
  51.  
  52. /* Private variables ---------------------------------------------------------*/
  53.  
  54. /* USER CODE BEGIN PV */
  55. float y = 0;
  56. float t = 0;
  57. float A = 0;
  58. float volt = 0;
  59. int i = 0;
  60. extern uint16_t zmienna[100];
  61. /* USER CODE END PV */
  62.  
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. /* USER CODE BEGIN PFP */
  66.  
  67.  
  68.  
  69. void DAC_Set_Voltage(double val)
  70. {
  71.     uint32_t var;
  72.     var =((val/3.3)*4095);
  73.     HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, var);
  74. }
  75. /*
  76. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  77. {
  78.     A = 0.5 * HAL_ADC_GetValue(&hadc2)*0.000825;
  79.     HAL_ADC_Start(&hadc2);
  80.     t = t + 0.001;
  81.     y = 1.0 + A*sin (2*10*M_PI*t);
  82.     DAC_Set_Voltage(y);
  83.     HAL_ADC_Start(&hadc1);
  84.     volt = HAL_ADC_GetValue(&hadc1)*0.000825;
  85. }
  86. */
  87. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  88. {
  89.     if(i == 100)
  90.     {
  91.         i = 0;
  92.     }
  93.     float help  = 0;
  94.     float sin_value = 0 ;
  95.     A = 0.5 * HAL_ADC_GetValue(&hadc2)*0.000825;
  96.     help = A*((zmienna[i]/1000.0)-1);
  97.     HAL_ADC_Start(&hadc2);
  98.     sin_value = 1 + help ;
  99.     DAC_Set_Voltage((double)sin_value);
  100.     HAL_ADC_Start(&hadc1);
  101.     volt = HAL_ADC_GetValue(&hadc1)*0.000825;
  102.     i++;
  103. }
  104. /* USER CODE END PFP */
  105. /* Private user code ---------------------------------------------------------*/
  106. /* USER CODE BEGIN 0 */
  107.  
  108. /* USER CODE END 0 */
  109.  
  110. /**
  111.   * @brief  The application entry point.
  112.   * @retval int
  113.   */
  114. int main(void)
  115. {
  116.   /* USER CODE BEGIN 1 */
  117.  
  118.   /* USER CODE END 1 */
  119.  
  120.  
  121.   /* MCU Configuration--------------------------------------------------------*/
  122.  
  123.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  124.   HAL_Init();
  125.  
  126.   /* USER CODE BEGIN Init */
  127.  
  128.   /* USER CODE END Init */
  129.  
  130.   /* Configure the system clock */
  131.   SystemClock_Config();
  132.  
  133.   /* USER CODE BEGIN SysInit */
  134.  
  135.   /* USER CODE END SysInit */
  136.  
  137.   /* Initialize all configured peripherals */
  138.   MX_GPIO_Init();
  139.   MX_DMA_Init();
  140.   MX_USART3_UART_Init();
  141.   MX_USB_OTG_FS_PCD_Init();
  142.   MX_DAC_Init();
  143.   MX_TIM2_Init();
  144.   MX_ADC1_Init();
  145.   MX_ADC2_Init();
  146.   /* USER CODE BEGIN 2 */
  147.   HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
  148.   HAL_ADC_Start(&hadc1);
  149.   HAL_ADC_Start(&hadc2);
  150.   HAL_TIM_Base_Start_IT(&htim2);
  151.   generuj();
  152.  
  153.   /* USER CODE END 2 */
  154.  
  155.   /* Infinite loop */
  156.   /* USER CODE BEGIN WHILE */
  157.   while (1)
  158.   {
  159.     /* USER CODE END WHILE */
  160.  
  161.     /* USER CODE BEGIN 3 */
  162.   }
  163.   /* USER CODE END 3 */
  164. }
  165.  
  166. /**
  167.   * @brief System Clock Configuration
  168.   * @retval None
  169.   */
  170. void SystemClock_Config(void)
  171. {
  172.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  173.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  174.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  175.  
  176.   /** Configure LSE Drive Capability
  177.   */
  178.   HAL_PWR_EnableBkUpAccess();
  179.   /** Configure the main internal regulator output voltage
  180.   */
  181.   __HAL_RCC_PWR_CLK_ENABLE();
  182.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  183.   /** Initializes the CPU, AHB and APB busses clocks
  184.   */
  185.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  186.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  187.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  188.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  189.   RCC_OscInitStruct.PLL.PLLM = 4;
  190.   RCC_OscInitStruct.PLL.PLLN = 72;
  191.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  192.   RCC_OscInitStruct.PLL.PLLQ = 3;
  193.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  194.   {
  195.     Error_Handler();
  196.   }
  197.   /** Initializes the CPU, AHB and APB busses clocks
  198.   */
  199.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  200.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  201.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  202.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  203.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  204.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  205.  
  206.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  207.   {
  208.     Error_Handler();
  209.   }
  210.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_CLK48;
  211.   PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  212.   PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
  213.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  214.   {
  215.     Error_Handler();
  216.   }
  217. }
  218.  
  219. /* USER CODE BEGIN 4 */
  220.  
  221. /* USER CODE END 4 */
  222.  
  223. /**
  224.   * @brief  This function is executed in case of error occurrence.
  225.   * @retval None
  226.   */
  227. void Error_Handler(void)
  228. {
  229.   /* USER CODE BEGIN Error_Handler_Debug */
  230.   /* User can add his own implementation to report the HAL error return state */
  231.  
  232.   /* USER CODE END Error_Handler_Debug */
  233. }
  234.  
  235. #ifdef  USE_FULL_ASSERT
  236. /**
  237.   * @brief  Reports the name of the source file and the source line number
  238.   *         where the assert_param error has occurred.
  239.   * @param  file: pointer to the source file name
  240.   * @param  line: assert_param error line source number
  241.   * @retval None
  242.   */
  243. void assert_failed(uint8_t *file, uint32_t line)
  244. {
  245.   /* USER CODE BEGIN 6 */
  246.   /* User can add his own implementation to report the file name and line number,
  247.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  248.   /* USER CODE END 6 */
  249. }
  250. #endif /* USE_FULL_ASSERT */
  251.  
  252. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement