F439_CPP_BMP390_Barometric_Pressure_and_Altimeter_01 1.0
STM32F439 BMP390 barometric pressure and altimeter tutorial
Loading...
Searching...
No Matches
main.c File Reference

: Main program body More...

#include "main.h"
#include <math.h>
#include <stdio.h>
#include "bmp3.h"
#include "common.h"
#include "i2c_spi_interface.h"

Go to the source code of this file.

Macros

#define LOCAL_QNH_CALIBRATION
 Define to reference altitude against today's actual local sea-level pressure instead of the standard atmosphere.
 
#define SEA_LEVEL_PRESSURE_PA   102057.7
 Sea-level pressure (Pa) used as the altitude reference.
 

Functions

void SystemClock_Config (void)
 System Clock Configuration.
 
static void MX_GPIO_Init (void)
 GPIO Initialization Function.
 
static void MX_USART1_UART_Init (void)
 USART1 Initialization Function.
 
static void MX_I2C1_Init (void)
 I2C1 Initialization Function.
 
static void MX_TIM1_Init (void)
 TIM1 Initialization Function.
 
void delayUS (uint16_t us)
 Busy-waits for the given number of microseconds using TIM1 (1 MHz counter – see MX_TIM1_Init()'s prescaler).
 
static double calculateAltitudeFt (double pressurePa)
 Converts pressure to altitude using the barometric formula (International Standard Atmosphere model), referenced against SEA_LEVEL_PRESSURE_PA.
 
int main (void)
 The application entry point.
 
void _Error_Handler (const char *file, int line)
 Reports a fatal error and halts. See _Error_Handler() in main.c.
 

Variables

I2C_HandleTypeDef hi2c1
 I2C1 handle, used by the BMP390 driver's read/write callbacks.
 
TIM_HandleTypeDef htim1
 TIM1 handle, used as delayUS()'s free-running microsecond counter.
 
UART_HandleTypeDef huart1
 USART1 handle, used for printf() output via REDIRECT_PRINTF.
 
static I2C_SPI_Interface bmp390Intf
 I2C bus/address binding passed to the BMP390 driver as intf_ptr.
 
static struct bmp3_dev bmp390
 BMP390 driver device handle (Bosch bmp3_dev).
 

Detailed Description

: Main program body

Attention

Copyright (c) 2026 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.

Definition in file main.c.

Macro Definition Documentation

◆ LOCAL_QNH_CALIBRATION

#define LOCAL_QNH_CALIBRATION

Define to reference altitude against today's actual local sea-level pressure instead of the standard atmosphere.

This value was derived from a known 600 ft reference elevation and this sensor's readings on 2026-07-09 – it drifts with the weather, so it will need re-deriving the same way to stay accurate later.

Definition at line 49 of file main.c.

◆ SEA_LEVEL_PRESSURE_PA

#define SEA_LEVEL_PRESSURE_PA   102057.7

Sea-level pressure (Pa) used as the altitude reference.

Definition at line 53 of file main.c.

Referenced by calculateAltitudeFt().

Function Documentation

◆ _Error_Handler()

void _Error_Handler ( const char * file,
int line )

Reports a fatal error and halts. See _Error_Handler() in main.c.

Parameters
fileSource file name where the error occurred.
lineSource line number where the error occurred.

Definition at line 444 of file main.c.

◆ calculateAltitudeFt()

static double calculateAltitudeFt ( double pressurePa)
static

Converts pressure to altitude using the barometric formula (International Standard Atmosphere model), referenced against SEA_LEVEL_PRESSURE_PA.

Parameters
pressurePaMeasured pressure in Pa.
Returns
Altitude in feet relative to the SEA_LEVEL_PRESSURE_PA reference.

Definition at line 122 of file main.c.

References SEA_LEVEL_PRESSURE_PA.

Referenced by main().

◆ delayUS()

void delayUS ( uint16_t us)

Busy-waits for the given number of microseconds using TIM1 (1 MHz counter – see MX_TIM1_Init()'s prescaler).

Parameters
usNumber of microseconds to wait (0-65535).

Definition at line 109 of file main.c.

References htim1.

◆ main()

int main ( void )

The application entry point.

Return values
int

Definition at line 134 of file main.c.

References bmp390, bmp390Intf, calculateAltitudeFt(), hi2c1, htim1, MX_GPIO_Init(), MX_I2C1_Init(), MX_TIM1_Init(), MX_USART1_UART_Init(), and SystemClock_Config().

◆ MX_GPIO_Init()

static void MX_GPIO_Init ( void )
static

GPIO Initialization Function.

Definition at line 423 of file main.c.

Referenced by main().

◆ MX_I2C1_Init()

static void MX_I2C1_Init ( void )
static

I2C1 Initialization Function.

Configure Analogue filter

Configure Digital filter

Definition at line 302 of file main.c.

References Error_Handler, and hi2c1.

Referenced by main().

◆ MX_TIM1_Init()

static void MX_TIM1_Init ( void )
static

TIM1 Initialization Function.

Definition at line 348 of file main.c.

References Error_Handler, and htim1.

Referenced by main().

◆ MX_USART1_UART_Init()

static void MX_USART1_UART_Init ( void )
static

USART1 Initialization Function.

Definition at line 392 of file main.c.

References Error_Handler, and huart1.

Referenced by main().

◆ SystemClock_Config()

void SystemClock_Config ( void )

System Clock Configuration.

Configure the main internal regulator output voltage

Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure.

Activate the Over-Drive mode

Initializes the CPU, AHB and APB buses clocks

Definition at line 250 of file main.c.

References Error_Handler.

Referenced by main().

Variable Documentation

◆ bmp390

struct bmp3_dev bmp390
static

BMP390 driver device handle (Bosch bmp3_dev).

Definition at line 81 of file main.c.

Referenced by main().

◆ bmp390Intf

I2C_SPI_Interface bmp390Intf
static

I2C bus/address binding passed to the BMP390 driver as intf_ptr.

Definition at line 78 of file main.c.

Referenced by main().

◆ hi2c1

I2C_HandleTypeDef hi2c1

I2C1 handle, used by the BMP390 driver's read/write callbacks.

Definition at line 68 of file main.c.

Referenced by main(), and MX_I2C1_Init().

◆ htim1

TIM_HandleTypeDef htim1

TIM1 handle, used as delayUS()'s free-running microsecond counter.

Definition at line 71 of file main.c.

Referenced by delayUS(), main(), and MX_TIM1_Init().

◆ huart1

UART_HandleTypeDef huart1

USART1 handle, used for printf() output via REDIRECT_PRINTF.

Definition at line 74 of file main.c.

Referenced by MX_USART1_UART_Init().