Martin Johnson / accelerometer_test

Dependencies:   STM32F3-Discovery-minimal

Committer:
MartinJohnson
Date:
Wed May 02 04:14:23 2018 +0000
Revision:
4:7502a660b77f
Parent:
0:ed38888a34c2
A few small changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MartinJohnson 0:ed38888a34c2 1
MartinJohnson 0:ed38888a34c2 2 #include <stm32f3_discovery.h>
MartinJohnson 4:7502a660b77f 3 #include <stm32f3_discovery_lsm303dlhc.h>
MartinJohnson 0:ed38888a34c2 4
MartinJohnson 0:ed38888a34c2 5 void AccelerometerConfig(void)
MartinJohnson 0:ed38888a34c2 6 {
MartinJohnson 0:ed38888a34c2 7 LSM303DLHCMag_InitTypeDef LSM303DLHC_InitStructure;
MartinJohnson 0:ed38888a34c2 8 LSM303DLHCAcc_InitTypeDef LSM303DLHCAcc_InitStructure;
MartinJohnson 0:ed38888a34c2 9 LSM303DLHCAcc_FilterConfigTypeDef LSM303DLHCFilter_InitStructure;
MartinJohnson 0:ed38888a34c2 10
MartinJohnson 0:ed38888a34c2 11 /* Configure MEMS magnetometer main parameters: temp, working mode, full Scale and Data rate */
MartinJohnson 0:ed38888a34c2 12 LSM303DLHC_InitStructure.Temperature_Sensor = LSM303DLHC_TEMPSENSOR_DISABLE;
MartinJohnson 0:ed38888a34c2 13 LSM303DLHC_InitStructure.MagOutput_DataRate =LSM303DLHC_ODR_30_HZ ;
MartinJohnson 0:ed38888a34c2 14 LSM303DLHC_InitStructure.MagFull_Scale = LSM303DLHC_FS_8_1_GA;
MartinJohnson 0:ed38888a34c2 15 LSM303DLHC_InitStructure.Working_Mode = LSM303DLHC_CONTINUOS_CONVERSION;
MartinJohnson 0:ed38888a34c2 16 LSM303DLHC_MagInit(&LSM303DLHC_InitStructure);
MartinJohnson 0:ed38888a34c2 17
MartinJohnson 0:ed38888a34c2 18 /* Fill the accelerometer structure */
MartinJohnson 0:ed38888a34c2 19 LSM303DLHCAcc_InitStructure.Power_Mode = LSM303DLHC_NORMAL_MODE;
MartinJohnson 0:ed38888a34c2 20 LSM303DLHCAcc_InitStructure.AccOutput_DataRate = LSM303DLHC_ODR_50_HZ;
MartinJohnson 0:ed38888a34c2 21 LSM303DLHCAcc_InitStructure.Axes_Enable= LSM303DLHC_AXES_ENABLE;
MartinJohnson 0:ed38888a34c2 22 LSM303DLHCAcc_InitStructure.AccFull_Scale = LSM303DLHC_FULLSCALE_2G;
MartinJohnson 0:ed38888a34c2 23 LSM303DLHCAcc_InitStructure.BlockData_Update = LSM303DLHC_BlockUpdate_Continous;
MartinJohnson 0:ed38888a34c2 24 LSM303DLHCAcc_InitStructure.Endianness=LSM303DLHC_BLE_LSB;
MartinJohnson 0:ed38888a34c2 25 LSM303DLHCAcc_InitStructure.High_Resolution=LSM303DLHC_HR_ENABLE;
MartinJohnson 0:ed38888a34c2 26 /* Configure the accelerometer main parameters */
MartinJohnson 0:ed38888a34c2 27 LSM303DLHC_AccInit(&LSM303DLHCAcc_InitStructure);
MartinJohnson 0:ed38888a34c2 28
MartinJohnson 0:ed38888a34c2 29 /* Fill the accelerometer LPF structure */
MartinJohnson 0:ed38888a34c2 30 LSM303DLHCFilter_InitStructure.HighPassFilter_Mode_Selection =LSM303DLHC_HPM_NORMAL_MODE;
MartinJohnson 0:ed38888a34c2 31 LSM303DLHCFilter_InitStructure.HighPassFilter_CutOff_Frequency = LSM303DLHC_HPFCF_16;
MartinJohnson 0:ed38888a34c2 32 LSM303DLHCFilter_InitStructure.HighPassFilter_AOI1 = LSM303DLHC_HPF_AOI1_DISABLE;
MartinJohnson 0:ed38888a34c2 33 LSM303DLHCFilter_InitStructure.HighPassFilter_AOI2 = LSM303DLHC_HPF_AOI2_DISABLE;
MartinJohnson 0:ed38888a34c2 34
MartinJohnson 0:ed38888a34c2 35 /* Configure the accelerometer LPF main parameters */
MartinJohnson 0:ed38888a34c2 36 LSM303DLHC_AccFilterConfig(&LSM303DLHCFilter_InitStructure);
MartinJohnson 0:ed38888a34c2 37 }
MartinJohnson 0:ed38888a34c2 38
MartinJohnson 4:7502a660b77f 39 void ReadAccelerometer(int16_t * data) {
MartinJohnson 4:7502a660b77f 40 LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A, (uint8_t *)data, 6);
MartinJohnson 0:ed38888a34c2 41 }
MartinJohnson 0:ed38888a34c2 42
MartinJohnson 0:ed38888a34c2 43 volatile unsigned sysTiming;
MartinJohnson 0:ed38888a34c2 44 volatile unsigned sysTicks = 0;
MartinJohnson 0:ed38888a34c2 45
MartinJohnson 0:ed38888a34c2 46 void SysTick_Handler(void) {
MartinJohnson 0:ed38888a34c2 47 sysTicks++;
MartinJohnson 0:ed38888a34c2 48 if (sysTiming > 0) --sysTiming;
MartinJohnson 0:ed38888a34c2 49 }
MartinJohnson 0:ed38888a34c2 50
MartinJohnson 0:ed38888a34c2 51 void sysDelayMs(unsigned dly) {
MartinJohnson 0:ed38888a34c2 52 sysTiming = dly;
MartinJohnson 0:ed38888a34c2 53 while (sysTiming > 0) __wfi();
MartinJohnson 0:ed38888a34c2 54
MartinJohnson 0:ed38888a34c2 55 }
MartinJohnson 0:ed38888a34c2 56
MartinJohnson 0:ed38888a34c2 57 int main(void) {
MartinJohnson 0:ed38888a34c2 58
MartinJohnson 0:ed38888a34c2 59 SysTick_Config((SystemCoreClock / 1000));
MartinJohnson 0:ed38888a34c2 60 RCC->AHBENR |= RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOA;
MartinJohnson 0:ed38888a34c2 61 GPIOE->MODER = (GPIOE->MODER&0xffff) | 0x55550000; // output mode for PE8-15
MartinJohnson 0:ed38888a34c2 62 GPIOA->MODER = (GPIOA->MODER&0xfffffffc) ; // input mode for PA0
MartinJohnson 0:ed38888a34c2 63
MartinJohnson 0:ed38888a34c2 64 AccelerometerConfig();
MartinJohnson 0:ed38888a34c2 65 int16_t acc[3];
MartinJohnson 0:ed38888a34c2 66 int b=0;
MartinJohnson 4:7502a660b77f 67 int TH=200*16;
MartinJohnson 0:ed38888a34c2 68 while(1) {
MartinJohnson 0:ed38888a34c2 69
MartinJohnson 0:ed38888a34c2 70 ReadAccelerometer(acc);
MartinJohnson 4:7502a660b77f 71 printf("Accelerometer: %d %d %d\n",acc[0]/16,acc[1]/16,acc[2]/16);
MartinJohnson 0:ed38888a34c2 72 GPIOE->BSRR=b<<(8+16);
MartinJohnson 0:ed38888a34c2 73 b=0;
MartinJohnson 0:ed38888a34c2 74 if(acc[1]<-TH) {
MartinJohnson 0:ed38888a34c2 75 if(acc[0]<-TH) b=1<<4;
MartinJohnson 0:ed38888a34c2 76 else if(acc[0]>TH) b=1<<2;
MartinJohnson 0:ed38888a34c2 77 else b=1<<3;
MartinJohnson 0:ed38888a34c2 78 } else {
MartinJohnson 0:ed38888a34c2 79 if(acc[1]>TH) {
MartinJohnson 0:ed38888a34c2 80 if(acc[0]<-TH) b=1<<6;
MartinJohnson 0:ed38888a34c2 81 else if(acc[0]>TH) b=1<<0;
MartinJohnson 0:ed38888a34c2 82 else b=1<<7;
MartinJohnson 0:ed38888a34c2 83 } else {
MartinJohnson 0:ed38888a34c2 84 if(acc[0]<-TH) b=1<<5;
MartinJohnson 0:ed38888a34c2 85 else if(acc[0]>TH) b=1<<1;
MartinJohnson 0:ed38888a34c2 86 }
MartinJohnson 0:ed38888a34c2 87 }
MartinJohnson 0:ed38888a34c2 88 GPIOE->BSRR=b<<8;
MartinJohnson 0:ed38888a34c2 89 while(GPIOA->IDR&1);
MartinJohnson 0:ed38888a34c2 90 sysDelayMs(100);
MartinJohnson 0:ed38888a34c2 91
MartinJohnson 0:ed38888a34c2 92 }
MartinJohnson 0:ed38888a34c2 93 }
MartinJohnson 0:ed38888a34c2 94