2021-03-03 14:55:52 +01:00

142 lines
3.2 KiB
C

/**
******************************************************************************
* @file BSP/Src/mems.c
* @author MCD Application Team
* @brief This example code shows how to use MEMS features.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "mems.h"
/** @addtogroup STM32F4xx_HAL_Examples
* @{
*/
/** @addtogroup BSP
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern __IO uint8_t UserPressButton;
/* Init af threshold to detect acceleration on MEMS */
/* Typical value:
- No acceleration: X, Y inferior to 100 (positive or negative)
- Max acceleration: X, Y around 2000 (positive or negative) */
int16_t ThresholdHigh = 200;
int16_t ThresholdLow = -200;
/* Private function prototypes -----------------------------------------------*/
static void ACCELERO_ReadAcc(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Test ACCELERATOR MEMS Hardware.
* The main objective of this test is to check acceleration on 2 axes X and Y
* @param None
* @retval None
*/
void ACCELERO_MEMS_Test(void)
{
/* Init Accelerometer MEMS */
if(BSP_ACCELERO_Init() != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
UserPressButton = 0;
while(!UserPressButton)
{
ACCELERO_ReadAcc();
}
}
/**
* @brief Read Acceleration data.
* @param None
* @retval None
*/
static void ACCELERO_ReadAcc(void)
{
/* Accelerometer variables */
int16_t buffer[3] = {0};
int16_t xval, yval = 0x00;
/* Read Acceleration */
BSP_ACCELERO_GetXYZ(buffer);
xval = buffer[0];
yval = buffer[1];
if((ABS(xval))>(ABS(yval)))
{
if(xval > ThresholdHigh)
{
/* LED5 On */
BSP_LED_On(LED5);
HAL_Delay(10);
}
else if(xval < ThresholdLow)
{
/* LED4 On */
BSP_LED_On(LED4);
HAL_Delay(10);
}
else
{
HAL_Delay(10);
}
}
else
{
if(yval < ThresholdLow)
{
/* LED6 On */
BSP_LED_On(LED6);
HAL_Delay(10);
}
else if(yval > ThresholdHigh)
{
/* LED3 On */
BSP_LED_On(LED3);
HAL_Delay(10);
}
else
{
HAL_Delay(10);
}
}
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
BSP_LED_Off(LED5);
BSP_LED_Off(LED6);
}
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/