GebraBit

IAM20381 Sensor Project With STM32F303 Microcontroller

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

IAM20381 gebrabit project

IAM20381 Sensor Project With STM32F303 Microcontroller

IAM20381 gebrabit project
  1. Home
  2. »
  3. motion
  4. »
  5. IAM20381 Sensor Project With STM32F303 Microcontroller

What's the purpose of this project?

In this section, we are going to launch the IAM20381 sensor using ARM microcontroller, STM32F series. In order to use more conveniently and optimally in this project, we use two ready modules GB301IM and GebraBit STM32F303. These two modules contain the minimum necessary elements of the IAM20381 sensor and the STM32F microcontroller, which are provided by the GebraBit team to facilitate the work.

What are we going to learn in this tutorial?

In this tutorial, in addition to setting up and using the IAM20381 sensor, you will get to know all the IAM20381 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 GB301IM 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

To do so, first we should select the SPI communication protocol by using the on-board jumpers and then we place the GebraBit IAM20381 module as Pin to Pin on the GebraBit STM32F303 module as shown  in the below picture :

Note: The above picture is intended only to show how the GebraBit IAM20381 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, in the “Watch1” window of the Keil compiler in “debug session” mode, you can observe the temperature and acceleration values ​​of the three “X, Y, Z” axes in real time.

STM32CubeMX settings

In the following, we will review the relevant settings for each of the ‘SPI’, ‘RCC’, ‘Debug’ and ‘Clock’ sections of the STM32F303 microcontroller for developing the GebraBit IAM20381 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

Since in the GebraBit STM32F303 module, access to the SWCLK and SWDIO pins is provided for the user, 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

In this code,the “clock” settings for each part of the STM32F303 microcontroller , are as you can see in the  below picture:

Project Manager settings

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

Once all these settings have been completed, simply click on the ‘GENERATE CODE’ button and you will be able to create a code in as little as one click by adding an IAM20381 library and driver provided by GebraBit.

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

IAM20381 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 “toturial” 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 IAM20381.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 IAM20381 sensor and the configurations related to each of the IAM20381 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ IAM20381. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.

IAM20381_Interface Enum

This enum is used to select the communication protocol with the sensor:

				
					typedef enum  interface
{  
 NOT_SPI = 0,                  						  
 IS_SPI                                 					
}IAM20381_Interface;

				
			

IAM20381_Soft_Reset_Config Enum

This enum is used for software reset of the sensor:

				
					typedef enum Soft_Reset_Config
{
IAM20381_RESET     = 0x01,                        
IAM20381_NOT_RESET = 0x00,              
} IAM20381_Soft_Reset_Config;

				
			

IAM20381_A_DLPF_CFG Enum

This enum is used to set the low pass filter of the Accelerometer sensor:

				
					typedef enum A_DLPF_CFG
{
	IAM20381_A_DLPF_CFG_218    = 1, 									
	IAM20381_A_DLPF_CFG_99     = 2,								 
	IAM20381_A_DLPF_CFG_45     = 3, 									
	IAM20381_A_DLPF_CFG_21     = 4, 									
	IAM20381_A_DLPF_CFG_10     = 5, 									
	IAM20381_A_DLPF_CFG_5      = 6, 								
	IAM20381_A_DLPF_CFG_420    = 7, 									
}   IAM20381_A_DLPF_CFG ;

				
			

IAM20381_ DLPF_CFG Enum

This enum is used to set the low pass filter of the temperature sensor:  

				
					typedef enum DLPF_CFG
{
	IAM20381_DLPF_CFG_4000   = 0, 						
	IAM20381_DLPF_CFG_188    = 1, 						
	IAM20381_DLPF_CFG_98     = 2, 								
	IAM20381_DLPF_CFG_42     = 3, 								
	IAM20381_DLPF_CFG_20 	 = 4, 								
	IAM20381_DLPF_CFG_10 	 = 5, 									
	IAM20381_DLPF_CFG_5      = 6, 									
	IAM20381_DLPF_CFG_4000_  = 7, 									
}   IAM20381_DLPF_CFG ;

				
			

IAM20381_Accel_Fs_Sel Enum

This enum is used to set the Full Scale Range of the sensor:

				
					typedef enum accel_fs_sel
{  
FS_2g = 0,                    							  
FS_4g    ,                							      
FS_8g    ,               							      
FS_16g            													
}IAM20381_Accel_Fs_Sel;

				
			

