Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: STM32F3-Discovery-minimal
main.c@0:ed38888a34c2, 2016-05-22 (annotated)
- Committer:
- MartinJohnson
- Date:
- Sun May 22 23:34:31 2016 +0000
- Revision:
- 0:ed38888a34c2
- Child:
- 1:c92d1851a036
- Child:
- 4:7502a660b77f
initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MartinJohnson | 0:ed38888a34c2 | 1 | |
| MartinJohnson | 0:ed38888a34c2 | 2 | #include <stm32f3_discovery.h> |
| MartinJohnson | 0:ed38888a34c2 | 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 | 0:ed38888a34c2 | 39 | void ReadAccelerometer(int16_t * data) |
| MartinJohnson | 0:ed38888a34c2 | 40 | { |
| MartinJohnson | 0:ed38888a34c2 | 41 | uint8_t buffer[6]; |
| MartinJohnson | 0:ed38888a34c2 | 42 | |
| MartinJohnson | 0:ed38888a34c2 | 43 | LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A, buffer, 6); |
| MartinJohnson | 0:ed38888a34c2 | 44 | |
| MartinJohnson | 0:ed38888a34c2 | 45 | for(int i=0; i<3; i++) { |
| MartinJohnson | 0:ed38888a34c2 | 46 | data[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i])/16; |
| MartinJohnson | 0:ed38888a34c2 | 47 | } |
| MartinJohnson | 0:ed38888a34c2 | 48 | } |
| MartinJohnson | 0:ed38888a34c2 | 49 | |
| MartinJohnson | 0:ed38888a34c2 | 50 | volatile unsigned sysTiming; |
| MartinJohnson | 0:ed38888a34c2 | 51 | volatile unsigned sysTicks = 0; |
| MartinJohnson | 0:ed38888a34c2 | 52 | |
| MartinJohnson | 0:ed38888a34c2 | 53 | void SysTick_Handler(void) { |
| MartinJohnson | 0:ed38888a34c2 | 54 | sysTicks++; |
| MartinJohnson | 0:ed38888a34c2 | 55 | if (sysTiming > 0) --sysTiming; |
| MartinJohnson | 0:ed38888a34c2 | 56 | } |
| MartinJohnson | 0:ed38888a34c2 | 57 | |
| MartinJohnson | 0:ed38888a34c2 | 58 | void sysDelayMs(unsigned dly) { |
| MartinJohnson | 0:ed38888a34c2 | 59 | sysTiming = dly; |
| MartinJohnson | 0:ed38888a34c2 | 60 | while (sysTiming > 0) __wfi(); |
| MartinJohnson | 0:ed38888a34c2 | 61 | |
| MartinJohnson | 0:ed38888a34c2 | 62 | } |
| MartinJohnson | 0:ed38888a34c2 | 63 | |
| MartinJohnson | 0:ed38888a34c2 | 64 | int main(void) { |
| MartinJohnson | 0:ed38888a34c2 | 65 | |
| MartinJohnson | 0:ed38888a34c2 | 66 | SysTick_Config((SystemCoreClock / 1000)); |
| MartinJohnson | 0:ed38888a34c2 | 67 | RCC->AHBENR |= RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOA; |
| MartinJohnson | 0:ed38888a34c2 | 68 | GPIOE->MODER = (GPIOE->MODER&0xffff) | 0x55550000; // output mode for PE8-15 |
| MartinJohnson | 0:ed38888a34c2 | 69 | GPIOA->MODER = (GPIOA->MODER&0xfffffffc) ; // input mode for PA0 |
| MartinJohnson | 0:ed38888a34c2 | 70 | |
| MartinJohnson | 0:ed38888a34c2 | 71 | AccelerometerConfig(); |
| MartinJohnson | 0:ed38888a34c2 | 72 | int16_t acc[3]; |
| MartinJohnson | 0:ed38888a34c2 | 73 | int b=0; |
| MartinJohnson | 0:ed38888a34c2 | 74 | int TH=200; |
| MartinJohnson | 0:ed38888a34c2 | 75 | while(1) { |
| MartinJohnson | 0:ed38888a34c2 | 76 | |
| MartinJohnson | 0:ed38888a34c2 | 77 | ReadAccelerometer(acc); |
| MartinJohnson | 0:ed38888a34c2 | 78 | GPIOE->BSRR=b<<(8+16); |
| MartinJohnson | 0:ed38888a34c2 | 79 | b=0; |
| MartinJohnson | 0:ed38888a34c2 | 80 | if(acc[1]<-TH) { |
| MartinJohnson | 0:ed38888a34c2 | 81 | if(acc[0]<-TH) b=1<<4; |
| MartinJohnson | 0:ed38888a34c2 | 82 | else if(acc[0]>TH) b=1<<2; |
| MartinJohnson | 0:ed38888a34c2 | 83 | else b=1<<3; |
| MartinJohnson | 0:ed38888a34c2 | 84 | } else { |
| MartinJohnson | 0:ed38888a34c2 | 85 | if(acc[1]>TH) { |
| MartinJohnson | 0:ed38888a34c2 | 86 | if(acc[0]<-TH) b=1<<6; |
| MartinJohnson | 0:ed38888a34c2 | 87 | else if(acc[0]>TH) b=1<<0; |
| MartinJohnson | 0:ed38888a34c2 | 88 | else b=1<<7; |
| MartinJohnson | 0:ed38888a34c2 | 89 | } else { |
| MartinJohnson | 0:ed38888a34c2 | 90 | if(acc[0]<-TH) b=1<<5; |
| MartinJohnson | 0:ed38888a34c2 | 91 | else if(acc[0]>TH) b=1<<1; |
| MartinJohnson | 0:ed38888a34c2 | 92 | } |
| MartinJohnson | 0:ed38888a34c2 | 93 | } |
| MartinJohnson | 0:ed38888a34c2 | 94 | GPIOE->BSRR=b<<8; |
| MartinJohnson | 0:ed38888a34c2 | 95 | while(GPIOA->IDR&1); |
| MartinJohnson | 0:ed38888a34c2 | 96 | sysDelayMs(100); |
| MartinJohnson | 0:ed38888a34c2 | 97 | |
| MartinJohnson | 0:ed38888a34c2 | 98 | } |
| MartinJohnson | 0:ed38888a34c2 | 99 | } |
| MartinJohnson | 0:ed38888a34c2 | 100 |