From 508d9e248d1e568a87d2f889f2870b4d8b8153cc Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 1 Dec 2024 13:20:04 +0100 Subject: [PATCH] feat(arm03): add capture button --- arm03/src/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/arm03/src/main.c b/arm03/src/main.c index a72e1f4..67d05ed 100644 --- a/arm03/src/main.c +++ b/arm03/src/main.c @@ -1,8 +1,10 @@ +#include "stm32f401xe.h" +#include "timer.h" #include +#include #include #include "exti.h" #include "pin.h" -#include "timer.h" #include "registers.h" #include "display.h" #include "delay.h" @@ -75,6 +77,8 @@ exti_t startstop_exti; pin_t startstop_button; pin_t null_button; +bool counting; + void null_timer(void); void toggle_timer(void); @@ -132,9 +136,27 @@ void main() } timer_init(&stopwatch_timer, TIM2, 2); + timer_set_counter(&stopwatch_timer, 0); timer_configure(&stopwatch_timer, 0, 60000, 0); timer_set_refresh(&stopwatch_timer, 10000*4); + { + pin_t pin_capture; + pin_init(&pin_capture, GPIOA, 1); + pin_into_alternate(&pin_capture, 1); + + reg_write_bits(&stopwatch_timer.periph->CCMR1, + // TI2 on CCR1 no prescaler - capture every event sampling freq = fdts / 32, N = 8 + // fdts = 24 MHz, fsample = 750kHz + (2 << TIM_CCMR1_CC1S_Pos) | (0 << TIM_CCMR1_IC1PSC_Pos) | (0xF << TIM_CCMR1_IC1F_Pos), + TIM_CCMR1_CC1S | TIM_CCMR1_IC1PSC | TIM_CCMR1_IC1F); + reg_write_bits( + &stopwatch_timer.periph->CCER, + // enable CC1 rising edge captures + (1 << TIM_CCER_CC1E_Pos) | (0 << TIM_CCER_CC1NP_Pos) | (1 << TIM_CCER_CC1P_Pos), + TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC1NP); + } + timer_init(&startstop_debounce_timer, TIM4, 4); timer_configure(&startstop_debounce_timer, 0, 60000, 1); timer_set_refresh(&startstop_debounce_timer, 10); @@ -148,15 +170,37 @@ void main() __enable_irq(); + bool captured = false; + uint32_t capture = 0; + // Application while (1) { uint32_t count = 0; if (!pin_read(&null_button)) { null_timer(); + captured = false; } else { count = timer_count(&stopwatch_timer) >> 2; } + if (stopwatch_timer.periph->SR & TIM_SR_CC1IF) { + reg_clear_bits(&stopwatch_timer.periph->SR, TIM_SR_CC1IF); + + capture = stopwatch_timer.periph->CCR1 >> 2; + captured = true; + } else if (!counting) { + captured = false; + } + + if (captured && counting) { + // prevent capture wrapping + if (count < capture) { + count += 10000; + } + + count -= capture; + } + display_enable_digit(&display, 0, (count % display.max_value) >= 1000); display_convert_number(&display, count); @@ -172,8 +216,10 @@ void null_timer(void) { void toggle_timer(void) { if (timer_is_enabled(&stopwatch_timer)) { timer_disable(&stopwatch_timer); + counting = false; } else { timer_enable(&stopwatch_timer); + counting = true; } } @@ -183,11 +229,6 @@ void EXTI0_handler(void) { timer_enable(&startstop_debounce_timer); } -void EXTI4_handler(void) { - exti_clear_interrupt(&null_button); - null_timer(); -} - /* void TIM2_handler(void) { */ /* // The stopwatch timer */ /* } */ -- 2.48.1