Facebook
From BERKAY, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 226
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.1.2
  6.   * @date    14-August-2015
  7.   * @brief Program configures triaxial magnetometer and accelerometer.  
  8.         * This time to sensor configuration was used special prepared library from
  9.         * LSM303DLH.c module. This module alow on faster sensor configuration. To configure sensor we have to
  10.   * fill up all field of the configuration structure and execute configuration function.
  11.         * Additionaly INT1 line was enabled. Thanks to them uC is informed about new data measured by sensor
  12.         * ready to read.
  13.   ******************************************************************************
  14.   */
  15.  
  16.  
  17. /* Includes ------------------------------------------------------------------*/
  18. #include "stm32f30x.h"
  19. #include "Led.h"
  20. #include "Hardware.h"
  21. #include "Delay.h"
  22. #include "LSM303DLHC.h"
  23. #include "Time.h"
  24.  
  25. /** @addtogroup STM32F30x_StdPeriph_Examples
  26.   * @{
  27.   */
  28.  
  29. /** @addtogroup Basic_example
  30.   * @{
  31.   */
  32.  
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /* Private macro -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. unsigned char measurementData[6];
  38. signed short rawData[3];
  39. float accData[3];
  40. float magData[3];
  41.  
  42. int mag_period;
  43. /* Private function prototypes -----------------------------------------------*/
  44. void I2c_Configuration(void);
  45. /* Private functions ---------------------------------------------------------*/
  46.  
  47. /**
  48.   * @brief  Main program.
  49.   * @param  None
  50.   * @retval None
  51.   */
  52. int main(void)
  53. {
  54.         LSM303DLHCAcc_InitTypeDef acc_init;
  55.         LSM303DLHCMag_InitTypeDef mag_init;
  56.   /*!< At this stage the microcontroller clock setting is already configured,
  57.        this is done through SystemInit() function which is called from startup
  58.        file (startup_stm32f30x.s) before to branch to application main.
  59.        To reconfigure the default setting of SystemInit() function, refer to
  60.        system_stm32f30x.c file
  61.      */
  62.         /* configure basic modules, pins and clocks*/
  63.   Hardware_Init();
  64.  
  65.        
  66.         /* Time module initialisation */
  67.         Time_Init();
  68.  
  69.         /* configure I2C1 port */
  70.         I2c_Configuration();
  71.        
  72.         /* Accelerometer configuration */
  73.        
  74.         acc_init.AccFull_Scale=LSM303DLHC_FULLSCALE_16G;
  75.         acc_init.AccOutput_DataRate=LSM303DLHC_ACC_ODR_200_HZ;
  76.         acc_init.Axes_Enable=LSM303DLHC_AXES_ENABLE;
  77.         acc_init.BlockData_Update=LSM303DLHC_BlockUpdate_Continous;
  78.         acc_init.Endianness=LSM303DLHC_BLE_LSB;
  79.         acc_init.High_Resolution=LSM303DLHC_HR_ENABLE;
  80.         acc_init.Power_Mode=LSM303DLHC_NORMAL_MODE;
  81.        
  82.         LSM303DLHC_AccInit(&acc_init);
  83.         //init INT1 line - on this line a high level will be set after the complete acc measurement
  84.         LSM303DLHC_AccIT1Config(LSM303DLHC_IT1_DRY1,ENABLE);
  85.        
  86.         /* Magnetic sensor configuration */
  87.        
  88.         mag_init.MagFull_Scale=LSM303DLHC_FS_4_0_GA;
  89.         mag_init.MagOutput_DataRate=LSM303DLHC_MAG_ODR_75_HZ;
  90.         mag_init.Temperature_Sensor=LSM303DLHC_TEMPSENSOR_ENABLE;
  91.         mag_init.Working_Mode=LSM303DLHC_CONTINUOS_CONVERSION;
  92.        
  93.         LSM303DLHC_MagInit(&mag_init);
  94.  
  95.   while(1)
  96.   {
  97.                 //wait on high level on INT1 - PE4
  98.                 while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0);
  99.                 //wait on sensor measurements
  100.                 mag_period=Time_GetPeriod();
  101.                 //wait 20mss - this value comes frome selected sampling frequency (50Hz)
  102.                 //DelayUS(10000);
  103.                 //read acc data measurement
  104.                 LSM303DLHC_Read(0x32,0x28,&measurementData[0],6);
  105.                 //join two bytes to create short type variable
  106.                 rawData[0]=(signed short)measurementData[0]+(signed short)measurementData[1]*256;
  107.                 rawData[1]=(signed short)measurementData[2]+(signed short)measurementData[3]*256;
  108.                 rawData[2]=(signed short)measurementData[4]+(signed short)measurementData[5]*256;
  109.                 //scale measurements and express them in g units
  110.                 accData[0]=(float)rawData[0]*(float)0.012/16;
  111.                 accData[1]=(float)rawData[1]*(float)0.012/16;
  112.                 accData[2]=(float)rawData[2]*(float)0.012/16;
  113.                
  114.                 //read mag data
  115.                 LSM303DLHC_Read(0x3C,0x03,&measurementData[0],6);
  116.                 //join two bytes to create short type variable
  117.                 rawData[0]=(signed short)measurementData[1]+(signed short)measurementData[0]*256;
  118.                 rawData[1]=(signed short)measurementData[3]+(signed short)measurementData[2]*256;
  119.                 rawData[2]=(signed short)measurementData[5]+(signed short)measurementData[4]*256;
  120.                 //scale measurements and express them in Gaus units
  121.                 magData[0]=(float)rawData[0]/LSM303DLHC_M_SENSITIVITY_XY_4Ga;
  122.                 magData[1]=(float)rawData[1]/LSM303DLHC_M_SENSITIVITY_XY_4Ga;
  123.                 magData[2]=(float)rawData[2]/LSM303DLHC_M_SENSITIVITY_Z_4Ga;
  124.                
  125.   }
  126. }
  127.  
  128. void I2c_Configuration(void){
  129.         I2C_InitTypeDef  I2C_InitStructure;
  130.           /* I2C configuration -------------------------------------------------------*/
  131.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  132.   I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  133.   I2C_InitStructure.I2C_DigitalFilter = 0x00;
  134.   I2C_InitStructure.I2C_OwnAddress1 = 0x00;
  135.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  136.   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  137.   I2C_InitStructure.I2C_Timing = 0x00902025;
  138.  
  139.   /* Apply LSM303DLHC_I2C configuration after enabling it */
  140.   I2C_Init(I2C1, &I2C_InitStructure);
  141.  
  142.   /* LSM303DLHC_I2C Peripheral Enable */
  143.   I2C_Cmd(I2C1, ENABLE);
  144. }
  145. #ifdef  USE_FULL_ASSERT
  146.  
  147. /**
  148.   * @brief  Reports the name of the source file and the source line number
  149.   *         where the assert_param error has occurred.
  150.   * @param  file: pointer to the source file name
  151.   * @param  line: assert_param error line source number
  152.   * @retval None
  153.   */
  154. void assert_failed(uint8_t* file, uint32_t line)
  155. {
  156.   /* User can add his own implementation to report the file name and line number,
  157.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  158.  
  159.   /* Infinite loop */
  160.   while (1)
  161.   {
  162.   }
  163. }
  164. #endif
  165.