IAM20381_Accel_Scale_Factor Enum

The Scale Factor values corresponding to Full Scale Range are defined in this enum:   

				
					typedef enum Scale_Factor
{  
SCALE_FACTOR_2048_LSB_g  = 2048,           
SCALE_FACTOR_4096_LSB_g  = 4096,               
SCALE_FACTOR_8192_LSB_g  = 8192,                     
SCALE_FACTOR_16384_LSB_g = 16384           
}IAM20381_Accel_Scale_Factor;

				
			

IAM20381_FIFO_MODE Enum

The FIFO working mode of the sensor is set By using the values of this enum:

				
					typedef enum FIFO_Config
{  
STREAM_TO_FIFO      ,                          
STOP_ON_FULL_FIFO_SNAPSHOT                                   
}IAM20381_FIFO_MODE ;

				
			

IAM20381_Ability Enum

The FIFO working mode of the sensor is set By using the values of this enum:

				
					typedef enum Ability
{  
Disable = 0,                      
Enable     
}IAM20381_Ability;

				
			

IAM20381_Sleep Enum

The values of this enum are used to set the sensor working mode :

				
					typedef enum Sleep
{
IAM20381_AWAKE   = 0,					
IAM20381_SLEEP                                  
}IAM20381_Sleep ;

				
			

IAM20381_Clock_Source Enum

To determine the sensor clock source, the values of this enum are set:

				
					typedef enum Clock_Source
{  
INTERNAL_20MHZ_OSCILLATOR = 0,                      
AUTO_SELECT               = 1,                    			
CLOCK_STOP                = 7                                        
}IAM20381_Clock_Source ;

				
			

IAM20381_Power_Mode Enum

The values of this enum are used to set the sensor Power Mode:

				
					typedef enum Power_Mode
{
IAM20381_LOW_NOISE   = 0x03,        						
IAM20381_LOW_POWER   = 0x02,			
IAM20381_ACCEL_SLEEP = 0x01											              
} IAM20381_Power_Mode;

				
			

IAM20381_ Low_Power_Filter_AVG Enum

The values of this enum are used to determine the filter used in the sensor in Low Power mode:

				
					typedef enum LP_Averaging_Filter
{
  LP_AVERAGE_4_SAMPLES_FILTER  = 0 ,				 
  LP_AVERAGE_8_SAMPLES_FILTER  = 1 ,                 
  LP_AVERAGE_16_SAMPLES_FILTER = 2 ,	
  LP_AVERAGE_32_SAMPLES_FILTER = 3					
}IAM20381_LP_Averaging_Filter ;

				
			

IAM20381_Preparation Enum

This enum reflects the status of being ready or not for any data in the sensor:

				
					typedef enum Preparation
{  
IS_NOT_Ready = 0,                      
IS_Ready     
}IAM20381_Preparation; 

				
			

IAM20381_FCHOICEB Enum

The values of this enum are used to determine the DLPF filter:

				
					typedef enum FCHOICEB
{  
NOT_BYPASS_DLPF_FCHOICEB_0 = 0,                      
BYPASS_DLPF_FCHOICEB_1                               
}IAM20381_FCHOICEB;

				
			

IAM20381_Reset_Status Enum

The final status of the sensor software reset is expressed in this enum:

				
					typedef enum Reset_Status
{  
FAILED = 0,                      
DONE     
}IAM20381_Reset_Status;

				
			

IAM20381_FIFO_Ability Enum

This Enum is used to enable or disable FIFO:

				
					typedef enum FIFO_Ability
{  
FIFO_DISABLE = 0,                      
FIFO_ENABLE     
} IAM20381_FIFO_Ability;

				
			

IAM20381_Get_DATA Enum

How to receive data from the sensor is described in this enum:

				
					typedef enum Get_DATA
{  
FROM_REGISTER = 0,                      
FROM_FIFO     
} IAM20381_Get_DATA;

				
			

GebraBit_ IAM20381 structure

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.

Declaration of functions

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

				
					/********************************************************
 *Declare Read&Write IAM20381 Register Values Functions *
 ********************************************************/
