ddeexxiikk

LAB7

Jun 5th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.18 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. I2C_HandleTypeDef hi2c1;
  43.  
  44. UART_HandleTypeDef huart2;
  45.  
  46. /* USER CODE BEGIN PV */
  47. //Rejestry - przykład
  48. /*
  49. #define ADDRESS (0x5F << 1) // adres wilgotosci: 0011110x
  50. #define WHO_AM_I 0x0F // adres do identyfikacji
  51. #define CTRL_REG1 0x20 // rejestr ustawien 1
  52. #define T_OUT_L 0x2A // niższy bajt danych temperatury
  53. #define T_OUT_H 0x2B // wyzszy bajt danych temperatury
  54. #define T0_degC_x8 0x32
  55. #define T1_degC_x8 0x33
  56. #define T1_T0msb 0x35
  57. #define T0_OUT_L 0x3C
  58. #define T0_OUT_H 0x3D
  59. #define T1_OUT_L 0x3E
  60. #define T1_OUT_H 0x3F
  61. #define T_OUT (T_OUT_L | 0x80)
  62. #define T0_O (T0_OUT_L | 0x80)
  63. #define T1_O (T1_OUT_L | 0x80)
  64. */
  65.  
  66. //Rejestry - wilgotnosc
  67. /*
  68. #define ADDRESS (0x5F << 1) // adres wilgotosci: 0011110x
  69. #define WHO_AM_I 0x0F // adres do identyfikacji
  70. #define CTRL_REG1 0x20 // rejestr ustawien 1
  71. #define H_OUT_L 0x28 // niższy bajt danych wilgoci
  72. #define H_OUT_H 0x29 // wyzszy bajt danych wigloci
  73. #define H0_rH_x2 0x30
  74. #define H1_rH_x2 0x31
  75. #define T1_T0msb 0x35
  76. #define H0_T0_OUT_L 0x36
  77. #define H0_T0_OUT_H 0x37
  78. #define H1_T0_OUT_L 0x3A
  79. #define H1_T0_OUT_H 0x3B
  80. #define H_OUT (H_OUT_L | 0x80)
  81. #define H0_O (H0_T0_OUT_L | 0x80)
  82. #define H1_O (H1_T0_OUT_L | 0x80)
  83. */
  84.  
  85. // Maski bitowe
  86. #define MASK_L 0x01 // 0000 0001
  87. #define MASK_H 0x80 //1000 0000
  88. // Zmienne
  89. uint8_t tData1[2];
  90. uint8_t tData2[2];// Zmienna do bezposredniego odczytu danych temperatury
  91. uint8_t tData20,tData21,tData22;
  92. uint8_t tData3[2];
  93. uint8_t tData4[2];
  94. int16_t HOUT = 0;
  95. int16_t H1 = 0;
  96. int16_t H2= 0;
  97. int16_t H3 = 0;
  98. int16_t H0O = 0;
  99. int16_t H1O = 0;
  100. float humidity=0;
  101. uint8_t who_am;
  102.  
  103. //Rejestry - cisnienie
  104. #define LPS22HB_ADDR       (0x5C << 1)
  105. #define WHO_AM_I_REG       0x0F
  106. #define CTRL_REG1          0x10
  107. #define PRESS_OUT_XL       0x28
  108. #define PRESS_OUT_L        0x29
  109. #define PRESS_OUT_H        0x2A
  110. #define WHO_AM_I_VALUE     0xB1
  111.  
  112. uint8_t who_am_i = 0;
  113. uint8_t press_raw[3];
  114. int32_t press_int = 0;
  115. float pressure_hPa = 0.0f;
  116. /* USER CODE END PV */
  117.  
  118. /* Private function prototypes -----------------------------------------------*/
  119. void SystemClock_Config(void);
  120. static void MX_GPIO_Init(void);
  121. static void MX_USART2_UART_Init(void);
  122. static void MX_I2C1_Init(void);
  123. /* USER CODE BEGIN PFP */
  124.  
  125. /* USER CODE END PFP */
  126.  
  127. /* Private user code ---------------------------------------------------------*/
  128. /* USER CODE BEGIN 0 */
  129. void send_char(char c)
  130. {
  131.     HAL_UART_Transmit(&huart2, (uint8_t*)&c, 1, 1000);
  132. }
  133. int __io_putchar(int ch)
  134. {
  135.     if (ch == '\n') send_char('\r');
  136.     send_char(ch);
  137.     return ch;
  138. }
  139. /* USER CODE END 0 */
  140.  
  141. /**
  142.   * @brief  The application entry point.
  143.   * @retval int
  144.   */
  145. int main(void)
  146. {
  147.   /* USER CODE BEGIN 1 */
  148.  
  149.   /* USER CODE END 1 */
  150.  
  151.   /* MCU Configuration--------------------------------------------------------*/
  152.  
  153.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  154.   HAL_Init();
  155.  
  156.   /* USER CODE BEGIN Init */
  157.  
  158.   /* USER CODE END Init */
  159.  
  160.   /* Configure the system clock */
  161.   SystemClock_Config();
  162.  
  163.   /* USER CODE BEGIN SysInit */
  164.  
  165.   /* USER CODE END SysInit */
  166.  
  167.   /* Initialize all configured peripherals */
  168.   MX_GPIO_Init();
  169.   MX_USART2_UART_Init();
  170.   MX_I2C1_Init();
  171.   /* USER CODE BEGIN 2 */
  172.   // wypelnieine zmiennej konfiguracyjnej odpowiednimi opcjami
  173.   /*
  174.   uint8_t Settings = MASK_L | MASK_H; // Wpisanie konfiguracji do rejestru
  175.   HAL_I2C_Mem_Write(&hi2c1, ADDRESS, CTRL_REG1, 1, &Settings, 1, 100);
  176.   printf("Wyszukiwanie ...\n");
  177.   HAL_I2C_Mem_Read(&hi2c1, ADDRESS, 0x0F, 1, &who_am, 1, 100);
  178.   if (who_am == 0xBC)
  179.   {
  180.       printf("Znaleziono czujnik HTS221\n");
  181.   }
  182.   else
  183.   {
  184.       printf("Niepoprawna odpowiedz ukladu (0x%02X)\n", who_am);
  185.   }
  186.   */
  187.   HAL_I2C_Mem_Read(&hi2c1, LPS22HB_ADDR, WHO_AM_I_REG, 1, &who_am_i, 1, 100);
  188.   if (who_am_i == WHO_AM_I_VALUE)
  189.   {
  190.       printf("Znaleziono czujnik LPS22HB (WHO_AM_I=0x%02X)\n", who_am_i);
  191.   }
  192.   else
  193.   {
  194.       printf("Niepoprawna odpowiedz ukladu (0x%02X)\n", who_am_i);
  195.       //while(1); // zatrzymaj program
  196.   }
  197.   // Włączenie czujnika: aktywacja, ODR = 10 Hz (0x20)
  198.   uint8_t ctrl_reg1_value = 0x20;
  199.      HAL_I2C_Mem_Write(&hi2c1, LPS22HB_ADDR, CTRL_REG1, 1, &ctrl_reg1_value, 1, 100);
  200.  
  201.   /* USER CODE END 2 */
  202.  
  203.   /* Infinite loop */
  204.   /* USER CODE BEGIN WHILE */
  205.    while (1)
  206.    {
  207.        /* Temperatura - przykład
  208.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T_OUT, 1, tData1, 2, 100);
  209.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T0_degC_x8, 1, &tData20, 1, 100);
  210.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_degC_x8, 1, &tData21, 1, 100);
  211.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_T0msb, 1, &tData22, 1, 100);
  212.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T0_O, 1, tData3, 2, 100);
  213.    HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_O, 1, tData4, 2, 100);
  214.    // Konwersja odebranych bajtow danych na typ int16_t
  215.    TOUT = ((tData1[1] << 8) | tData1[0]);
  216.    T1T0msb = tData22;
  217.    T0degC = (tData20|((T1T0msb& 0x03)<< 8))/8.0;
  218.    T1degC = (tData21|((T1T0msb& 0x0C)<< 6))/8.0;
  219.    T0O = ((tData3[1] << 8) | tData3[0]);
  220.    T1O = ((tData4[1] << 8) | tData4[0]);
  221.    temperatura=((float)(T1degC -T0degC)*(float)(TOUT-T0O)/(float)(T1O-T0O)+(float)T0degC);
  222.    printf("Temp = %.2f\n\r", temperatura);
  223.    HAL_Delay(1000);
  224.    */
  225.     /* USER CODE END WHILE */
  226.  
  227.     /* USER CODE BEGIN 3 */
  228.        /*
  229.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H_OUT, 1, tData1, 2, 100);
  230.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H0_rH_x2, 1, &tData20, 1, 100);
  231.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H1_rH_x2, 1, &tData21, 1, 100);
  232.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_T0msb, 1, &tData22, 1, 100);
  233.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H0_O, 1, tData3, 2, 100);
  234.     HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H1_O, 1, tData4, 2, 100);
  235.  
  236.     H1= tData20/2;
  237.     H2= tData21/2;
  238.     H0O = ((tData3[1] << 8) | tData3[0]);
  239.     H1O = ((tData4[1] << 8) | tData4[0]);
  240.     HOUT= ((tData1[1] << 8) | tData1[0]);
  241.  
  242.     humidity = (((float)(H2-H1)*(float)(HOUT-H0O))/(float)(H1O-H0O))+(float)H1;
  243.  
  244.     printf("HUM = %d\n\r", (int)humidity);
  245.     HAL_Delay(1000);
  246.     */
  247.     // Odczyt 3 bajtów ciśnienia
  248.             HAL_I2C_Mem_Read(&hi2c1, LPS22HB_ADDR, PRESS_OUT_XL | 0x80, 1, press_raw, 3, 100);
  249.  
  250.             // 24-bitowa wartość ciśnienia (wartość jest 2’s complement, ale dla LPS22HB jest zawsze dodatnia)
  251.             press_int = (int32_t)((press_raw[2] << 16) | (press_raw[1] << 8) | press_raw[0]);
  252.  
  253.             // Przeskalowanie na hPa, jednostka w datasheet to 1/4096 hPa
  254.             pressure_hPa = press_int / 4096.0f;
  255.  
  256.             printf("Ciśnienie: %.2f hPa\n\r", pressure_hPa);
  257.  
  258.             HAL_Delay(1000);
  259.   }
  260.   /* USER CODE END 3 */
  261. }
  262.  
  263. /**
  264.   * @brief System Clock Configuration
  265.   * @retval None
  266.   */
  267. void SystemClock_Config(void)
  268. {
  269.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  270.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  271.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  272.  
  273.   /** Initializes the RCC Oscillators according to the specified parameters
  274.   * in the RCC_OscInitTypeDef structure.
  275.   */
  276.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  277.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  278.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  279.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  280.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  281.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  282.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  283.   {
  284.     Error_Handler();
  285.   }
  286.  
  287.   /** Initializes the CPU, AHB and APB buses clocks
  288.   */
  289.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  290.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  291.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  292.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  293.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  294.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  295.  
  296.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  297.   {
  298.     Error_Handler();
  299.   }
  300.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  301.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  302.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  303.   {
  304.     Error_Handler();
  305.   }
  306. }
  307.  
  308. /**
  309.   * @brief I2C1 Initialization Function
  310.   * @param None
  311.   * @retval None
  312.   */
  313. static void MX_I2C1_Init(void)
  314. {
  315.  
  316.   /* USER CODE BEGIN I2C1_Init 0 */
  317.  
  318.   /* USER CODE END I2C1_Init 0 */
  319.  
  320.   /* USER CODE BEGIN I2C1_Init 1 */
  321.  
  322.   /* USER CODE END I2C1_Init 1 */
  323.   hi2c1.Instance = I2C1;
  324.   hi2c1.Init.Timing = 0x0000020B;
  325.   hi2c1.Init.OwnAddress1 = 0;
  326.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  327.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  328.   hi2c1.Init.OwnAddress2 = 0;
  329.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  330.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  331.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  332.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  333.   {
  334.     Error_Handler();
  335.   }
  336.  
  337.   /** Configure Analogue filter
  338.   */
  339.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  340.   {
  341.     Error_Handler();
  342.   }
  343.  
  344.   /** Configure Digital filter
  345.   */
  346.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  347.   {
  348.     Error_Handler();
  349.   }
  350.   /* USER CODE BEGIN I2C1_Init 2 */
  351.  
  352.   /* USER CODE END I2C1_Init 2 */
  353.  
  354. }
  355.  
  356. /**
  357.   * @brief USART2 Initialization Function
  358.   * @param None
  359.   * @retval None
  360.   */
  361. static void MX_USART2_UART_Init(void)
  362. {
  363.  
  364.   /* USER CODE BEGIN USART2_Init 0 */
  365.  
  366.   /* USER CODE END USART2_Init 0 */
  367.  
  368.   /* USER CODE BEGIN USART2_Init 1 */
  369.  
  370.   /* USER CODE END USART2_Init 1 */
  371.   huart2.Instance = USART2;
  372.   huart2.Init.BaudRate = 115200;
  373.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  374.   huart2.Init.StopBits = UART_STOPBITS_1;
  375.   huart2.Init.Parity = UART_PARITY_NONE;
  376.   huart2.Init.Mode = UART_MODE_TX_RX;
  377.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  378.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  379.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  380.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  381.   if (HAL_UART_Init(&huart2) != HAL_OK)
  382.   {
  383.     Error_Handler();
  384.   }
  385.   /* USER CODE BEGIN USART2_Init 2 */
  386.  
  387.   /* USER CODE END USART2_Init 2 */
  388.  
  389. }
  390.  
  391. /**
  392.   * @brief GPIO Initialization Function
  393.   * @param None
  394.   * @retval None
  395.   */
  396. static void MX_GPIO_Init(void)
  397. {
  398.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  399.  
  400.   /* GPIO Ports Clock Enable */
  401.   __HAL_RCC_GPIOC_CLK_ENABLE();
  402.   __HAL_RCC_GPIOF_CLK_ENABLE();
  403.   __HAL_RCC_GPIOA_CLK_ENABLE();
  404.   __HAL_RCC_GPIOB_CLK_ENABLE();
  405.  
  406.   /*Configure GPIO pin Output Level */
  407.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  408.  
  409.   /*Configure GPIO pin : B1_Pin */
  410.   GPIO_InitStruct.Pin = B1_Pin;
  411.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  412.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  413.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  414.  
  415.   /*Configure GPIO pin : LD2_Pin */
  416.   GPIO_InitStruct.Pin = LD2_Pin;
  417.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  418.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  419.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  420.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  421.  
  422. }
  423.  
  424. /* USER CODE BEGIN 4 */
  425.  
  426. /* USER CODE END 4 */
  427.  
  428. /**
  429.   * @brief  This function is executed in case of error occurrence.
  430.   * @retval None
  431.   */
  432. void Error_Handler(void)
  433. {
  434.   /* USER CODE BEGIN Error_Handler_Debug */
  435.   /* User can add his own implementation to report the HAL error return state */
  436.   __disable_irq();
  437.   while (1)
  438.   {
  439.   }
  440.   /* USER CODE END Error_Handler_Debug */
  441. }
  442.  
  443. #ifdef  USE_FULL_ASSERT
  444. /**
  445.   * @brief  Reports the name of the source file and the source line number
  446.   *         where the assert_param error has occurred.
  447.   * @param  file: pointer to the source file name
  448.   * @param  line: assert_param error line source number
  449.   * @retval None
  450.   */
  451. void assert_failed(uint8_t *file, uint32_t line)
  452. {
  453.   /* USER CODE BEGIN 6 */
  454.   /* User can add his own implementation to report the file name and line number,
  455.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  456.   /* USER CODE END 6 */
  457. }
  458. #endif /* USE_FULL_ASSERT */
  459.  
Add Comment
Please, Sign In to add comment