From e06d7be1593d9bd7cc970ce027dab82b56612f01 Mon Sep 17 00:00:00 2001 From: Devoalda Date: Mon, 11 Sep 2023 14:34:37 +0800 Subject: [PATCH] feature(Lab 3): Lab 3 done: - Start and stop time - stopwatch component done Missing: - Debounce button on START --- lab_3/CMakeLists.txt | 51 ++++++++++++++++++++++++++ lab_3/lab_3.c | 86 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 lab_3/CMakeLists.txt create mode 100644 lab_3/lab_3.c diff --git a/lab_3/CMakeLists.txt b/lab_3/CMakeLists.txt new file mode 100644 index 0000000..b2b38a4 --- /dev/null +++ b/lab_3/CMakeLists.txt @@ -0,0 +1,51 @@ +# Generated Cmake Pico project file + +cmake_minimum_required(VERSION 3.13) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +# Initialise pico_sdk from installed location +# (note this can come from environment, CMake cache etc) +set(PICO_SDK_PATH "/home/junwei/Documents/pico/pico-sdk") + +set(PICO_BOARD pico_w CACHE STRING "Board type") + +# Pull in Raspberry Pi Pico SDK (must be before project) +include(pico_sdk_import.cmake) + +if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0") + message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}") +endif() + +project(lab_3 C CXX ASM) + +# Initialise the Raspberry Pi Pico SDK +pico_sdk_init() + +# Add executable. Default name is the project name, version 0.1 + +add_executable(lab_3 lab_3.c ) + +pico_set_program_name(lab_3 "lab_3") +pico_set_program_version(lab_3 "0.1") + +pico_enable_stdio_uart(lab_3 1) +pico_enable_stdio_usb(lab_3 1) + +# Add the standard library to the build +target_link_libraries(lab_3 + pico_stdlib) + +# Add the standard include files to the build +target_include_directories(lab_3 PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required +) + +# Add any user requested libraries +target_link_libraries(lab_3 + ) + +pico_add_extra_outputs(lab_3) + diff --git a/lab_3/lab_3.c b/lab_3/lab_3.c new file mode 100644 index 0000000..f96e5ff --- /dev/null +++ b/lab_3/lab_3.c @@ -0,0 +1,86 @@ +/** + * To develop a simple stopwatch that measures time intervals between + * button presses. + * + * On pressing the START pseudo-button (GP15), + * the stopwatch will begin, + * and the elapsed time will be displayed every second on + * the Serial Monitor (or equivalent). + * + * Releasing the START pseudo-button will stop the timer and + * reset the elapsed time to zero. + * + * The START pseudo-button must incorporate a debouncing algorithm. + * + */ + +#include +#include "pico/stdlib.h" +#include "hardware/gpio.h" + +#define PSEUDO_BTN 15 +#define DELAY_US 1000000 + +unsigned long long time = 0; +struct repeating_timer timer; + +bool repeating_timer_callback(struct repeating_timer *t) { + // Print elapsed time every second + printf("Elapsed Time: %llus\n", + (time_us_64() - time) / 1000000); + return true; +} + + +void gpio_callback(uint gpio, uint32_t events) { + // Start timer on button press + if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_FALL) + { + time = time_us_64(); + printf("Timer Started at %llu\n", time); + + add_repeating_timer_us( + DELAY_US, + repeating_timer_callback, + NULL, + &timer + ); + } + // Stop timer on button release + else if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_RISE) + { + printf("Timer Stopped at %llu\n", time_us_64()); + cancel_repeating_timer(&timer); + + // Print elapsed time and reset + printf("Final Elapsed Time: %llus\n", + (time_us_64() - time) / 1000000); + + time = 0; + } +} + +static void btn_init() +{ + gpio_init(PSEUDO_BTN); + gpio_set_dir(PSEUDO_BTN, GPIO_IN); + gpio_set_pulls(PSEUDO_BTN, true, false); + + gpio_set_irq_enabled_with_callback( + PSEUDO_BTN, + GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, + true, + &gpio_callback + ); +} + +int main() +{ + stdio_init_all(); + btn_init(); + + while (true) { + tight_loop_contents(); + } + +}