Skip to content

Instantly share code, notes, and snippets.

@SealtielFreak
Created December 24, 2023 19:30
Show Gist options
  • Save SealtielFreak/bfeb60071777374a53a201ae29875a21 to your computer and use it in GitHub Desktop.
Save SealtielFreak/bfeb60071777374a53a201ae29875a21 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pico/stdlib.h>
#include "hardware/pwm.h"
#include "hardware/adc.h"
#define LED_PIN_BUILT 25
#define DEFAULT_WRAP_PWM 4096
#define DEFAULT_DIV_PWM 3
void pwm_init_pin(uint8_t pin, uint slice, uint chan, float div) {
gpio_set_function(pin, GPIO_FUNC_PWM);
pwm_config config = pwm_get_default_config();
pwm_config_set_clkdiv(&config, div);
pwm_config_set_wrap(&config, DEFAULT_WRAP_PWM);
pwm_init(slice, &config, true);
}
int main() {
stdio_init_all();
while (!set_sys_clock_khz(270000, true)) {
printf("Failure config clock");
}
adc_init();
adc_gpio_init(26);
adc_select_input(0);
adc_set_clkdiv(1919);
const uint8_t pwm_0 = 0;
uint chan_num_0 = pwm_gpio_to_channel(pwm_0);
uint slice_num_0 = pwm_gpio_to_slice_num(pwm_0);
pwm_init_pin(pwm_0, slice_num_0, chan_num_0, DEFAULT_DIV_PWM);
const uint8_t pwm_1 = 1;
uint chan_num_1 = pwm_gpio_to_channel(pwm_1);
uint slice_num_1 = pwm_gpio_to_slice_num(pwm_1);
pwm_init_pin(pwm_1, chan_num_1, slice_num_1, DEFAULT_DIV_PWM);
while (1) {
uint16_t signal = adc_read();
pwm_set_chan_level(slice_num_0, chan_num_0, signal);
pwm_set_chan_level(slice_num_1, chan_num_1, signal & 0x7ff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment