GebraBit

MS5611 Sensor Project With STM32F303 Microcontroller

متن سربرگ خود را وارد کنید

MS5611 gebrabit project

MS5611 Sensor Project With STM32F303 Microcontroller

MS5611 gebrabit project
  1. Home
  2. »
  3. Projects
  4. »
  5. MS5611 Sensor Project With STM32F303 Microcontroller

What's the purpose of this project?

In this project, we are going to use the GB640EN module to set up the MS5611 temperature and humidity sensor with the STM32F microcontroller through the SPI protocol. Since you probably don’t know the GB640EN module, it is appropriate to say that GB640EN is a module that includes the MS5611 sensor and other necessary components, designed and produced by the GebraBit team in the form of an integrated kit for your convenience.

What are we going to learn in this tutorial?

In this tutorial, in addition to setting up and using the MS5611 sensor, you will get to know all the MS5611 sensor registers, how to set the various parts of the STM32 microcontroller to set up this sensor using the SPI protocol, how to use the GB640EN module specific library and driver file. You will also learn how to declare functions and finally receive sensor data in the Keil compiler.  

What do we need to start this project?

As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start. ^-^

Required hardware
Required software
Keil compiler 
 STM32CubeMX program
 ST-LINK/V2 programmer

Are you ready?? so let’s start !!

First of all, we must select the SPI communication protocol using the jumpers on the board and then place the GebraBit MS5611 module Pin to Pin on the GebraBit STM32F303 module as shown below:

Note: The above picture is intended only to show how the GebraBit MS5611 module is placed as pin to pin on the GebraBit STM32F303 module. Therefore, to use the SPI communication protocol, the user must choose the correct state of the on-board selector jumpers.

Finally, we will see the values of temperature, pressure, and approximate height in real time in the “Watch1” window of the Keil compiler in “Debug Session” mode.

STM32CubeMX settings

In the following, we review the settings related to each of the “SPI”, “RCC”, “Debug”, and “Clock” sections in the STM32F303 microcontroller to develop the GebraBit MS5611 module.

RCC settings

Due to the presence of “8Mhz” crystal in the GebraBit STM32F303 module, we select the “external clock” in the “RCC” section:

Debug & Programming settings

Regarding the access to “SWCLK” and “SWDIO” pins in the GebraBit STM32F303 module, to reduce the number of pins during “Debug & Programming”, in the “SYS” block, we select the “Serial Wire” option in the “Debug” section:

SPI settings

To communicate with the GebraBit STM32F303 module via SPI, we select the “Full Duplex Master” mode and select the PB3, PB4, and PB5 pins as SCK, MISO and MOSI and define the PC13 pin as CS:

According to the sensor data sheet, the settings of the SPI parameters in the “Parameter Settings” section will be set as shown in the above image.

Clock settings

The “clock” settings for each part of the STM32F303 microcontroller in this code, are as follows:

Project Manager settings

“Project Manager” settings are as follows, here we have used “MDK-ARM” version “5.32” compiler:

After completing all the above settings, we can develop our code easily just by one click on “GENERATE CODE” and adding the MS5611 library and driver (provided by GebraBit).

You can download the “STM32Cube MX”, “library”, “driver” and KEIL project at the end of this tutorial.   

MS5611 library and driver

In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.

For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “  .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.

All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.

GebraBit MS5611.h header file

In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the MS5611 sensor and the configurations related to each of the MS5611 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ MS5611. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.    

USER REGISTER MAP

The registry map or sensor commands are defined in this section:

				
					1. #define MS5611_RESET                          		  (0x1E)
2. #define MS5611_PRESSURE_SAMPLING_START       		  (0x40)
3. #define MS5611_TEMPERATURE_SAMPLING_START		      (0x50)
4. #define MS5611_ADC_READ                       		  (0x00)
5. #define MS5611_PROM_READ                     		  (0xA0)

				
			

MS5611_Output_Sample_Rate Enum

The values of this enum are used to select the OSR of the sensor:

				
					1. typedef enum Output_Sample_Rate
2. {
3. OSR_256         = 0x00 ,                  
4. OSR_512         = 0x02 ,						
5. OSR_1024        = 0x04 ,				
6. OSR_2048        = 0x06 ,
7. OSR_4096        = 0x08                   
8. } MS5611_Output_Sample_Rate;

				
			

MS5611 struct

All sensor properties, calibration coefficients and sensor data are defined in this “struct” and All the information and configuration implemented on the sensor are stored in this “structure” and you can see the changes in each part of the sensor in the “Debug Session” environment.

				
					1. typedef struct MS5611
 2. {
 3. 	  uint8_t                         Register_Cache1;
 4. 	  MS5611_Output_Sample_Rate       PRESSURE_SAMPLE_RATE;
 5. 	  MS5611_Output_Sample_Rate       TEMPERATURE_SAMPLE_RATE;
 6. 	  uint8_t 		                  PROM_DATA[PROM_DATA_BUFFER_SIZE];
 7. 	  uint16_t                        FACTORY_DATA;
 8. 	  uint16_t                        C1;
 9. 	  uint16_t                        C2;
10. 	  uint16_t                        C3;
11. 	  uint16_t                        C4;
12. 	  uint16_t                        C5;
13. 	  uint16_t                        C6;
14. 	  uint16_t                        CRC_SERIAL_CODE;
15. 	  uint8_t 		                  ADC_DATA[ADC_DATA_BUFFER_SIZE];
16. 	  uint32_t               	      ADC_RAW_PRESSURE;
17.       uint32_t               	      ADC_RAW_TEMPERATURE;
18. 	  int32_t               	      DT;
19. 	  int64_t               	      T2;
20. 	  int64_t               	      OFF2;
21. 	  int64_t               	      SENS2;
22. 	  int64_t               	      OFF;
23. 	  int64_t                         SENS;
24. 	  float                           TEMPERATURE;
25. 	  float               	          PRESSURE;
26. 	  double 		                  ALTITUDE;
27. }GebraBit_MS5611;

				
			

Declaration of functions

At the end of this file, all the functions for reading and writing in MS5611 registers, sensor configuration and receiving data from the sensor are declared:

				
					1. /********************************************************
 2.  *Declare Read&Write MS5611 Register Values Functions *
 3.  ********************************************************/
 4. extern	uint8_t GB_MS5611_Burst_Read(uint8_t regAddr,uint8_t *data, uint16_t byteQuantity);
 5. extern	uint8_t GB_MS5611_Write_Reg_Data(uint8_t regAddr);
 6. /********************************************************
 7.  *       Declare MS5611 Configuration Functions       *
 8.  ********************************************************/
 9. extern void GB_MS5611_Soft_Reset ( GebraBit_MS5611 * MS5611 );
10. extern void GB_MS5611_Read_PROM ( GebraBit_MS5611 * MS5611 );
11. extern void GB_MS5611_Pressure_Sample_Rate(GebraBit_MS5611 * MS5611 , MS5611_Output_Sample_Rate rate);
12. extern void GB_MS5611_Temperature_Sample_Rate(GebraBit_MS5611 * MS5611 , MS5611_Output_Sample_Rate rate);
13. extern void	GB_MS5611_Start_Pressure_Sampling(GebraBit_MS5611 * MS5611);
14. extern void	GB_MS5611_Start_Temperature_Sampling(GebraBit_MS5611 * MS5611);
15. extern void GB_MS5611_Read_ADC ( GebraBit_MS5611 * MS5611 ) ;
16. extern void GB_MS5611_Read_ADC_Raw_Pressure(GebraBit_MS5611* MS5611);
17. extern void GB_MS5611_Read_ADC_Raw_Temperature(GebraBit_MS5611* MS5611);
18. extern void GB_MS5611_initialize( GebraBit_MS5611 * MS5611 );
19. extern void GB_MS5611_Calculate_Temperature(GebraBit_MS5611* MS5611);
20. extern void GB_MS5611_Calculate_Temperature_Compensated_Pressure(GebraBit_MS5611* MS5611);
21. extern void GB_MS5611_Altitude(GebraBit_MS5611 * MS5611);

				
			

