~ruther/uni-mam-arm

91359abb1abbbf56f187591cc3b4b818fa64ffdf — Rutherther 4 months ago a3abb3a
feat(arm03): add debouncer timer for startstop button
1 files changed, 29 insertions(+), 11 deletions(-)

M arm03/src/main.c
M arm03/src/main.c => arm03/src/main.c +29 -11
@@ 69,8 69,10 @@ display_t display;

timer_t stopwatch_timer;
timer_t display_timer;
timer_t startstop_debounce_timer;

exti_t startstop_button;
exti_t startstop_exti;
pin_t startstop_button;
exti_t null_button;

void main()


@@ 80,8 82,8 @@ void main()
  systick_configure();

  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN;
  RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN | RCC_APB2ENR_TIM1EN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM4EN;
  RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;

  // The timer clocks are at 12 MHz.
  // For 100 Hz, the prescaler would be 120 000. That is too big,


@@ 97,10 99,14 @@ void main()
  exti_rising_interrupt(&null_button);
  exti_enable_interrupt(&null_button);

  exti_init(&startstop_button, 0, EXTI, SYSCFG);
  exti_external_interrupt(&startstop_button, EXTI_GPIOB);
  exti_rising_interrupt(&startstop_button);
  exti_enable_interrupt(&startstop_button);
  exti_init(&startstop_exti, 0, EXTI, SYSCFG);
  exti_external_interrupt(&startstop_exti, EXTI_GPIOB);
  exti_rising_interrupt(&startstop_exti);
  exti_enable_interrupt(&startstop_exti);

  pin_init(&startstop_button, GPIOB, 0);
  pin_into_output_pushpull(&startstop_button);
  pin_speed(&startstop_button, LOW_SPEED);

  {
    pin_t pin_data;


@@ 123,6 129,11 @@ void main()
  timer_configure(&stopwatch_timer, 0, 60000, 0);
  timer_set_refresh(&stopwatch_timer, 10000*4);

  timer_init(&startstop_debounce_timer, TIM4, 2);
  timer_configure(&startstop_debounce_timer, 0, 60000, 1);
  timer_set_refresh(&startstop_debounce_timer, 10);
  timer_enable_interrupt(&startstop_debounce_timer);

  timer_init(&display_timer, TIM3, 3);
  timer_configure(&display_timer, 0, 500, 0);
  timer_set_refresh(&display_timer, 4);


@@ 156,9 167,9 @@ void toggle_timer(void) {
}

void EXTI0_handler(void) {
  exti_clear_interrupt(&startstop_button);
  toggle_timer();
  exti_clear_interrupt(&startstop_button);
  exti_clear_interrupt(&startstop_exti);
  timer_set_counter(&startstop_debounce_timer, 0);
  timer_enable(&startstop_debounce_timer);
}

void EXTI4_handler(void) {


@@ 166,7 177,7 @@ void EXTI4_handler(void) {
  null_timer();
}

/* void TIM1_handler(void) { */
/* void TIM2_handler(void) { */
/*   // The stopwatch timer */
/* } */



@@ 174,3 185,10 @@ void TIM3_handler(void) {
  timer_clear_interrupt(&display_timer);
  display_update(&display);
}

void TIM4_handler(void) {
  timer_clear_interrupt(&startstop_debounce_timer);
  if (pin_read(&startstop_button)) {
    toggle_timer();
  }
}

Do not follow this link