feature(Lab 3):

Lab 3 done:
- Start and stop time
- stopwatch component done

Missing:
- Debounce button on START
This commit is contained in:
Devoalda 2023-09-11 14:34:37 +08:00
parent 663e87237a
commit e06d7be159
2 changed files with 137 additions and 0 deletions

51
lab_3/CMakeLists.txt Normal file
View File

@ -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)

86
lab_3/lab_3.c Normal file
View File

@ -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 <stdio.h>
#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();
}
}