Example using the LSM303 MEMS compass onboard the STM32F3-Discovery board.
Dependencies: LSM303DLHC mbed
main.cpp@1:4d3e9a507feb, 2015-03-21 (annotated)
- Committer:
- dousape2
- Date:
- Sat Mar 21 16:20:45 2015 +0000
- Revision:
- 1:4d3e9a507feb
- Parent:
- 0:6b2892e8a0b2
- Child:
- 2:781079d0c247
snad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dousape2 | 1:4d3e9a507feb | 1 | /********************************************************************************** |
dousape2 | 1:4d3e9a507feb | 2 | * @file main.cpp |
dousape2 | 1:4d3e9a507feb | 3 | * @author Name |
dousape2 | 1:4d3e9a507feb | 4 | * @version V0.1 |
dousape2 | 1:4d3e9a507feb | 5 | * @date 09-March-2015 |
dousape2 | 1:4d3e9a507feb | 6 | * @brief LEDs blinking with ticker. |
dousape2 | 1:4d3e9a507feb | 7 | * With push button pressed, LEDs turn on. |
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" |
dousape2 | 1:4d3e9a507feb | 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 | |
dousape2 | 1:4d3e9a507feb | 26 | // In some mbed libraries are not included these definines |
dousape2 | 0:6b2892e8a0b2 | 27 | |
dousape2 | 1:4d3e9a507feb | 28 | //#define GPIOE_BASE (AHB2PERIPH_BASE + 0x1000) |
dousape2 | 1:4d3e9a507feb | 29 | //#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) |
dousape2 | 1:4d3e9a507feb | 30 | //#define RCC_AHBENR_GPIOEEN ((uint32_t)0x00200000) |
dousape2 | 1:4d3e9a507feb | 31 | //#define RCC_AHBPeriph_GPIOE RCC_AHBENR_GPIOEEN |
dousape2 | 1:4d3e9a507feb | 32 | |
dousape2 | 1:4d3e9a507feb | 33 | /* Variables ---------------------------------------------------------------------*/ |
dousape2 | 0:6b2892e8a0b2 | 34 | bool oppos = false; |
dousape2 | 0:6b2892e8a0b2 | 35 | |
dousape2 | 1:4d3e9a507feb | 36 | //mbed - initialization of peripherals |
dousape2 | 1:4d3e9a507feb | 37 | InterruptIn my_button(PA_0); |
dousape2 | 1:4d3e9a507feb | 38 | |
dousape2 | 1:4d3e9a507feb | 39 | //LSM303DLHC - initialization of peripherals |
dousape2 | 0:6b2892e8a0b2 | 40 | LSM303DLHC compass(PB_7, PB_6); |
dousape2 | 0:6b2892e8a0b2 | 41 | |
dousape2 | 1:4d3e9a507feb | 42 | /* Functions----------------------------------------------------------------------*/ |
dousape2 | 1:4d3e9a507feb | 43 | |
dousape2 | 1:4d3e9a507feb | 44 | /******************************************************************************* |
dousape2 | 1:4d3e9a507feb | 45 | * Function Name : pressed. |
dousape2 | 1:4d3e9a507feb | 46 | * Description : Blinks whit 8 LEDs if button is pressed and change variables. |
dousape2 | 1:4d3e9a507feb | 47 | * Input : None. |
dousape2 | 1:4d3e9a507feb | 48 | * Output : Blinks whit 8 LEDs and set variable. |
dousape2 | 1:4d3e9a507feb | 49 | * Return : None. |
dousape2 | 1:4d3e9a507feb | 50 | *******************************************************************************/ |
dousape2 | 0:6b2892e8a0b2 | 51 | void pressed() |
dousape2 | 0:6b2892e8a0b2 | 52 | { |
dousape2 | 1:4d3e9a507feb | 53 | // Toggle pins PE_15 to PE_8 , where LEDs are attached to |
dousape2 | 0:6b2892e8a0b2 | 54 | 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 | 1:4d3e9a507feb | 55 | // oposit value of oppos |
dousape2 | 0:6b2892e8a0b2 | 56 | oppos = !oppos; |
dousape2 | 0:6b2892e8a0b2 | 57 | } |
dousape2 | 0:6b2892e8a0b2 | 58 | |
dousape2 | 1:4d3e9a507feb | 59 | /*********************************************************************************** |
dousape2 | 1:4d3e9a507feb | 60 | * Function Name : main. |
dousape2 | 1:4d3e9a507feb | 61 | * Description : Main routine. |
dousape2 | 1:4d3e9a507feb | 62 | * Input : None. |
dousape2 | 1:4d3e9a507feb | 63 | * Output : None. |
dousape2 | 1:4d3e9a507feb | 64 | * Return : None. |
dousape2 | 1:4d3e9a507feb | 65 | ***********************************************************************************/ |
dousape2 | 0:6b2892e8a0b2 | 66 | int main() |
dousape2 | 0:6b2892e8a0b2 | 67 | { |
dousape2 | 0:6b2892e8a0b2 | 68 | float ax, ay, az; |
dousape2 | 0:6b2892e8a0b2 | 69 | float mx, my, mz; |
dousape2 | 0:6b2892e8a0b2 | 70 | |
dousape2 | 1:4d3e9a507feb | 71 | //inicialize power (clock source) to port E (GPIOE) |
dousape2 | 0:6b2892e8a0b2 | 72 | __GPIOE_CLK_ENABLE(); |
dousape2 | 1:4d3e9a507feb | 73 | // RCC->AHBENR |= RCC_AHBPeriph_GPIOE; // if __GPIOE_CLK_ENABLE(); is not defined |
dousape2 | 1:4d3e9a507feb | 74 | |
dousape2 | 1:4d3e9a507feb | 75 | //inicialize pins |
dousape2 | 1:4d3e9a507feb | 76 | // atructure to set GPIO |
dousape2 | 0:6b2892e8a0b2 | 77 | GPIO_InitTypeDef GPIO_InitStruct; |
dousape2 | 1:4d3e9a507feb | 78 | //Specifies the operating mode for the selected pins. |
dousape2 | 1:4d3e9a507feb | 79 | // Output Push Pull Mode |
dousape2 | 0:6b2892e8a0b2 | 80 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
dousape2 | 1:4d3e9a507feb | 81 | //Specifies the Pull-up or Pull-Down activation for the selected pins. |
dousape2 | 1:4d3e9a507feb | 82 | //Pull-up activation |
dousape2 | 0:6b2892e8a0b2 | 83 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
dousape2 | 1:4d3e9a507feb | 84 | //Specifies the speed for the selected pins. |
dousape2 | 1:4d3e9a507feb | 85 | //High speed |
dousape2 | 0:6b2892e8a0b2 | 86 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
dousape2 | 1:4d3e9a507feb | 87 | //Specifies the GPIO pins to be configured. |
dousape2 | 1:4d3e9a507feb | 88 | //Pins 15 to 8 |
dousape2 | 1:4d3e9a507feb | 89 | 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 | 90 | |
dousape2 | 1:4d3e9a507feb | 91 | //Inicialize pins PE_15 to PE_8 |
dousape2 | 1:4d3e9a507feb | 92 | HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); |
dousape2 | 1:4d3e9a507feb | 93 | |
dousape2 | 1:4d3e9a507feb | 94 | // button was pressed, call function pressed |
dousape2 | 0:6b2892e8a0b2 | 95 | my_button.fall(&pressed); |
dousape2 | 0:6b2892e8a0b2 | 96 | |
dousape2 | 0:6b2892e8a0b2 | 97 | while (1) { |
dousape2 | 1:4d3e9a507feb | 98 | //read value from compass |
dousape2 | 0:6b2892e8a0b2 | 99 | compass.read(&ax, &ay, &az, &mx, &my, &mz); |
dousape2 | 1:4d3e9a507feb | 100 | //calculate angle |
dousape2 | 0:6b2892e8a0b2 | 101 | double heading = atan2(my, mx); |
dousape2 | 0:6b2892e8a0b2 | 102 | if (heading < 0) |
dousape2 | 0:6b2892e8a0b2 | 103 | heading += 2 * 3.14159265359; |
dousape2 | 0:6b2892e8a0b2 | 104 | heading=heading * (double)180 / 3.14159265359; |
dousape2 | 1:4d3e9a507feb | 105 | //set LEDs |
dousape2 | 1:4d3e9a507feb | 106 | if(heading>=338 || heading<23) { // compare angle |
dousape2 | 1:4d3e9a507feb | 107 | // on value oppos set (reset) one LED and other reset (set) |
dousape2 | 0:6b2892e8a0b2 | 108 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 109 | 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 | 110 | } |
dousape2 | 0:6b2892e8a0b2 | 111 | if(heading>=23 && heading<68) { |
dousape2 | 0:6b2892e8a0b2 | 112 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 113 | 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 | 114 | } |
dousape2 | 0:6b2892e8a0b2 | 115 | if(heading>=68 && heading<113) { |
dousape2 | 0:6b2892e8a0b2 | 116 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 117 | 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 | 118 | } |
dousape2 | 0:6b2892e8a0b2 | 119 | if(heading>=113 && heading<158) { |
dousape2 | 0:6b2892e8a0b2 | 120 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 121 | 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 | 122 | } |
dousape2 | 0:6b2892e8a0b2 | 123 | if(heading>=158 && heading<203) { |
dousape2 | 0:6b2892e8a0b2 | 124 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 125 | 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 | 126 | } |
dousape2 | 0:6b2892e8a0b2 | 127 | if(heading>=203 && heading<248) { |
dousape2 | 0:6b2892e8a0b2 | 128 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_10,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 129 | 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 | 130 | } |
dousape2 | 0:6b2892e8a0b2 | 131 | if(heading>=248 && heading<293) { |
dousape2 | 0:6b2892e8a0b2 | 132 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 133 | 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 | 134 | } |
dousape2 | 0:6b2892e8a0b2 | 135 | if(heading>=293 && heading<338) { |
dousape2 | 0:6b2892e8a0b2 | 136 | HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,oppos?GPIO_PIN_SET:GPIO_PIN_RESET); |
dousape2 | 0:6b2892e8a0b2 | 137 | 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 | 138 | } |
dousape2 | 0:6b2892e8a0b2 | 139 | } |
dousape2 | 0:6b2892e8a0b2 | 140 | } |
dousape2 | 0:6b2892e8a0b2 | 141 |