add button to increment second

This commit is contained in:
King Kévin 2016-04-17 12:16:23 +02:00
parent 6b34d98394
commit 8e1678301c
2 changed files with 18 additions and 12 deletions

View File

@ -49,15 +49,15 @@
/** @defgroup board_button board user button GPIO
* @{
*/
#if defined(MAPLE_MINI)
//#if defined(MAPLE_MINI)
/* on maple mini user button is on 32/PB8 */
#define BUTTON_RCC RCC_GPIOB /**< GPIO peripheral clock (port B on maple mini) */
#define BUTTON_PORT GPIOB /**< GPIO port (port B on maple mini) */
#define BUTTON_PIN GPIO8 /**< GPIO pin (pin PB8 on maple mini) */
#define BUTTON_EXTI EXTI8 /**< GPIO external interrupt (exti 8 for pin 8 on maple mini) */
#define BUTTON_IRQ NVIC_EXTI9_5_IRQ /**< GPIO line interrupt (interrupt for line 9 to 5 for pin 8 on maple mini) */
#define BUTTON_ISR exti9_5_isr /**< GPIO line interrupt service routine (isr for line 9 to 5 for pin 8 on maple mini) */
#endif
#define BUTTON_RCC RCC_GPIOB /**< GPIO peripheral clock */
#define BUTTON_PORT GPIOB /**< GPIO port */
#define BUTTON_PIN GPIO8 /**< GPIO pin */
#define BUTTON_EXTI EXTI8 /**< GPIO external interrupt */
#define BUTTON_IRQ NVIC_EXTI9_5_IRQ /**< GPIO line interrupt (interrupt for line 9 to 5 for pin) */
#define BUTTON_ISR exti9_5_isr /**< GPIO line interrupt service routine (isr for line 9 to 5 for pin 8) */
//#endif
/** @} */
/** @brief switch on board LED */

14
main.c
View File

@ -360,10 +360,11 @@ int main(void)
// setup button
#if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ)
rcc_periph_clock_enable(BUTTON_RCC); // enable clock for button
gpio_set_mode(BUTTON_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BUTTON_PIN); // set button pin to input
gpio_set_mode(BUTTON_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, BUTTON_PIN); // set button pin to input
gpio_clear(BUTTON_PORT, BUTTON_PIN); // pull down to be able to detect button push (go high)
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_BOTH); // trigger on both edge
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger on both edge
exti_enable_request(BUTTON_EXTI); // enable external interrupt
nvic_enable_irq(BUTTON_IRQ); // enable interrupt
#endif
@ -488,9 +489,14 @@ int main(void)
}
}
while (button_flag) { // user pressed button
button_flag = false; // reset flag
action = true; // action has been performed
led_toggle(); // toggle LED
printf("time incremented by 1 second\n");
rtc_set_counter_val(rtc_get_counter_val()+ticks_second); // increment time
//led_toggle(); // toggle LED
for (uint32_t i=0; i<1000000; i++) { // wait a bit to remove noise and double trigger
__asm__("nop");
}
button_flag = false; // reset flag
}
while (time_flag) { // time passed
time_flag = false; // reset flag