extern	uint8_t	GB_IAM20381_Read_Reg_Data ( uint8_t regAddr, uint8_t* data);
extern	uint8_t GB_IAM20381_Read_Reg_Bits (uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t* data);
extern	uint8_t GB_IAM20381_Burst_Read(uint8_t regAddr, uint8_t *data, uint16_t byteQuantity);
extern	uint8_t GB_IAM20381_Write_Reg_Data(uint8_t regAddr, uint8_t data);
extern	uint8_t	GB_IAM20381_Write_Reg_Bits(uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t data);
extern	uint8_t GB_IAM20381_Burst_Write		( uint8_t regAddr,uint8_t *data, 	uint16_t byteQuantity);
/********************************************************
 *       Declare IAM20381 Configuration Functions       *
 ********************************************************/
 
extern void	GB_IAM20381_Who_am_I(GebraBit_IAM20381 * iam20381);  
 
/********************************************************
 *          Declare IAM20381 FIFO Functions             *
 ********************************************************/
extern void GB_IAM20381_SET_WOM_Threshold (GebraBit_IAM20381 * iam20381 ,IAM20381_Ability watermark , uint8_t wm);
extern void GB_IAM20381_Access_Serial_Interface_To_FIFO(GebraBit_IAM20381 * iam20381 ,IAM20381_Ability interface_access_fifo) ;
extern void GB_IAM20381_Write_TEMP_ACCEL_FIFO(GebraBit_IAM20381 * iam20381 ,IAM20381_Ability write_temp_fifo,IAM20381_Ability write_accel_fifo );
extern void GB_IAM20381_SET_FIFO_Mode(GebraBit_IAM20381 * iam20381 ,IAM0381_FIFO_MODE fmode);
extern void GB_IAM20381_FIFO_Reset(void);
extern void GB_IAM20381_Get_FIFO_Count(GebraBit_IAM20381 * iam20381 );
extern void GB_IAM20381_Read_FIFO(GebraBit_IAM20381 * iam20381 , uint16_t qty)  ;
extern void GB_IAM20381_FIFO_Data_Partition_ACCEL_XYZ_TEMP(GebraBit_IAM20381 * iam20381);
/********************************************************
 *          Declare IAM20381 ACCEL Functions             *
 ********************************************************/
extern void GB_IAM20381_Enable_Disable_XYZ_ACCEL(GebraBit_IAM20381 * iam20381 ,IAM20381_Ability x_axis,IAM20381_Ability y_axis,IAM20381_Ability z_axis );
extern void GB_IAM20381_Enable_SPI4_Disable_I2C(GebraBit_IAM20381 * iam20381 , IAM20381_Interface spisel);
extern void GB_IAM20381_Set_INT_Pin(GebraBit_IAM20381 * iam20381 , IAM20381_INT_Level level ,IAM20381_INT_Type type , IAM20381_Latch_Type latch );
extern void GB_IAM20381_Set_Clock_Source(GebraBit_IAM20381 * iam20381 , IAM20381_Clock_Source clk);
extern void GB_IAM20381_Sleep_Awake (GebraBit_IAM20381 * iam20381, IAM20381_Sleep  working  ) ;
extern void GB_IAM20381_Set_ACCEL_LN_Low_Pass_Filter  (GebraBit_IAM20381 * iam20381 , IAM20381_A_DLPF_CFG cfg) ;
extern void GB_IAM20381_Set_TEMP_Low_Pass_Filter  (GebraBit_IAM20381 * iam20381 , IAM20381_DLPF_CFG cfg) ;
extern void GB_IAM20381_Set_ACCEL_FS ( GebraBit_IAM20381 * iam20381 , IAM20381_Accel_Fs_Sel fs ) ;
extern void GB_IAM20381_Enable_Disable_Data_Ready_Interrupt(GebraBit_IAM20381 * iam20381 ,IAM20381_Ability data_int);
extern void GB_IAM20381_Set_ACCEL_Cycle(GebraBit_IAM20381 * iam20381 ,IAM20381_CYCLE cycle );
extern void GB_IAM20381_Sensor_Output_Sample_Rate (GebraBit_IAM20381 * iam20381 , uint16_t rate_hz);
extern void GB_IAM20381_LP_Averaging_Filter ( GebraBit_IAM20381 * iam20381 ,IAM20381_LP_Averaging_Filter filter);
/********************************************************
 *          Declare IAM20381 DATA Functions             *
 ********************************************************/
