feature(Debounce and Formatting):
Added Debounce Modified formatting and comments
This commit is contained in:
parent
e06d7be159
commit
9e31f48085
|
@ -1,4 +1,5 @@
|
||||||
/**
|
/**
|
||||||
|
* Lab 3
|
||||||
* To develop a simple stopwatch that measures time intervals between
|
* To develop a simple stopwatch that measures time intervals between
|
||||||
* button presses.
|
* button presses.
|
||||||
*
|
*
|
||||||
|
@ -18,21 +19,35 @@
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "hardware/gpio.h"
|
#include "hardware/gpio.h"
|
||||||
|
|
||||||
#define PSEUDO_BTN 15
|
#define PSEUDO_BTN 15 // GPIO 15 is the pseudo-button
|
||||||
#define DELAY_US 1000000
|
#define DELAY_US 1000000 // 1-second delay for repeating timer
|
||||||
|
#define DEBOUNCE_MS 200 // Debounce delay
|
||||||
|
|
||||||
unsigned long long time = 0;
|
unsigned long long time = 0; // Elapsed time
|
||||||
struct repeating_timer timer;
|
struct repeating_timer timer; // Repeating timer
|
||||||
|
|
||||||
bool repeating_timer_callback(struct repeating_timer *t) {
|
/**
|
||||||
|
* Callback function for repeating timer
|
||||||
|
* This function will print the elapsed time every second as required by the
|
||||||
|
* lab specification.
|
||||||
|
* @param t The timer that triggered the callback
|
||||||
|
* @return True to keep the timer running, false to stop it
|
||||||
|
*/
|
||||||
|
bool repeating_timer_callback (struct repeating_timer *t) {
|
||||||
// Print elapsed time every second
|
// Print elapsed time every second
|
||||||
printf("Elapsed Time: %llus\n",
|
printf("Elapsed Time: %llus\n",
|
||||||
(time_us_64() - time) / 1000000);
|
(time_us_64() - time) / 1000000);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
void gpio_callback(uint gpio, uint32_t events) {
|
* Interrupt callback function for pseudo-button (pull-up)
|
||||||
|
* This function will start the timer on button press (falling edge)
|
||||||
|
* and stop the timer on button release. (rising edge)
|
||||||
|
* @param gpio The GPIO pin that triggered the interrupt
|
||||||
|
* @param events The type of interrupt that triggered the callback
|
||||||
|
*/
|
||||||
|
void gpio_callback (uint gpio, uint32_t events) {
|
||||||
// Start timer on button press
|
// Start timer on button press
|
||||||
if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_FALL)
|
if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_FALL)
|
||||||
{
|
{
|
||||||
|
@ -44,24 +59,32 @@ void gpio_callback(uint gpio, uint32_t events) {
|
||||||
repeating_timer_callback,
|
repeating_timer_callback,
|
||||||
NULL,
|
NULL,
|
||||||
&timer
|
&timer
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Stop timer on button release
|
// Stop timer on button release
|
||||||
else if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_RISE)
|
else
|
||||||
{
|
{
|
||||||
printf("Timer Stopped at %llu\n", time_us_64());
|
if (gpio == PSEUDO_BTN && events == GPIO_IRQ_EDGE_RISE)
|
||||||
cancel_repeating_timer(&timer);
|
{
|
||||||
|
printf("Timer Stopped at %llu\n", time_us_64());
|
||||||
|
cancel_repeating_timer(&timer);
|
||||||
|
|
||||||
// Print elapsed time and reset
|
// Print elapsed time and reset
|
||||||
printf("Final Elapsed Time: %llus\n",
|
printf("Final Elapsed Time: %llus\n",
|
||||||
(time_us_64() - time) / 1000000);
|
(time_us_64() - time) / 1000000);
|
||||||
|
|
||||||
time = 0;
|
time = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_init()
|
/**
|
||||||
{
|
* Initialize the pseudo-button (pull-up)
|
||||||
|
* The pseudo-button is connected to GPIO 15 and is configured as an input
|
||||||
|
* with a pull-up resistor.
|
||||||
|
* The interrupt is configured to trigger on both rising and falling edges.
|
||||||
|
*/
|
||||||
|
static void btn_init () {
|
||||||
gpio_init(PSEUDO_BTN);
|
gpio_init(PSEUDO_BTN);
|
||||||
gpio_set_dir(PSEUDO_BTN, GPIO_IN);
|
gpio_set_dir(PSEUDO_BTN, GPIO_IN);
|
||||||
gpio_set_pulls(PSEUDO_BTN, true, false);
|
gpio_set_pulls(PSEUDO_BTN, true, false);
|
||||||
|
@ -74,13 +97,15 @@ static void btn_init()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main () {
|
||||||
{
|
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
btn_init();
|
btn_init();
|
||||||
|
|
||||||
while (true) {
|
while (true)
|
||||||
|
{
|
||||||
tight_loop_contents();
|
tight_loop_contents();
|
||||||
|
// Debounce
|
||||||
|
sleep_ms(DEBOUNCE_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue