Martin Johnson / Colour_Video_Demo

Dependencies:   STM32F3-Discovery-minimal

Fork of Space_Invaders_Demo by Martin Johnson

Committer:
MartinJohnson
Date:
Tue Mar 01 02:40:19 2016 +0000
Revision:
0:404dae88af71
Child:
1:1b37c4b989b4
space invaders game

Who changed what in which revision?

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