extern IAM20381_Preparation GB_IAM20381_Check_Data_Preparation(GebraBit_IAM20381 * iam20381);
extern IAM20381_Preparation GB_IAM20381_Check_FIFO_Overflow(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_Temp_Register_Raw_Data(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_Temp_Register_Valid_Data(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_X_Register_Raw(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_Y_Register_Raw(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_Z_Register_Raw(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_X_Register_Valid_Data(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_Y_Register_Valid_Data(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_DATA_Z_Register_Valid_Data(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_Temperature(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_XYZ_ACCELERATION(GebraBit_IAM20381 * iam20381);
/********************************************************
 *          Declare IAM20381 HIGH LEVEL Functions       *
 ********************************************************/
extern void GB_IAM20381_Set_Power_Management(GebraBit_IAM20381 * iam20381 , IAM20381_Power_Mode pmode);
extern void GB_IAM20381_FIFO_Configuration ( GebraBit_IAM20381 * IAM20381, IAM20381_Ability fifo  );
extern void GB_IAM20381_Soft_Reset ( GebraBit_IAM20381 * iam20381 );
extern void GB_IAM20381_Initialize( GebraBit_IAM20381 * iam20381 );
extern void GB_IAM20381_Configuration(GebraBit_IAM20381 * iam20381, IAM20381_FIFO_Ability fifo);
extern void GB_IAM20381_Get_ACCEL_XYZ_TEMP_From_Registers(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_ACCEL_XYZ_TEMP_From_FIFO(GebraBit_IAM20381 * iam20381);
extern void GB_IAM20381_Get_Data(GebraBit_IAM20381 * iam20381 , IAM20381_Get_DATA get_data);

				
			

GebraBit_ IAM20381.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_ IAM20381.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_ IAM20381 module in the “watch” part in the 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_ IAM20381.h” header has been added to access the structures and functions required by the GebraBit IAM20381 module. In the next part, a variable named IAM20381_Module of the GebraBit_IIM42352 structure type (this structure is in the GebraBit_ IAM20381 header and is explained in the GebraBit_ IAM20381 library description section) is defined for the configuration of the GebraBit IAM20381 module:

				
					/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_IAM20381 IAM20381_Module;
/* USER CODE END PTD */

				
			

In the next part of the written code, the configuration and settings of the GebraBit IAM20381 module should be done by using the GB_IAM20381_initialize() and GB_IAM20381_Configuration() functions:

				
						GB_IAM20381_Initialize( &IAM20381_Module );
	GB_IAM20381_Configuration(&IAM20381_Module ,FIFO_ENABLE);
             //GB_IAM20381_Configuration(&IAM20381_Module , FIFO_DISABLE );

				
			

at last, in the “while” part of the program, the values of GebraBit IAM20381 module temperature  and 3 axes (X, Y, Z)  are received continuously:

				
					GB_IAM20381_Get_Data( &IAM20381_Module , FROM_FIFO );
//GB_IAM20381_Get_Data(  &IAM20381_Module , FROM_REGISTER  );

				
			

By removing the GB_ IAM20381_Configuration(&IAM20381_Module , FIFO_DISABLE ); and GB_ IAM20381_Get_Data( & IAM20381_Module , FROM_REGISTER ) functions; Data values can be read directly from the data registers.

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
	* @Author       	: Mehrdad Zeinali
  ******************************************************************************
  * @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_IAM20381.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern GebraBit_IAM20381 IAM20381_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 */ 
  GB_IAM20381_Initialize(&IAM20381_Module);  
	//GB_IAM20381_Configuration(&IAM20381_Module , FIFO_ENABLE );	 
	GB_IAM20381_Configuration(&IAM20381_Module , FIFO_DISABLE );
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
     
    /* USER CODE BEGIN 3 */
		//GB_IAM20381_Get_Data(  &IAM20381_Module , FROM_FIFO  );
		GB_IAM20381_Get_Data(  &IAM20381_Module , FROM_REGISTER  );
  }
  /* 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 IAM20381 modules, because they receive their supply voltage directly from the STLINK V2 programmer.

finally, enter the “Debug” mode and by adding the “IAM20381_Module” to the “watch” window and running  the program, we can see the changes of temperature and the GebraBit IAM20381 module values  in the 3 axes( X, Y, Z) directly from the data registers and FIFO :

Receiving sensor data directly from data registers:

Receiving sensor data from FIFO:

In the following, you can download the “GebraBit IAM20381 module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “IAM20381 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?