From e1c963e10bc0c41eb4235ec1398c790a46a31963 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Tue, 20 Dec 2022 21:21:20 +0100 Subject: [PATCH] First PWM test --- CMakeLists.txt | 4 +++- src/main.c | 54 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94d5563..dfdeee2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,4 +17,6 @@ pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 1) pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 0) pico_add_extra_outputs(${CMAKE_PROJECT_NAME}) -target_link_libraries(${CMAKE_PROJECT_NAME} pico_stdlib) +target_link_libraries(${CMAKE_PROJECT_NAME} + pico_stdlib + hardware_pwm) diff --git a/src/main.c b/src/main.c index 59e7788..c8d3d0c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,40 +1,56 @@ #include "hardware/gpio.h" +#include "hardware/pwm.h" //#include #include -#define LED1_PIN 13 -#define LED2_PIN 14 -#define LED3_PIN 15 +#define LEDY_PIN 13 +#define LEDG_PIN 14 +#define LEDR_PIN 15 + +#define POWER_SWITCH_PIN 10 int main() { //stdio_init_all(); //printf("Hello, world!\n"); - gpio_init(LED1_PIN); - gpio_init(LED2_PIN); - gpio_init(LED3_PIN); + bool power_switch_state = false; - gpio_set_dir(LED1_PIN, true); - gpio_set_dir(LED2_PIN, true); - gpio_set_dir(LED3_PIN, true); + gpio_init(LEDY_PIN); + gpio_init(LEDG_PIN); + gpio_init(LEDR_PIN); - gpio_put(LED1_PIN, true); - gpio_put(LED2_PIN, true); - gpio_put(LED3_PIN, true); + gpio_set_dir(LEDY_PIN, true); + gpio_set_dir(LEDG_PIN, true); + gpio_set_dir(LEDR_PIN, true); + + gpio_put(LEDY_PIN, true); + gpio_put(LEDG_PIN, true); + gpio_put(LEDR_PIN, true); + + // set up PWM for 50 kHz @ 512 steps resolution + uint slice_num = pwm_gpio_to_slice_num(POWER_SWITCH_PIN); + + pwm_set_clkdiv_int_frac(slice_num, 4, (88 << 4) / 100); + gpio_set_function(POWER_SWITCH_PIN, GPIO_FUNC_PWM); + pwm_set_wrap(slice_num, 512); + pwm_set_chan_level(slice_num, PWM_CHAN_A, 410); + pwm_set_enabled(slice_num, true); sleep_ms(3000); + gpio_put(LEDY_PIN, false); + gpio_put(LEDG_PIN, false); + while (true) { - gpio_put(LED1_PIN, true); - gpio_put(LED2_PIN, false); - gpio_put(LED3_PIN, false); + gpio_put(LEDR_PIN, true); + gpio_put(LEDG_PIN, false); sleep_ms(500); - gpio_put(LED1_PIN, false); - gpio_put(LED2_PIN, true); + gpio_put(LEDY_PIN, true); + gpio_put(LEDR_PIN, false); sleep_ms(500); - gpio_put(LED2_PIN, false); - gpio_put(LED3_PIN, true); + gpio_put(LEDY_PIN, false); + gpio_put(LEDG_PIN, true); sleep_ms(500); } return 0;