- /**
- ******************************************************************************
- * @file main.c
- * @author MCD Application Team
- * @version V1.1.2
- * @date 14-August-2015
- * @brief Program configures triaxial magnetometer and accelerometer.
- * This time to sensor configuration was used special prepared library from
- * LSM303DLH.c module. This module alow on faster sensor configuration. To configure sensor we have to
- * fill up all field of the configuration structure and execute configuration function.
- * Additionaly INT1 line was enabled. Thanks to them uC is informed about new data measured by sensor
- * ready to read.
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f30x.h"
- #include "Led.h"
- #include "Hardware.h"
- #include "Delay.h"
- #include "LSM303DLHC.h"
- #include "Time.h"
- /** @addtogroup STM32F30x_StdPeriph_Examples
- * @{
- */
- /** @addtogroup Basic_example
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- unsigned char measurementData[6];
- signed short rawData[3];
- float accData[3];
- float magData[3];
- int mag_period;
- /* Private function prototypes -----------------------------------------------*/
- void I2c_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- LSM303DLHCAcc_InitTypeDef acc_init;
- LSM303DLHCMag_InitTypeDef mag_init;
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32f30x.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32f30x.c file
- */
- /* configure basic modules, pins and clocks*/
- Hardware_Init();
- /* Time module initialisation */
- Time_Init();
- /* configure I2C1 port */
- I2c_Configuration();
- /* Accelerometer configuration */
- acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_16G;
- acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_200_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);
- //init INT1 line - on this line a high level will be set after the complete acc measurement
- LSM303DLHC_AccIT1Config(LSM303DLHC_IT1_DRY1,ENABLE);
- /* Magnetic sensor configuration */
- mag_init.MagFull_Scale=LSM303DLHC_FS_4_0_GA;
- mag_init.MagOutput_DataRate=LSM303DLHC_MAG_ODR_75_HZ;
- mag_init.Temperature_Sensor=LSM303DLHC_TEMPSENSOR_ENABLE;
- mag_init.Working_Mode=LSM303DLHC_CONTINUOS_CONVERSION;
- LSM303DLHC_MagInit(&mag_init);
- while(1)
- {
- //wait on high level on INT1 - PE4
- while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0);
- //wait on sensor measurements
- mag_period=Time_GetPeriod();
- //wait 20mss - this value comes frome selected sampling frequency (50Hz)
- //DelayUS(10000);
- //read acc data measurement
- LSM303DLHC_Read(0x32,0x28,&measurementData[0],6);
- //join two bytes to create short type variable
- 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;
- //scale measurements and express them in g units
- accData[0]=(float)rawData[0]*(float)0.012/16;
- accData[1]=(float)rawData[1]*(float)0.012/16;
- accData[2]=(float)rawData[2]*(float)0.012/16;
- //read mag data
- LSM303DLHC_Read(0x3C,0x03,&measurementData[0],6);
- //join two bytes to create short type variable
- rawData[0]=(signed short)measurementData[1]+(signed short)measurementData[0]*256;
- rawData[1]=(signed short)measurementData[3]+(signed short)measurementData[2]*256;
- rawData[2]=(signed short)measurementData[5]+(signed short)measurementData[4]*256;
- //scale measurements and express them in Gaus units
- magData[0]=(float)rawData[0]/LSM303DLHC_M_SENSITIVITY_XY_4Ga;
- magData[1]=(float)rawData[1]/LSM303DLHC_M_SENSITIVITY_XY_4Ga;
- magData[2]=(float)rawData[2]/LSM303DLHC_M_SENSITIVITY_Z_4Ga;
- }
- }
- void I2c_Configuration(void){
- I2C_InitTypeDef I2C_InitStructure;
- /* I2C configuration -------------------------------------------------------*/
- I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
- I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
- I2C_InitStructure.I2C_DigitalFilter = 0x00;
- I2C_InitStructure.I2C_OwnAddress1 = 0x00;
- I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
- I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
- I2C_InitStructure.I2C_Timing = 0x00902025;
- /* Apply LSM303DLHC_I2C configuration after enabling it */
- I2C_Init(I2C1, &I2C_InitStructure);
- /* LSM303DLHC_I2C Peripheral Enable */
- I2C_Cmd(I2C1, ENABLE);
- }
- #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 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) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif