/** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** ** This notice applies to any and all portions of this file * that are not between comment pairs USER CODE BEGIN and * USER CODE END. Other portions of this file, whether * inserted by the user or by software development tools * are owned by their respective copyright owners. * * COPYRIGHT(c) 2020 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f3xx_hal.h" #include "i2c.h" #include "gpio.h" /* USER CODE BEGIN Includes */ #include "LSM303DLHC.h" #include "math.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ unsigned char measurementData[6]; signed short rawData[3]; float accData[3]; float teta; float fi; float xfi; float xteta; signed short magData[3]; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ LSM303DLHCAcc_InitTypeDef acc_init; LSM303DLHCMag_InitTypeDef mag_init; /* 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(); /* USER CODE BEGIN 2 */ unsigned char recData[10]; HAL_I2C_Master_Receive(&hi2c1,0x31,recData,10,100); HAL_I2C_Mem_Read(&hi2c1,0x31,0x10,1,recData,10,100); acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_2G; acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_10_HZ; acc_init.Axes_Enable=LSM303DLHC_AXES_ENABLE; acc_init.BlockData_Update=LSM303DLHC_BlockUpdate_Continous; acc_init.Endianness=LSM303DLHC_BLE_LSB; acc_init.High_Resolution=LSM303DLHC_HR_ENABLE; acc_init.Power_Mode=LSM303DLHC_NORMAL_MODE; LSM303DLHC_AccInit(&acc_init); mag_init.MagFull_Scale=LSM303DLHC_FS_1_3_GA; mag_init.MagOutput_DataRate=LSM303DLHC_MAG_ODR_7_5_HZ; mag_init.Temperature_Sensor=LSM303DLHC_TEMPSENSOR_ENABLE; mag_init.Working_Mode=LSM303DLHC_CONTINUOS_CONVERSION; LSM303DLHC_MagInit(&mag_init); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_Delay(100); LSM303DLHC_Read(0x32,0x28 | 0x80,&measurementData[0],6); rawData[0]=(signed short)measurementData[0]+(signed short)measurementData[1]*256; rawData[1]=(signed short)measurementData[2]+(signed short)measurementData[3]*256; rawData[2]=(signed short)measurementData[4]+(signed short)measurementData[5]*256; accData[0]=(float)rawData[0]*(float)0.001/16; accData[1]=(float)rawData[1]*(float)0.001/16; accData[2]=(float)rawData[2]*(float)0.001/16; teta=asin(accData[0]); fi=asin(-accData[1]/(1.0*cos(teta))); teta=teta*(180/3.14); fi=fi*(180/3.14); if (fi>1) { HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET); } else if (fi<-1) { HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET); } else if (-11) { HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET); } else if (teta<-1) { HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9, GPIO_PIN_RESET); } else if (-1