Example using the LSM303 MEMS compass onboard the STM32F3-Discovery board.

Dependencies:   LSM303DLHC mbed

Committer:
dousape2
Date:
Tue Mar 10 22:12:47 2015 +0000
Revision:
0:6b2892e8a0b2
Child:
1:4d3e9a507feb
Kompas s DiscoveryF3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dousape2 0:6b2892e8a0b2 1 #include "mbed.h"
dousape2 0:6b2892e8a0b2 2 #include "LSM303DLHC.h"
dousape2 0:6b2892e8a0b2 3 #include "stm32f3xx_hal_gpio.h"
dousape2 0:6b2892e8a0b2 4
dousape2 0:6b2892e8a0b2 5 //DigitalOut my_led(LED1);
dousape2 0:6b2892e8a0b2 6 InterruptIn my_button(PA_0);
dousape2 0:6b2892e8a0b2 7 bool oppos = false;
dousape2 0:6b2892e8a0b2 8
dousape2 0:6b2892e8a0b2 9 LSM303DLHC compass(PB_7, PB_6);
dousape2 0:6b2892e8a0b2 10
dousape2 0:6b2892e8a0b2 11 void pressed()
dousape2 0:6b2892e8a0b2 12 {
dousape2 0:6b2892e8a0b2 13 HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8);
dousape2 0:6b2892e8a0b2 14 oppos = !oppos;
dousape2 0:6b2892e8a0b2 15 }
dousape2 0:6b2892e8a0b2 16
dousape2 0:6b2892e8a0b2 17 int main()
dousape2 0:6b2892e8a0b2 18 {
dousape2 0:6b2892e8a0b2 19 float ax, ay, az;
dousape2 0:6b2892e8a0b2 20 float mx, my, mz;
dousape2 0:6b2892e8a0b2 21
dousape2 0:6b2892e8a0b2 22 __GPIOE_CLK_ENABLE();
dousape2 0:6b2892e8a0b2 23 GPIO_InitTypeDef GPIO_InitStruct;
dousape2 0:6b2892e8a0b2 24 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
dousape2 0:6b2892e8a0b2 25 GPIO_InitStruct.Pull = GPIO_PULLUP;
dousape2 0:6b2892e8a0b2 26 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
dousape2 0:6b2892e8a0b2 27 GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8;
dousape2 0:6b2892e8a0b2 28 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
dousape2 0:6b2892e8a0b2 29
dousape2 0:6b2892e8a0b2 30 my_button.fall(&pressed);
dousape2 0:6b2892e8a0b2 31
dousape2 0:6b2892e8a0b2 32 while (1) {
dousape2 0:6b2892e8a0b2 33 compass.read(&ax, &ay, &az, &mx, &my, &mz);
dousape2 0:6b2892e8a0b2 34 //výpočet uhlu a blikání diodama
dousape2 0:6b2892e8a0b2 35 double heading = atan2(my, mx);
dousape2 0:6b2892e8a0b2 36 if (heading < 0)
dousape2 0:6b2892e8a0b2 37 heading += 2 * 3.14159265359;
dousape2 0:6b2892e8a0b2 38 heading=heading * (double)180 / 3.14159265359;
dousape2 0:6b2892e8a0b2 39 if(heading>=338 || heading<23) {
dousape2 0:6b2892e8a0b2 40 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 41 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 42 }
dousape2 0:6b2892e8a0b2 43 if(heading>=23 && heading<68) {
dousape2 0:6b2892e8a0b2 44 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 45 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 46 }
dousape2 0:6b2892e8a0b2 47 if(heading>=68 && heading<113) {
dousape2 0:6b2892e8a0b2 48 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 49 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 50 }
dousape2 0:6b2892e8a0b2 51 if(heading>=113 && heading<158) {
dousape2 0:6b2892e8a0b2 52 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 53 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 54 }
dousape2 0:6b2892e8a0b2 55 if(heading>=158 && heading<203) {
dousape2 0:6b2892e8a0b2 56 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 57 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 58 }
dousape2 0:6b2892e8a0b2 59 if(heading>=203 && heading<248) {
dousape2 0:6b2892e8a0b2 60 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 61 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_9 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 62 }
dousape2 0:6b2892e8a0b2 63 if(heading>=248 && heading<293) {
dousape2 0:6b2892e8a0b2 64 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 65 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_8,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 66 }
dousape2 0:6b2892e8a0b2 67 if(heading>=293 && heading<338) {
dousape2 0:6b2892e8a0b2 68 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 69 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9,oppos?GPIO_PIN_RESET:GPIO_PIN_SET);
dousape2 0:6b2892e8a0b2 70 }
dousape2 0:6b2892e8a0b2 71 }
dousape2 0:6b2892e8a0b2 72 }
dousape2 0:6b2892e8a0b2 73