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

Dependencies:   LSM303DLHC mbed

main.cpp

Committer:
dousape2
Date:
2015-03-10
Revision:
0:6b2892e8a0b2
Child:
1:4d3e9a507feb

File content as of revision 0:6b2892e8a0b2:

#include "mbed.h"
#include "LSM303DLHC.h"
#include "stm32f3xx_hal_gpio.h"

//DigitalOut  my_led(LED1);
InterruptIn my_button(PA_0);
bool oppos = false;

LSM303DLHC compass(PB_7, PB_6);

void pressed()
{
    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);
    oppos = !oppos;
}

int main()
{
    float ax, ay, az;
    float mx, my, mz;

    __GPIOE_CLK_ENABLE();
    GPIO_InitTypeDef        GPIO_InitStruct;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    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;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

    my_button.fall(&pressed);

    while (1) {
        compass.read(&ax, &ay, &az, &mx, &my, &mz);
        //výpočet uhlu a blikání diodama
        double heading = atan2(my, mx);
        if (heading < 0)
            heading += 2 * 3.14159265359;
        heading=heading * (double)180 / 3.14159265359;
        if(heading>=338 || heading<23) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=23 && heading<68) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=68 && heading<113) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=113 && heading<158) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=158 && heading<203) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=203 && heading<248) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=248 && heading<293) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
        if(heading>=293 && heading<338) {
            HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
            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);
        }
    }
}