First PWM test

This commit is contained in:
Thomas Kolb 2022-12-20 21:21:20 +01:00
parent 06ecbf4137
commit e1c963e10b
2 changed files with 38 additions and 20 deletions

View file

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

View file

@ -1,40 +1,56 @@
#include "hardware/gpio.h"
#include "hardware/pwm.h"
//#include <stdio.h>
#include <pico/stdlib.h>
#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;