Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* 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 ---------------------------------------------------------*/
- I2C_HandleTypeDef hi2c1;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- //Rejestry - przykład
- /*
- #define ADDRESS (0x5F << 1) // adres wilgotosci: 0011110x
- #define WHO_AM_I 0x0F // adres do identyfikacji
- #define CTRL_REG1 0x20 // rejestr ustawien 1
- #define T_OUT_L 0x2A // niższy bajt danych temperatury
- #define T_OUT_H 0x2B // wyzszy bajt danych temperatury
- #define T0_degC_x8 0x32
- #define T1_degC_x8 0x33
- #define T1_T0msb 0x35
- #define T0_OUT_L 0x3C
- #define T0_OUT_H 0x3D
- #define T1_OUT_L 0x3E
- #define T1_OUT_H 0x3F
- #define T_OUT (T_OUT_L | 0x80)
- #define T0_O (T0_OUT_L | 0x80)
- #define T1_O (T1_OUT_L | 0x80)
- */
- //Rejestry - wilgotnosc
- /*
- #define ADDRESS (0x5F << 1) // adres wilgotosci: 0011110x
- #define WHO_AM_I 0x0F // adres do identyfikacji
- #define CTRL_REG1 0x20 // rejestr ustawien 1
- #define H_OUT_L 0x28 // niższy bajt danych wilgoci
- #define H_OUT_H 0x29 // wyzszy bajt danych wigloci
- #define H0_rH_x2 0x30
- #define H1_rH_x2 0x31
- #define T1_T0msb 0x35
- #define H0_T0_OUT_L 0x36
- #define H0_T0_OUT_H 0x37
- #define H1_T0_OUT_L 0x3A
- #define H1_T0_OUT_H 0x3B
- #define H_OUT (H_OUT_L | 0x80)
- #define H0_O (H0_T0_OUT_L | 0x80)
- #define H1_O (H1_T0_OUT_L | 0x80)
- */
- // Maski bitowe
- #define MASK_L 0x01 // 0000 0001
- #define MASK_H 0x80 //1000 0000
- // Zmienne
- uint8_t tData1[2];
- uint8_t tData2[2];// Zmienna do bezposredniego odczytu danych temperatury
- uint8_t tData20,tData21,tData22;
- uint8_t tData3[2];
- uint8_t tData4[2];
- int16_t HOUT = 0;
- int16_t H1 = 0;
- int16_t H2= 0;
- int16_t H3 = 0;
- int16_t H0O = 0;
- int16_t H1O = 0;
- float humidity=0;
- uint8_t who_am;
- //Rejestry - cisnienie
- #define LPS22HB_ADDR (0x5C << 1)
- #define WHO_AM_I_REG 0x0F
- #define CTRL_REG1 0x10
- #define PRESS_OUT_XL 0x28
- #define PRESS_OUT_L 0x29
- #define PRESS_OUT_H 0x2A
- #define WHO_AM_I_VALUE 0xB1
- uint8_t who_am_i = 0;
- uint8_t press_raw[3];
- int32_t press_int = 0;
- float pressure_hPa = 0.0f;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_I2C1_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void send_char(char c)
- {
- HAL_UART_Transmit(&huart2, (uint8_t*)&c, 1, 1000);
- }
- int __io_putchar(int ch)
- {
- if (ch == '\n') send_char('\r');
- send_char(ch);
- return ch;
- }
- /* 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_USART2_UART_Init();
- MX_I2C1_Init();
- /* USER CODE BEGIN 2 */
- // wypelnieine zmiennej konfiguracyjnej odpowiednimi opcjami
- /*
- uint8_t Settings = MASK_L | MASK_H; // Wpisanie konfiguracji do rejestru
- HAL_I2C_Mem_Write(&hi2c1, ADDRESS, CTRL_REG1, 1, &Settings, 1, 100);
- printf("Wyszukiwanie ...\n");
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, 0x0F, 1, &who_am, 1, 100);
- if (who_am == 0xBC)
- {
- printf("Znaleziono czujnik HTS221\n");
- }
- else
- {
- printf("Niepoprawna odpowiedz ukladu (0x%02X)\n", who_am);
- }
- */
- HAL_I2C_Mem_Read(&hi2c1, LPS22HB_ADDR, WHO_AM_I_REG, 1, &who_am_i, 1, 100);
- if (who_am_i == WHO_AM_I_VALUE)
- {
- printf("Znaleziono czujnik LPS22HB (WHO_AM_I=0x%02X)\n", who_am_i);
- }
- else
- {
- printf("Niepoprawna odpowiedz ukladu (0x%02X)\n", who_am_i);
- //while(1); // zatrzymaj program
- }
- // Włączenie czujnika: aktywacja, ODR = 10 Hz (0x20)
- uint8_t ctrl_reg1_value = 0x20;
- HAL_I2C_Mem_Write(&hi2c1, LPS22HB_ADDR, CTRL_REG1, 1, &ctrl_reg1_value, 1, 100);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* Temperatura - przykład
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T_OUT, 1, tData1, 2, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T0_degC_x8, 1, &tData20, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_degC_x8, 1, &tData21, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_T0msb, 1, &tData22, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T0_O, 1, tData3, 2, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_O, 1, tData4, 2, 100);
- // Konwersja odebranych bajtow danych na typ int16_t
- TOUT = ((tData1[1] << 8) | tData1[0]);
- T1T0msb = tData22;
- T0degC = (tData20|((T1T0msb& 0x03)<< 8))/8.0;
- T1degC = (tData21|((T1T0msb& 0x0C)<< 6))/8.0;
- T0O = ((tData3[1] << 8) | tData3[0]);
- T1O = ((tData4[1] << 8) | tData4[0]);
- temperatura=((float)(T1degC -T0degC)*(float)(TOUT-T0O)/(float)(T1O-T0O)+(float)T0degC);
- printf("Temp = %.2f\n\r", temperatura);
- HAL_Delay(1000);
- */
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- /*
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H_OUT, 1, tData1, 2, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H0_rH_x2, 1, &tData20, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H1_rH_x2, 1, &tData21, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, T1_T0msb, 1, &tData22, 1, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H0_O, 1, tData3, 2, 100);
- HAL_I2C_Mem_Read(&hi2c1, ADDRESS, H1_O, 1, tData4, 2, 100);
- H1= tData20/2;
- H2= tData21/2;
- H0O = ((tData3[1] << 8) | tData3[0]);
- H1O = ((tData4[1] << 8) | tData4[0]);
- HOUT= ((tData1[1] << 8) | tData1[0]);
- humidity = (((float)(H2-H1)*(float)(HOUT-H0O))/(float)(H1O-H0O))+(float)H1;
- printf("HUM = %d\n\r", (int)humidity);
- HAL_Delay(1000);
- */
- // Odczyt 3 bajtów ciśnienia
- HAL_I2C_Mem_Read(&hi2c1, LPS22HB_ADDR, PRESS_OUT_XL | 0x80, 1, press_raw, 3, 100);
- // 24-bitowa wartość ciśnienia (wartość jest 2’s complement, ale dla LPS22HB jest zawsze dodatnia)
- press_int = (int32_t)((press_raw[2] << 16) | (press_raw[1] << 8) | press_raw[0]);
- // Przeskalowanie na hPa, jednostka w datasheet to 1/4096 hPa
- pressure_hPa = press_int / 4096.0f;
- printf("Ciśnienie: %.2f hPa\n\r", pressure_hPa);
- HAL_Delay(1000);
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- 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.PLLMUL = RCC_PLL_MUL16;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses 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_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
- PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief I2C1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_I2C1_Init(void)
- {
- /* USER CODE BEGIN I2C1_Init 0 */
- /* USER CODE END I2C1_Init 0 */
- /* USER CODE BEGIN I2C1_Init 1 */
- /* USER CODE END I2C1_Init 1 */
- hi2c1.Instance = I2C1;
- hi2c1.Init.Timing = 0x0000020B;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure Analogue filter
- */
- if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure Digital filter
- */
- if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN I2C1_Init 2 */
- /* USER CODE END I2C1_Init 2 */
- }
- /**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART2_UART_Init(void)
- {
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
- /* USER CODE END USART2_Init 2 */
- }
- /**
- * @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_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
Add Comment
Please, Sign In to add comment