Add pwm sound sample code
This commit is contained in:
19
Code/picocalc_helloworld/pwm_sound/CMakeLists.txt
Normal file
19
Code/picocalc_helloworld/pwm_sound/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
project(pwm_sound
|
||||
VERSION 0.0.1
|
||||
DESCRIPTION "pwm_sound for rp2040."
|
||||
)
|
||||
|
||||
add_library(pwm_sound INTERFACE)
|
||||
|
||||
target_sources(pwm_sound INTERFACE
|
||||
pwm_sound.c
|
||||
)
|
||||
|
||||
target_link_libraries(pwm_sound INTERFACE pico_stdlib hardware_pwm)
|
||||
|
||||
target_include_directories(pwm_sound INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
33
Code/picocalc_helloworld/pwm_sound/pwm_sound.c
Normal file
33
Code/picocalc_helloworld/pwm_sound/pwm_sound.c
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#include "pwm_sound.h"
|
||||
|
||||
|
||||
void init_pwm(irq_handler_t my_handler) {
|
||||
|
||||
gpio_set_function(AUDIO_PIN_L, GPIO_FUNC_PWM);
|
||||
gpio_set_function(AUDIO_PIN_R, GPIO_FUNC_PWM);
|
||||
|
||||
int slice_l = pwm_gpio_to_slice_num(AUDIO_PIN_L);
|
||||
int slice_r = pwm_gpio_to_slice_num(AUDIO_PIN_R);
|
||||
|
||||
|
||||
pwm_clear_irq(slice_l);
|
||||
pwm_clear_irq(slice_r);
|
||||
pwm_set_irq_enabled(slice_l, true);
|
||||
pwm_set_irq_enabled(slice_r, true);
|
||||
|
||||
irq_set_exclusive_handler(PWM_IRQ_WRAP, my_handler);
|
||||
irq_set_enabled(PWM_IRQ_WRAP, true);
|
||||
|
||||
|
||||
pwm_config config = pwm_get_default_config();
|
||||
pwm_config_set_clkdiv(&config, 6.05f); // 133MHz
|
||||
pwm_config_set_wrap(&config, 250);
|
||||
|
||||
|
||||
pwm_init(slice_l, &config, true);
|
||||
pwm_init(slice_r, &config, true);
|
||||
|
||||
pwm_set_chan_level(slice_l, PWM_CHAN_A, 0);
|
||||
pwm_set_chan_level(slice_r, PWM_CHAN_B, 0);
|
||||
}
|
||||
15
Code/picocalc_helloworld/pwm_sound/pwm_sound.h
Normal file
15
Code/picocalc_helloworld/pwm_sound/pwm_sound.h
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
#ifndef PWM_SOUND_H
|
||||
#define PWM_SOUND_H
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/pwm.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define PWM_CLOCK_KHZ 133000
|
||||
#define AUDIO_PIN_L 26
|
||||
#define AUDIO_PIN_R 27
|
||||
|
||||
void init_pwm(irq_handler_t);
|
||||
|
||||
#endif //PWM_SOUND_H
|
||||
Reference in New Issue
Block a user