Port of Artekit space invaders demo (http://www.artekit.eu/space-invaders-for-stm32/) for the STM32F3 Discovery board. Also shows game of life if started with the user button pressed.
Dependencies: STM32F3-Discovery
compass.c@2:1c1f7677ac17, 2016-05-17 (annotated)
- Committer:
- MartinJohnson
- Date:
- Tue May 17 23:53:10 2016 +0000
- Revision:
- 2:1c1f7677ac17
- Parent:
- 1:1b37c4b989b4
Remove old logo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MartinJohnson | 1:1b37c4b989b4 | 1 | #include "stm32f30x.h" |
MartinJohnson | 1:1b37c4b989b4 | 2 | #include "stm32f3_discovery.h" |
MartinJohnson | 1:1b37c4b989b4 | 3 | #include <stdio.h> |
MartinJohnson | 1:1b37c4b989b4 | 4 | #include "stm32f3_discovery_lsm303dlhc.h" |
MartinJohnson | 1:1b37c4b989b4 | 5 | |
MartinJohnson | 1:1b37c4b989b4 | 6 | #define PI (float) 3.14159265f |
MartinJohnson | 1:1b37c4b989b4 | 7 | |
MartinJohnson | 1:1b37c4b989b4 | 8 | #define LSM_Acc_Sensitivity_2g (float) 1.0f /*!< accelerometer sensitivity with 2 g full scale [LSB/mg] */ |
MartinJohnson | 1:1b37c4b989b4 | 9 | #define LSM_Acc_Sensitivity_4g (float) 0.5f /*!< accelerometer sensitivity with 4 g full scale [LSB/mg] */ |
MartinJohnson | 1:1b37c4b989b4 | 10 | #define LSM_Acc_Sensitivity_8g (float) 0.25f /*!< accelerometer sensitivity with 8 g full scale [LSB/mg] */ |
MartinJohnson | 1:1b37c4b989b4 | 11 | #define LSM_Acc_Sensitivity_16g (float) 0.0834f /*!< accelerometer sensitivity with 12 g full scale [LSB/mg] */ |
MartinJohnson | 1:1b37c4b989b4 | 12 | #define ABS(x) (x < 0) ? (-x) : x |
MartinJohnson | 1:1b37c4b989b4 | 13 | |
MartinJohnson | 1:1b37c4b989b4 | 14 | void Demo_CompassConfig(void) |
MartinJohnson | 1:1b37c4b989b4 | 15 | { |
MartinJohnson | 1:1b37c4b989b4 | 16 | LSM303DLHCMag_InitTypeDef LSM303DLHC_InitStructure; |
MartinJohnson | 1:1b37c4b989b4 | 17 | LSM303DLHCAcc_InitTypeDef LSM303DLHCAcc_InitStructure; |
MartinJohnson | 1:1b37c4b989b4 | 18 | LSM303DLHCAcc_FilterConfigTypeDef LSM303DLHCFilter_InitStructure; |
MartinJohnson | 1:1b37c4b989b4 | 19 | |
MartinJohnson | 1:1b37c4b989b4 | 20 | /* Configure MEMS magnetometer main parameters: temp, working mode, full Scale and Data rate */ |
MartinJohnson | 1:1b37c4b989b4 | 21 | LSM303DLHC_InitStructure.Temperature_Sensor = LSM303DLHC_TEMPSENSOR_DISABLE; |
MartinJohnson | 1:1b37c4b989b4 | 22 | LSM303DLHC_InitStructure.MagOutput_DataRate =LSM303DLHC_ODR_30_HZ ; |
MartinJohnson | 1:1b37c4b989b4 | 23 | LSM303DLHC_InitStructure.MagFull_Scale = LSM303DLHC_FS_8_1_GA; |
MartinJohnson | 1:1b37c4b989b4 | 24 | LSM303DLHC_InitStructure.Working_Mode = LSM303DLHC_CONTINUOS_CONVERSION; |
MartinJohnson | 1:1b37c4b989b4 | 25 | LSM303DLHC_MagInit(&LSM303DLHC_InitStructure); |
MartinJohnson | 1:1b37c4b989b4 | 26 | |
MartinJohnson | 1:1b37c4b989b4 | 27 | /* Fill the accelerometer structure */ |
MartinJohnson | 1:1b37c4b989b4 | 28 | LSM303DLHCAcc_InitStructure.Power_Mode = LSM303DLHC_NORMAL_MODE; |
MartinJohnson | 1:1b37c4b989b4 | 29 | LSM303DLHCAcc_InitStructure.AccOutput_DataRate = LSM303DLHC_ODR_50_HZ; |
MartinJohnson | 1:1b37c4b989b4 | 30 | LSM303DLHCAcc_InitStructure.Axes_Enable= LSM303DLHC_AXES_ENABLE; |
MartinJohnson | 1:1b37c4b989b4 | 31 | LSM303DLHCAcc_InitStructure.AccFull_Scale = LSM303DLHC_FULLSCALE_2G; |
MartinJohnson | 1:1b37c4b989b4 | 32 | LSM303DLHCAcc_InitStructure.BlockData_Update = LSM303DLHC_BlockUpdate_Continous; |
MartinJohnson | 1:1b37c4b989b4 | 33 | LSM303DLHCAcc_InitStructure.Endianness=LSM303DLHC_BLE_LSB; |
MartinJohnson | 1:1b37c4b989b4 | 34 | LSM303DLHCAcc_InitStructure.High_Resolution=LSM303DLHC_HR_ENABLE; |
MartinJohnson | 1:1b37c4b989b4 | 35 | /* Configure the accelerometer main parameters */ |
MartinJohnson | 1:1b37c4b989b4 | 36 | LSM303DLHC_AccInit(&LSM303DLHCAcc_InitStructure); |
MartinJohnson | 1:1b37c4b989b4 | 37 | |
MartinJohnson | 1:1b37c4b989b4 | 38 | /* Fill the accelerometer LPF structure */ |
MartinJohnson | 1:1b37c4b989b4 | 39 | LSM303DLHCFilter_InitStructure.HighPassFilter_Mode_Selection =LSM303DLHC_HPM_NORMAL_MODE; |
MartinJohnson | 1:1b37c4b989b4 | 40 | LSM303DLHCFilter_InitStructure.HighPassFilter_CutOff_Frequency = LSM303DLHC_HPFCF_16; |
MartinJohnson | 1:1b37c4b989b4 | 41 | LSM303DLHCFilter_InitStructure.HighPassFilter_AOI1 = LSM303DLHC_HPF_AOI1_DISABLE; |
MartinJohnson | 1:1b37c4b989b4 | 42 | LSM303DLHCFilter_InitStructure.HighPassFilter_AOI2 = LSM303DLHC_HPF_AOI2_DISABLE; |
MartinJohnson | 1:1b37c4b989b4 | 43 | |
MartinJohnson | 1:1b37c4b989b4 | 44 | /* Configure the accelerometer LPF main parameters */ |
MartinJohnson | 1:1b37c4b989b4 | 45 | LSM303DLHC_AccFilterConfig(&LSM303DLHCFilter_InitStructure); |
MartinJohnson | 1:1b37c4b989b4 | 46 | } |
MartinJohnson | 0:404dae88af71 | 47 | |
MartinJohnson | 1:1b37c4b989b4 | 48 | void Demo_CompassReadAcc(float* pfData) |
MartinJohnson | 1:1b37c4b989b4 | 49 | { |
MartinJohnson | 1:1b37c4b989b4 | 50 | int16_t pnRawData[3]; |
MartinJohnson | 1:1b37c4b989b4 | 51 | uint8_t ctrlx[2]; |
MartinJohnson | 1:1b37c4b989b4 | 52 | uint8_t buffer[6], cDivider; |
MartinJohnson | 1:1b37c4b989b4 | 53 | uint8_t i = 0; |
MartinJohnson | 1:1b37c4b989b4 | 54 | float LSM_Acc_Sensitivity = LSM_Acc_Sensitivity_2g; |
MartinJohnson | 1:1b37c4b989b4 | 55 | |
MartinJohnson | 1:1b37c4b989b4 | 56 | /* Read the register content */ |
MartinJohnson | 1:1b37c4b989b4 | 57 | LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, ctrlx,2); |
MartinJohnson | 1:1b37c4b989b4 | 58 | LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A, buffer, 6); |
MartinJohnson | 1:1b37c4b989b4 | 59 | |
MartinJohnson | 1:1b37c4b989b4 | 60 | if(ctrlx[1]&0x40) |
MartinJohnson | 1:1b37c4b989b4 | 61 | cDivider=64; |
MartinJohnson | 1:1b37c4b989b4 | 62 | else |
MartinJohnson | 1:1b37c4b989b4 | 63 | cDivider=16; |
MartinJohnson | 1:1b37c4b989b4 | 64 | |
MartinJohnson | 1:1b37c4b989b4 | 65 | /* check in the control register4 the data alignment*/ |
MartinJohnson | 1:1b37c4b989b4 | 66 | if(!(ctrlx[0] & 0x40) || (ctrlx[1] & 0x40)) /* Little Endian Mode or FIFO mode */ |
MartinJohnson | 1:1b37c4b989b4 | 67 | { |
MartinJohnson | 1:1b37c4b989b4 | 68 | for(i=0; i<3; i++) |
MartinJohnson | 1:1b37c4b989b4 | 69 | { |
MartinJohnson | 1:1b37c4b989b4 | 70 | pnRawData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i])/cDivider; |
MartinJohnson | 1:1b37c4b989b4 | 71 | } |
MartinJohnson | 1:1b37c4b989b4 | 72 | } |
MartinJohnson | 1:1b37c4b989b4 | 73 | else /* Big Endian Mode */ |
MartinJohnson | 1:1b37c4b989b4 | 74 | { |
MartinJohnson | 1:1b37c4b989b4 | 75 | for(i=0; i<3; i++) |
MartinJohnson | 1:1b37c4b989b4 | 76 | pnRawData[i]=((int16_t)((uint16_t)buffer[2*i] << 8) + buffer[2*i+1])/cDivider; |
MartinJohnson | 1:1b37c4b989b4 | 77 | } |
MartinJohnson | 1:1b37c4b989b4 | 78 | /* Read the register content */ |
MartinJohnson | 1:1b37c4b989b4 | 79 | LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, ctrlx,2); |
MartinJohnson | 1:1b37c4b989b4 | 80 | |
MartinJohnson | 1:1b37c4b989b4 | 81 | |
MartinJohnson | 1:1b37c4b989b4 | 82 | if(ctrlx[1]&0x40) |
MartinJohnson | 1:1b37c4b989b4 | 83 | { |
MartinJohnson | 1:1b37c4b989b4 | 84 | /* FIFO mode */ |
MartinJohnson | 1:1b37c4b989b4 | 85 | LSM_Acc_Sensitivity = 0.25; |
MartinJohnson | 1:1b37c4b989b4 | 86 | } |
MartinJohnson | 1:1b37c4b989b4 | 87 | else |
MartinJohnson | 1:1b37c4b989b4 | 88 | { |
MartinJohnson | 1:1b37c4b989b4 | 89 | /* normal mode */ |
MartinJohnson | 1:1b37c4b989b4 | 90 | /* switch the sensitivity value set in the CRTL4*/ |
MartinJohnson | 1:1b37c4b989b4 | 91 | switch(ctrlx[0] & 0x30) |
MartinJohnson | 1:1b37c4b989b4 | 92 | { |
MartinJohnson | 1:1b37c4b989b4 | 93 | case LSM303DLHC_FULLSCALE_2G: |
MartinJohnson | 1:1b37c4b989b4 | 94 | LSM_Acc_Sensitivity = LSM_Acc_Sensitivity_2g; |
MartinJohnson | 1:1b37c4b989b4 | 95 | break; |
MartinJohnson | 1:1b37c4b989b4 | 96 | case LSM303DLHC_FULLSCALE_4G: |
MartinJohnson | 1:1b37c4b989b4 | 97 | LSM_Acc_Sensitivity = LSM_Acc_Sensitivity_4g; |
MartinJohnson | 1:1b37c4b989b4 | 98 | break; |
MartinJohnson | 1:1b37c4b989b4 | 99 | case LSM303DLHC_FULLSCALE_8G: |
MartinJohnson | 1:1b37c4b989b4 | 100 | LSM_Acc_Sensitivity = LSM_Acc_Sensitivity_8g; |
MartinJohnson | 1:1b37c4b989b4 | 101 | break; |
MartinJohnson | 1:1b37c4b989b4 | 102 | case LSM303DLHC_FULLSCALE_16G: |
MartinJohnson | 1:1b37c4b989b4 | 103 | LSM_Acc_Sensitivity = LSM_Acc_Sensitivity_16g; |
MartinJohnson | 1:1b37c4b989b4 | 104 | break; |
MartinJohnson | 1:1b37c4b989b4 | 105 | } |
MartinJohnson | 1:1b37c4b989b4 | 106 | } |
MartinJohnson | 1:1b37c4b989b4 | 107 | |
MartinJohnson | 1:1b37c4b989b4 | 108 | /* Obtain the mg value for the three axis */ |
MartinJohnson | 1:1b37c4b989b4 | 109 | for(i=0; i<3; i++) |
MartinJohnson | 1:1b37c4b989b4 | 110 | { |
MartinJohnson | 1:1b37c4b989b4 | 111 | pfData[i]=(float)pnRawData[i]/LSM_Acc_Sensitivity; |
MartinJohnson | 1:1b37c4b989b4 | 112 | } |
MartinJohnson | 1:1b37c4b989b4 | 113 | |
MartinJohnson | 1:1b37c4b989b4 | 114 | } |
MartinJohnson | 1:1b37c4b989b4 | 115 | |
MartinJohnson | 1:1b37c4b989b4 | 116 |