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

Dependencies:   LSM303DLHC mbed

Committer:
Foxnec
Date:
Tue May 12 09:17:49 2015 +0000
Revision:
4:5be48963926c
Parent:
3:9b71b3bf2501
Changes to comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dousape2 1:4d3e9a507feb 1 /**********************************************************************************
dousape2 1:4d3e9a507feb 2 * @file main.cpp
dousape2 3:9b71b3bf2501 3 * @author Petr Dousa
dousape2 3:9b71b3bf2501 4 * @version V1.00
dousape2 3:9b71b3bf2501 5 * @date 22-March-2015
dousape2 2:781079d0c247 6 * @brief LEDs blinking with LSM303.
Foxnec 4:5be48963926c 7 * With push button pressed, LEDs are opposite.
dousape2 1:4d3e9a507feb 8 ***********************************************************************************/
dousape2 1:4d3e9a507feb 9
dousape2 1:4d3e9a507feb 10 /**********************************************************************************/
dousape2 1:4d3e9a507feb 11 /* Table of used pins on STM32F3 Discovery kit */
dousape2 1:4d3e9a507feb 12 /**********************************************************************************/
dousape2 1:4d3e9a507feb 13 /* Discovery pin | ST Nucleo F303RE pin | peripheral */
dousape2 1:4d3e9a507feb 14 /* PA_0 | PC_13 | User button */
dousape2 1:4d3e9a507feb 15 /* PE_8 to PE_15 | PA_5 | LEDs */
dousape2 1:4d3e9a507feb 16 /**********************************************************************************/
dousape2 1:4d3e9a507feb 17
dousape2 1:4d3e9a507feb 18 /* Includes ----------------------------------------------------------------------*/
dousape2 1:4d3e9a507feb 19
dousape2 0:6b2892e8a0b2 20 #include "mbed.h"
Foxnec 4:5be48963926c 21 #include "LSM303DLHC.h" //library necessary to comunicate with LSM303DLHC
dousape2 1:4d3e9a507feb 22 #include "stm32f3xx_hal_gpio.h" //library necessary to blink LEDs on STM32F3 discovery
dousape2 1:4d3e9a507feb 23
dousape2 1:4d3e9a507feb 24 /* Defines -----------------------------------------------------------------------*/
dousape2 1:4d3e9a507feb 25
Foxnec 4:5be48963926c 26 // In some mbed libraries these defines are not included
Foxnec 4:5be48963926c 27 // please uncomment if the example is not working
dousape2 0:6b2892e8a0b2 28
dousape2 1:4d3e9a507feb 29 //#define GPIOE_BASE (AHB2PERIPH_BASE + 0x1000)
dousape2 1:4d3e9a507feb 30 //#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE)
dousape2 1:4d3e9a507feb 31 //#define RCC_AHBENR_GPIOEEN ((uint32_t)0x00200000)
dousape2 1:4d3e9a507feb 32 //#define RCC_AHBPeriph_GPIOE RCC_AHBENR_GPIOEEN
dousape2 1:4d3e9a507feb 33
dousape2 3:9b71b3bf2501 34 /* Function prototypes -----------------------------------------------------------*/
dousape2 3:9b71b3bf2501 35
dousape2 1:4d3e9a507feb 36 /* Variables ---------------------------------------------------------------------*/
dousape2 0:6b2892e8a0b2 37 bool oppos = false;
dousape2 0:6b2892e8a0b2 38
dousape2 1:4d3e9a507feb 39 //mbed - initialization of peripherals
dousape2 1:4d3e9a507feb 40 InterruptIn my_button(PA_0);
dousape2 1:4d3e9a507feb 41
dousape2 1:4d3e9a507feb 42 //LSM303DLHC - initialization of peripherals
dousape2 0:6b2892e8a0b2 43 LSM303DLHC compass(PB_7, PB_6);
dousape2 0:6b2892e8a0b2 44
dousape2 1:4d3e9a507feb 45 /* Functions----------------------------------------------------------------------*/
dousape2 1:4d3e9a507feb 46
dousape2 1:4d3e9a507feb 47 /*******************************************************************************
dousape2 1:4d3e9a507feb 48 * Function Name : pressed.
dousape2 1:4d3e9a507feb 49 * Description : Blinks whit 8 LEDs if button is pressed and change variables.
dousape2 1:4d3e9a507feb 50 * Input : None.
dousape2 1:4d3e9a507feb 51 * Output : Blinks whit 8 LEDs and set variable.
dousape2 1:4d3e9a507feb 52 * Return : None.
dousape2 1:4d3e9a507feb 53 *******************************************************************************/
dousape2 0:6b2892e8a0b2 54 void pressed()
dousape2 0:6b2892e8a0b2 55 {
Foxnec 4:5be48963926c 56 // Toggle pins PE_15 to PE_8, where LEDs are attached to
dousape2 0:6b2892e8a0b2 57 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);
Foxnec 4:5be48963926c 58 //inverts oppos
dousape2 0:6b2892e8a0b2 59 oppos = !oppos;
dousape2 0:6b2892e8a0b2 60 }
dousape2 0:6b2892e8a0b2 61
dousape2 1:4d3e9a507feb 62 /***********************************************************************************
dousape2 1:4d3e9a507feb 63 * Function Name : main.
dousape2 1:4d3e9a507feb 64 * Description : Main routine.
dousape2 1:4d3e9a507feb 65 * Input : None.
dousape2 1:4d3e9a507feb 66 * Output : None.
dousape2 1:4d3e9a507feb 67 * Return : None.
dousape2 1:4d3e9a507feb 68 ***********************************************************************************/
dousape2 0:6b2892e8a0b2 69 int main()
dousape2 0:6b2892e8a0b2 70 {
dousape2 0:6b2892e8a0b2 71 float ax, ay, az;
dousape2 0:6b2892e8a0b2 72 float mx, my, mz;
dousape2 0:6b2892e8a0b2 73
Foxnec 4:5be48963926c 74 //initialize power (clock source) to port E (GPIOE)
dousape2 0:6b2892e8a0b2 75 __GPIOE_CLK_ENABLE();
dousape2 1:4d3e9a507feb 76 // RCC->AHBENR |= RCC_AHBPeriph_GPIOE; // if __GPIOE_CLK_ENABLE(); is not defined
dousape2 1:4d3e9a507feb 77
Foxnec 4:5be48963926c 78 // initialize pins
Foxnec 4:5be48963926c 79 // structure to set GPIO
dousape2 0:6b2892e8a0b2 80 GPIO_InitTypeDef GPIO_InitStruct;
Foxnec 4:5be48963926c 81 // Specifies the operating mode for the selected pins.
dousape2 1:4d3e9a507feb 82 // Output Push Pull Mode
dousape2 0:6b2892e8a0b2 83 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
Foxnec 4:5be48963926c 84 // Specifies the Pull-up or Pull-Down activation for the selected pins.
Foxnec 4:5be48963926c 85 // Pull-up activation
dousape2 0:6b2892e8a0b2 86 GPIO_InitStruct.Pull = GPIO_PULLUP;
Foxnec 4:5be48963926c 87 // Specifies the speed for the selected pins.
Foxnec 4:5be48963926c 88 // High speed
dousape2 0:6b2892e8a0b2 89 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
Foxnec 4:5be48963926c 90 // Specifies the GPIO pins to be configured.
Foxnec 4:5be48963926c 91 // Pins 15 to 8
dousape2 1:4d3e9a507feb 92 GPIO_InitStruct.Pin = GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_12 | GPIO_PIN_11 | GPIO_PIN_10 | GPIO_PIN_9 | GPIO_PIN_8;
dousape2 1:4d3e9a507feb 93
Foxnec 4:5be48963926c 94 // Initialize pins PE_15 to PE_8
dousape2 1:4d3e9a507feb 95 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
dousape2 1:4d3e9a507feb 96
dousape2 1:4d3e9a507feb 97 // button was pressed, call function pressed
dousape2 0:6b2892e8a0b2 98 my_button.fall(&pressed);
dousape2 0:6b2892e8a0b2 99
dousape2 0:6b2892e8a0b2 100 while (1) {
dousape2 1:4d3e9a507feb 101 //read value from compass
dousape2 0:6b2892e8a0b2 102 compass.read(&ax, &ay, &az, &mx, &my, &mz);
dousape2 1:4d3e9a507feb 103 //calculate angle
dousape2 0:6b2892e8a0b2 104 double heading = atan2(my, mx);
dousape2 0:6b2892e8a0b2 105 if (heading < 0)
dousape2 0:6b2892e8a0b2 106 heading += 2 * 3.14159265359;
dousape2 0:6b2892e8a0b2 107 heading=heading * (double)180 / 3.14159265359;
dousape2 1:4d3e9a507feb 108 //set LEDs
dousape2 1:4d3e9a507feb 109 if(heading>=338 || heading<23) { // compare angle
Foxnec 4:5be48963926c 110 // on value oppos set (reset) one LED and reset the other (set)
dousape2 0:6b2892e8a0b2 111 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 112 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 113 }
dousape2 0:6b2892e8a0b2 114 if(heading>=23 && heading<68) {
dousape2 0:6b2892e8a0b2 115 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 116 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 117 }
dousape2 0:6b2892e8a0b2 118 if(heading>=68 && heading<113) {
dousape2 0:6b2892e8a0b2 119 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 120 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 121 }
dousape2 0:6b2892e8a0b2 122 if(heading>=113 && heading<158) {
dousape2 0:6b2892e8a0b2 123 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 124 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 125 }
dousape2 0:6b2892e8a0b2 126 if(heading>=158 && heading<203) {
dousape2 0:6b2892e8a0b2 127 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 128 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 129 }
dousape2 0:6b2892e8a0b2 130 if(heading>=203 && heading<248) {
dousape2 0:6b2892e8a0b2 131 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 132 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 133 }
dousape2 0:6b2892e8a0b2 134 if(heading>=248 && heading<293) {
dousape2 0:6b2892e8a0b2 135 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 136 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 137 }
dousape2 0:6b2892e8a0b2 138 if(heading>=293 && heading<338) {
dousape2 0:6b2892e8a0b2 139 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,oppos?GPIO_PIN_SET:GPIO_PIN_RESET);
dousape2 0:6b2892e8a0b2 140 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 141 }
dousape2 0:6b2892e8a0b2 142 }
dousape2 0:6b2892e8a0b2 143 }
dousape2 0:6b2892e8a0b2 144