GebraBit_ MS5611.c source file

In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.

Sample program in Keil

After making the Keil project by STM32CubeMX and adding the “GebraBit_ MS5611.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_MS5611 module in the “watch” part in the Keil compiler “Debugging” environment.

Description of “main.c” file

If you look carefully at the beginning part of the “main.c” file, you will notice that the “GebraBit_ MS5611.h” header has been added to access the GebraBit MS5611 module required structures, Enums and functions. In the next part, a variable named MS5611_Module of the GebraBit_MS5611 structure type (this structure is in the GebraBit_MS5611 header and is explained in the GebraBit_ MS5611 library description section) is defined for the configuration of the GebraBit MS5611 module:  

				
					/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_MS5611 MS5611_Module;
/* USER CODE END PTD */

				
			

In the next section of the written code, we initialize the GebraBit MS5611 module using the GB_MS5611_initialize (&MS5611_Module) function, and finally, in the while part of the program, the data is read from the sensor and the pressure, temperature, and height values are continuously received:

				
					1.   while (1)
 2.   {
 3.     /* USER CODE END WHILE */
 4.     /* USER CODE BEGIN 3 */
 5. 	GB_MS5611_Calculate_Temperature(&MS5611_Module);
 6. 	GB_MS5611_Calculate_Temperature_Compensated_Pressure(&MS5611_Module);
 7. 	GB_MS5611_Altitude(&MS5611_Module);   
 8.   }
 9.    /* USER CODE END 3 */
10. }
11.  

				
			

The “main.c” file code text:

				
					/* USER CODE BEGIN Header */
/*
 * ________________________________________________________________________________________________________
 * Copyright (c) 2020 GebraBit Inc. All rights reserved.
 *
 * This software, related documentation and any modifications thereto (collectively “Software”) is subject
 * to GebraBit and its licensors' intellectual property rights under U.S. and international copyright
 * and other intellectual property rights laws. 
 *
 * GebraBit and its licensors retain all intellectual property and proprietary rights in and to the Software
 * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
 * from GebraBit is strictly prohibited.
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT IN  
 * NO EVENT SHALL GebraBit BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THE SOFTWARE.
 * ________________________________________________________________________________________________________
 */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "GebraBit_MS5611.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_MS5611 MS5611_Module;
/* 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 ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_I2C1_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
GB_MS5611_initialize(&MS5611_Module);
  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
		GB_MS5611_Calculate_Temperature(&MS5611_Module);
		GB_MS5611_Calculate_Temperature_Compensated_Pressure(&MS5611_Module);
		GB_MS5611_Altitude(&MS5611_Module);   
  }
  /* 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_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/* 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 */

				
			

Program output

After generating the Keil project using STM32CubeMX and adding the library, we connect the STLINK V2 programmer to the GebraBit STM32F303 using the STLINKV2 adapter:

STLINKV2 adapter:

By connecting the STLINK V2 programmer to the GebraBit STM32F303, there is no need to apply power to the GebraBit STM32F303 and GebraBit MS5611 modules, because they receive their supply voltage directly from the STLINK V2 programmer.

Finally, enter the “Debug” mode and by adding the “MS5611_Module” to the “watch” window and running the program, we can see the changes in pressure, temperature, and height of the GebraBit MS5611 module:

In the following, you can download the “GebraBit MS5611 module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “MS5611 datasheet”.

Program output video

این مقاله را با دوستانتان به اشتراک بگذارید!

Be the first to write a review

Please help the Gebra team to improve the quality by sending comments and ratings

Your email address will not be published. Required fields are marked *

Shopping cart
Start typing to see posts you are looking for.

Sign in

No account yet?