add buzzer program

This commit is contained in:
rmanach 2025-02-27 17:56:01 +01:00
parent 0db9d21ec6
commit 9c0f6b0552
2 changed files with 89 additions and 0 deletions

44
buzzer/CMakeLists.txt Normal file
View File

@ -0,0 +1,44 @@
# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Pull in Raspberry Pi Pico SDK (must be before project)
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
project(buzzer C CXX ASM)
# force picotool executable path
add_executable(picotool IMPORTED GLOBAL)
set_target_properties(picotool PROPERTIES
IMPORTED_LOCATION "$(PICOTOOL_PATH)/picotool"
)
# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()
# Add executable. Default name is the project name, version 0.1
add_executable(buzzer main.c )
pico_set_program_name(buzzer "buzzer")
pico_set_program_version(buzzer "0.1")
# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(buzzer 0)
pico_enable_stdio_usb(buzzer 1)
# Add the standard library to the build
target_link_libraries(buzzer
pico_stdlib hardware_pwm)
# Add the standard include files to the build
target_include_directories(buzzer PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
pico_add_extra_outputs(buzzer)

45
buzzer/main.c Normal file
View File

@ -0,0 +1,45 @@
#include <pico/printf.h>
#include <pico/stdlib.h>
#include <hardware/pwm.h>
#define GPIO_BUTTON 16
#define GPIO_BUZZER 15
#define NB_GPIO 2
typedef struct gpio gpio;
struct gpio {
size_t pin;
size_t direction;
};
static gpio GPIOS[2] = {
(gpio) {.pin = GPIO_BUZZER, .direction = GPIO_OUT},
(gpio) {
.pin = GPIO_BUTTON, .direction = GPIO_IN
}
};
void init_gpio() {
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
for (size_t i = 0; i < NB_GPIO; i++) {
gpio_init(GPIOS[i].pin);
gpio_set_dir(GPIOS[i].pin, GPIOS[i].direction);
}
}
int main() {
stdio_init_all();
init_gpio();
while (true) {
if (!gpio_get(GPIOS[1].pin)) {
gpio_put(PICO_DEFAULT_LED_PIN, true);
gpio_put(GPIOS[0].pin, true);
continue;
}
gpio_put(GPIOS[0].pin, false);
gpio_put(PICO_DEFAULT_LED_PIN, false);
}
}