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: mbed
main.cpp@1:73d739c64e4c, 2015-03-21 (annotated)
- Committer:
- dousape2
- Date:
- Sat Mar 21 11:21:32 2015 +0000
- Revision:
- 1:73d739c64e4c
- Parent:
- 0:1f39a86b8daa
- Child:
- 2:87dc86a7288b
Button added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dousape2 | 1:73d739c64e4c | 1 | /********************************************************************************** |
dousape2 | 1:73d739c64e4c | 2 | * @file main.cpp |
dousape2 | 1:73d739c64e4c | 3 | * @author Name |
dousape2 | 1:73d739c64e4c | 4 | * @version V0.1 |
dousape2 | 1:73d739c64e4c | 5 | * @date 09-March-2015 |
dousape2 | 1:73d739c64e4c | 6 | * @brief LEDs blinking with ticker. |
dousape2 | 1:73d739c64e4c | 7 | * With push button pressed, LEDs turn on. |
dousape2 | 1:73d739c64e4c | 8 | ***********************************************************************************/ |
dousape2 | 1:73d739c64e4c | 9 | |
dousape2 | 1:73d739c64e4c | 10 | /**********************************************************************************/ |
dousape2 | 1:73d739c64e4c | 11 | /* Table of used pins on STM32F3 Discovery kit */ |
dousape2 | 1:73d739c64e4c | 12 | /**********************************************************************************/ |
dousape2 | 1:73d739c64e4c | 13 | /* Discovery pin | ST Nucleo F303RE pin | peripheral */ |
dousape2 | 1:73d739c64e4c | 14 | /* PA_0 | PC_13 | User button */ |
dousape2 | 1:73d739c64e4c | 15 | /* PE_8 to PE_15 | PA_5 | LEDs */ |
dousape2 | 1:73d739c64e4c | 16 | /**********************************************************************************/ |
dousape2 | 1:73d739c64e4c | 17 | |
dousape2 | 1:73d739c64e4c | 18 | /* Includes ----------------------------------------------------------------------*/ |
dousape2 | 1:73d739c64e4c | 19 | |
dousape2 | 0:1f39a86b8daa | 20 | #include "mbed.h" |
dousape2 | 1:73d739c64e4c | 21 | #include "stm32f3xx_hal_gpio.h" //library necessary to blink LEDs on STM32F3 discovery |
dousape2 | 0:1f39a86b8daa | 22 | |
dousape2 | 1:73d739c64e4c | 23 | /* Defines -----------------------------------------------------------------------*/ |
dousape2 | 1:73d739c64e4c | 24 | |
dousape2 | 1:73d739c64e4c | 25 | // In some mbed libraries are not included these definines |
dousape2 | 1:73d739c64e4c | 26 | |
dousape2 | 1:73d739c64e4c | 27 | //#define GPIOE_BASE (AHB2PERIPH_BASE + 0x1000) |
dousape2 | 1:73d739c64e4c | 28 | //#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) |
dousape2 | 1:73d739c64e4c | 29 | //#define RCC_AHBENR_GPIOEEN ((uint32_t)0x00200000) |
dousape2 | 1:73d739c64e4c | 30 | //#define RCC_AHBPeriph_GPIOE RCC_AHBENR_GPIOEEN |
dousape2 | 1:73d739c64e4c | 31 | |
dousape2 | 1:73d739c64e4c | 32 | |
dousape2 | 1:73d739c64e4c | 33 | //mbed - initialization of peripherals |
dousape2 | 1:73d739c64e4c | 34 | InterruptIn button(PA_0); // inicialize button on STM32F3 discovery |
dousape2 | 1:73d739c64e4c | 35 | Ticker toggle_ticker; // inicialize ticker |
dousape2 | 1:73d739c64e4c | 36 | |
dousape2 | 1:73d739c64e4c | 37 | |
dousape2 | 1:73d739c64e4c | 38 | /* Functions----------------------------------------------------------------------*/ |
dousape2 | 1:73d739c64e4c | 39 | |
dousape2 | 1:73d739c64e4c | 40 | /******************************************************************************* |
dousape2 | 1:73d739c64e4c | 41 | * Function Name : toggle. |
dousape2 | 1:73d739c64e4c | 42 | * Description : Blinks whit 8 LEDs if ticker interval reached. |
dousape2 | 1:73d739c64e4c | 43 | * Input : None. |
dousape2 | 1:73d739c64e4c | 44 | * Output : Blinks whit 8 LEDs. |
dousape2 | 1:73d739c64e4c | 45 | * Return : None. |
dousape2 | 1:73d739c64e4c | 46 | *******************************************************************************/ |
dousape2 | 0:1f39a86b8daa | 47 | |
dousape2 | 0:1f39a86b8daa | 48 | void toggle() |
dousape2 | 0:1f39a86b8daa | 49 | { |
dousape2 | 1:73d739c64e4c | 50 | // Toggle pins PE_15 to PE_8 , where LEDs are attached to |
dousape2 | 1:73d739c64e4c | 51 | HAL_GPIO_TogglePin(GPIOE,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 | 0:1f39a86b8daa | 52 | } |
dousape2 | 0:1f39a86b8daa | 53 | |
dousape2 | 1:73d739c64e4c | 54 | /******************************************************************************* |
dousape2 | 1:73d739c64e4c | 55 | * Function Name : pressed. |
dousape2 | 1:73d739c64e4c | 56 | * Description : Set LEDs on if someone pressed the button. |
dousape2 | 1:73d739c64e4c | 57 | * Input : None. |
dousape2 | 1:73d739c64e4c | 58 | * Output : None. |
dousape2 | 1:73d739c64e4c | 59 | * Return : None. |
dousape2 | 1:73d739c64e4c | 60 | *******************************************************************************/ |
dousape2 | 1:73d739c64e4c | 61 | void pressed() |
dousape2 | 1:73d739c64e4c | 62 | { |
dousape2 | 1:73d739c64e4c | 63 | // Switch pins PE_15 to PE_8 to log. 1 , where LEDs are attached to |
dousape2 | 1:73d739c64e4c | 64 | 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 | GPIO_PIN_8 , GPIO_PIN_SET); |
dousape2 | 1:73d739c64e4c | 65 | } |
dousape2 | 1:73d739c64e4c | 66 | |
dousape2 | 1:73d739c64e4c | 67 | /*********************************************************************************** |
dousape2 | 1:73d739c64e4c | 68 | * Function Name : main. |
dousape2 | 1:73d739c64e4c | 69 | * Description : Main routine. |
dousape2 | 1:73d739c64e4c | 70 | * Input : None. |
dousape2 | 1:73d739c64e4c | 71 | * Output : None. |
dousape2 | 1:73d739c64e4c | 72 | * Return : None. |
dousape2 | 1:73d739c64e4c | 73 | ***********************************************************************************/ |
dousape2 | 0:1f39a86b8daa | 74 | |
dousape2 | 0:1f39a86b8daa | 75 | int main() |
dousape2 | 0:1f39a86b8daa | 76 | { |
dousape2 | 0:1f39a86b8daa | 77 | //inicialize power (clock source) to port E (GPIOE) |
dousape2 | 0:1f39a86b8daa | 78 | __GPIOE_CLK_ENABLE(); |
dousape2 | 1:73d739c64e4c | 79 | // RCC->AHBENR |= RCC_AHBPeriph_GPIOE; // if __GPIOE_CLK_ENABLE(); is not defined |
dousape2 | 0:1f39a86b8daa | 80 | |
dousape2 | 0:1f39a86b8daa | 81 | //inicialize pins |
dousape2 | 1:73d739c64e4c | 82 | // atructure to set GPIO |
dousape2 | 0:1f39a86b8daa | 83 | GPIO_InitTypeDef GPIO_InitStruct; |
dousape2 | 1:73d739c64e4c | 84 | //Specifies the operating mode for the selected pins. |
dousape2 | 1:73d739c64e4c | 85 | // Output Push Pull Mode |
dousape2 | 0:1f39a86b8daa | 86 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
dousape2 | 1:73d739c64e4c | 87 | //Specifies the Pull-up or Pull-Down activation for the selected pins. |
dousape2 | 1:73d739c64e4c | 88 | //Pull-up activation |
dousape2 | 0:1f39a86b8daa | 89 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
dousape2 | 1:73d739c64e4c | 90 | //Specifies the speed for the selected pins. |
dousape2 | 1:73d739c64e4c | 91 | //High speed |
dousape2 | 0:1f39a86b8daa | 92 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
dousape2 | 1:73d739c64e4c | 93 | //Specifies the GPIO pins to be configured. |
dousape2 | 1:73d739c64e4c | 94 | //Pins 15 to 8 |
dousape2 | 1:73d739c64e4c | 95 | 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:73d739c64e4c | 96 | |
dousape2 | 1:73d739c64e4c | 97 | //Inicialize pins PE_15 to PE_8 |
dousape2 | 0:1f39a86b8daa | 98 | HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); |
dousape2 | 0:1f39a86b8daa | 99 | |
dousape2 | 1:73d739c64e4c | 100 | // button was pressed, call function pressed |
dousape2 | 1:73d739c64e4c | 101 | button.fall(&pressed); |
dousape2 | 0:1f39a86b8daa | 102 | |
dousape2 | 1:73d739c64e4c | 103 | // Init the ticker with the address of the function (toggle) to be attached and the interval (1 s) |
dousape2 | 1:73d739c64e4c | 104 | toggle_ticker.attach(&toggle, 1); |
dousape2 | 1:73d739c64e4c | 105 | |
dousape2 | 1:73d739c64e4c | 106 | // infinity loop |
dousape2 | 0:1f39a86b8daa | 107 | while (1) { |
dousape2 | 0:1f39a86b8daa | 108 | } |
dousape2 | 0:1f39a86b8daa | 109 | } |