Advertisement
ddeexxiikk

LAB4

Jun 5th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2025 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24.  
  25. /* USER CODE END Includes */
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29.  
  30. /* USER CODE END PTD */
  31.  
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35.  
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38.  
  39. /* USER CODE END PM */
  40.  
  41. /* Private variables ---------------------------------------------------------*/
  42. ADC_HandleTypeDef hadc1;
  43. DMA_HandleTypeDef hdma_adc1;
  44.  
  45. /* USER CODE BEGIN PV */
  46. uint16_t Joystick[2];
  47. uint16_t PomiarADC;
  48. float Temp;
  49. float Vtemp;
  50. const float V25 = 1.43;
  51. const float Avg_slope = 0.0043;
  52. const float SupplyVoltage = 3.3;
  53. const float ADCResolution = 4095.0;
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void MX_GPIO_Init(void);
  59. static void MX_DMA_Init(void);
  60. static void MX_ADC1_Init(void);
  61. /* USER CODE BEGIN PFP */
  62. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  63. {
  64.     PomiarADC = HAL_ADC_GetValue(&hadc1);
  65.     Vtemp = (SupplyVoltage*PomiarADC)/(ADCResolution-1);
  66.     Temp = ((V25-Vtemp)/Avg_slope)+25;
  67. }
  68. /* USER CODE END PFP */
  69.  
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72.  
  73. /* USER CODE END 0 */
  74.  
  75. /**
  76.   * @brief  The application entry point.
  77.   * @retval int
  78.   */
  79. int main(void)
  80. {
  81.   /* USER CODE BEGIN 1 */
  82.  
  83.   /* USER CODE END 1 */
  84.  
  85.   /* MCU Configuration--------------------------------------------------------*/
  86.  
  87.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88.   HAL_Init();
  89.  
  90.   /* USER CODE BEGIN Init */
  91.  
  92.   /* USER CODE END Init */
  93.  
  94.   /* Configure the system clock */
  95.   SystemClock_Config();
  96.  
  97.   /* USER CODE BEGIN SysInit */
  98.  
  99.   /* USER CODE END SysInit */
  100.  
  101.   /* Initialize all configured peripherals */
  102.   MX_GPIO_Init();
  103.   MX_DMA_Init();
  104.   MX_ADC1_Init();
  105.   /* USER CODE BEGIN 2 */
  106.   HAL_ADC_Start_DMA(&hadc1, Joystick, 2);
  107.  
  108.   //HAL_ADC_Start_IT(&hadc1);
  109.  
  110.   //Zadanie 1
  111.   //HAL_ADC_Start_DMA(&hadc1, PomiarADC, 1);
  112.  
  113.   /* USER CODE END 2 */
  114.  
  115.   /* Infinite loop */
  116.   /* USER CODE BEGIN WHILE */
  117.   while (1)
  118.   {
  119.     if((Joystick[0]>4030*0.75||Joystick[1]>4030*0.75)||(Joystick[0]<4030*0.25||Joystick[1]<4030*0.25))
  120.     {
  121.         HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
  122.     }
  123.     else
  124.     {
  125.         HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  126.     }
  127.     /* USER CODE END WHILE */
  128.  
  129.     /* USER CODE BEGIN 3 */
  130.   }
  131.   /* USER CODE END 3 */
  132. }
  133.  
  134. /**
  135.   * @brief System Clock Configuration
  136.   * @retval None
  137.   */
  138. void SystemClock_Config(void)
  139. {
  140.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  141.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  142.  
  143.   /** Initializes the RCC Oscillators according to the specified parameters
  144.   * in the RCC_OscInitTypeDef structure.
  145.   */
  146.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  147.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  148.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  149.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  150.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  151.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;
  152.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  153.   {
  154.     Error_Handler();
  155.   }
  156.  
  157.   /** Initializes the CPU, AHB and APB buses clocks
  158.   */
  159.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  160.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  161.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  162.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  163.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  164.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  165.  
  166.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  167.   {
  168.     Error_Handler();
  169.   }
  170. }
  171.  
  172. /**
  173.   * @brief ADC1 Initialization Function
  174.   * @param None
  175.   * @retval None
  176.   */
  177. static void MX_ADC1_Init(void)
  178. {
  179.  
  180.   /* USER CODE BEGIN ADC1_Init 0 */
  181.  
  182.   /* USER CODE END ADC1_Init 0 */
  183.  
  184.   ADC_ChannelConfTypeDef sConfig = {0};
  185.  
  186.   /* USER CODE BEGIN ADC1_Init 1 */
  187.  
  188.   /* USER CODE END ADC1_Init 1 */
  189.  
  190.   /** Common config
  191.   */
  192.   hadc1.Instance = ADC1;
  193.   hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
  194.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  195.   hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  196.   hadc1.Init.ContinuousConvMode = ENABLE;
  197.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  198.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  199.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  200.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  201.   hadc1.Init.NbrOfConversion = 2;
  202.   hadc1.Init.DMAContinuousRequests = ENABLE;
  203.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  204.   hadc1.Init.LowPowerAutoWait = DISABLE;
  205.   hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  206.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  207.   {
  208.     Error_Handler();
  209.   }
  210.  
  211.   /** Configure Regular Channel
  212.   */
  213.   sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  214.   sConfig.Rank = ADC_REGULAR_RANK_1;
  215.   sConfig.SingleDiff = ADC_SINGLE_ENDED;
  216.   sConfig.SamplingTime = ADC_SAMPLETIME_601CYCLES_5;
  217.   sConfig.OffsetNumber = ADC_OFFSET_NONE;
  218.   sConfig.Offset = 0;
  219.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  220.   {
  221.     Error_Handler();
  222.   }
  223.  
  224.   /** Configure Regular Channel
  225.   */
  226.   sConfig.Channel = ADC_CHANNEL_1;
  227.   sConfig.Rank = ADC_REGULAR_RANK_2;
  228.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  229.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  230.   {
  231.     Error_Handler();
  232.   }
  233.   /* USER CODE BEGIN ADC1_Init 2 */
  234.  
  235.   /* USER CODE END ADC1_Init 2 */
  236.  
  237. }
  238.  
  239. /**
  240.   * Enable DMA controller clock
  241.   */
  242. static void MX_DMA_Init(void)
  243. {
  244.  
  245.   /* DMA controller clock enable */
  246.   __HAL_RCC_DMA1_CLK_ENABLE();
  247.  
  248.   /* DMA interrupt init */
  249.   /* DMA1_Channel1_IRQn interrupt configuration */
  250.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  251.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  252.  
  253. }
  254.  
  255. /**
  256.   * @brief GPIO Initialization Function
  257.   * @param None
  258.   * @retval None
  259.   */
  260. static void MX_GPIO_Init(void)
  261. {
  262.  
  263.   /* GPIO Ports Clock Enable */
  264.   __HAL_RCC_GPIOF_CLK_ENABLE();
  265.   __HAL_RCC_GPIOC_CLK_ENABLE();
  266.   __HAL_RCC_GPIOA_CLK_ENABLE();
  267.  
  268. }
  269.  
  270. /* USER CODE BEGIN 4 */
  271.  
  272. /* USER CODE END 4 */
  273.  
  274. /**
  275.   * @brief  This function is executed in case of error occurrence.
  276.   * @retval None
  277.   */
  278. void Error_Handler(void)
  279. {
  280.   /* USER CODE BEGIN Error_Handler_Debug */
  281.   /* User can add his own implementation to report the HAL error return state */
  282.   __disable_irq();
  283.   while (1)
  284.   {
  285.   }
  286.   /* USER CODE END Error_Handler_Debug */
  287. }
  288.  
  289. #ifdef  USE_FULL_ASSERT
  290. /**
  291.   * @brief  Reports the name of the source file and the source line number
  292.   *         where the assert_param error has occurred.
  293.   * @param  file: pointer to the source file name
  294.   * @param  line: assert_param error line source number
  295.   * @retval None
  296.   */
  297. void assert_failed(uint8_t *file, uint32_t line)
  298. {
  299.   /* USER CODE BEGIN 6 */
  300.   /* User can add his own implementation to report the file name and line number,
  301.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  302.   /* USER CODE END 6 */
  303. }
  304. #endif /* USE_FULL_ASSERT */
  305.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement