Skip to content

Commit 14e68f2

Browse files
committed
Pico Tuner
0 parents  commit 14e68f2

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

pico-tuner/BuzzerExample.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include "pico/stdlib.h"
3+
#include "hardware/gpio.h"
4+
#include "hardware/pwm.h"
5+
6+
#define TOGGLE 20
7+
#define CHANGE_NOTE 21
8+
9+
int notes[6] = {
10+
82, // E
11+
110, // A
12+
147, // D
13+
196, // G
14+
246, // B
15+
330 // e
16+
};
17+
18+
// https://www.i-programmer.info/programming/hardware/14849-the-pico-in-c-basic-pwm.html?start=2
19+
uint32_t pwm_set_freq_duty(uint slice_num,
20+
uint chan,uint32_t f, int d)
21+
{
22+
uint32_t clock = 125000000;
23+
uint32_t divider16 = clock / f / 4096 +
24+
(clock % (f * 4096) != 0);
25+
if (divider16 / 16 == 0)
26+
divider16 = 16;
27+
uint32_t wrap = clock * 16 / divider16 / f - 1;
28+
pwm_set_clkdiv_int_frac(slice_num, divider16/16,
29+
divider16 & 0xF);
30+
pwm_set_wrap(slice_num, wrap);
31+
pwm_set_chan_level(slice_num, chan, wrap * d / 100);
32+
return wrap;
33+
}
34+
35+
int main()
36+
{
37+
// Init
38+
stdio_init_all();
39+
gpio_init(TOGGLE);
40+
gpio_init(CHANGE_NOTE);
41+
42+
gpio_set_dir(TOGGLE,GPIO_IN);
43+
gpio_set_dir(CHANGE_NOTE,GPIO_IN);
44+
45+
gpio_pull_up(TOGGLE);
46+
gpio_pull_up(CHANGE_NOTE);
47+
48+
gpio_set_function(18, GPIO_FUNC_PWM);
49+
50+
51+
uint slice_num = pwm_gpio_to_slice_num(18);
52+
uint chan = pwm_gpio_to_channel(18);
53+
uint play = 0;
54+
uint tone = 0;
55+
56+
while (true)
57+
{
58+
if(!gpio_get(TOGGLE)){
59+
play = 1 - play;
60+
sleep_ms(300);
61+
}
62+
63+
if(!gpio_get(CHANGE_NOTE)){
64+
65+
tone = (tone+1)%6;
66+
67+
pwm_set_freq_duty(slice_num,chan,notes[tone],50);
68+
sleep_ms(500);
69+
}
70+
71+
if(play){
72+
// Set the PWM running
73+
pwm_set_enabled(slice_num, true);
74+
} else{
75+
pwm_set_enabled(slice_num, false);
76+
}
77+
78+
}
79+
80+
return 0;
81+
}
82+

pico-tuner/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Generated Cmake Pico project file
2+
3+
cmake_minimum_required(VERSION 3.13)
4+
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD 17)
7+
8+
# Initialise pico_sdk from installed location
9+
# (note this can come from environment, CMake cache etc)
10+
set(PICO_SDK_PATH "/mnt/c/Users/champ/Documents/Programming/pico-sdk")
11+
12+
# Pull in Raspberry Pi Pico SDK (must be before project)
13+
include(pico_sdk_import.cmake)
14+
15+
project(BuzzerExample C CXX ASM)
16+
17+
# Initialise the Raspberry Pi Pico SDK
18+
pico_sdk_init()
19+
20+
# Add executable. Default name is the project name, version 0.1
21+
22+
add_executable(BuzzerExample BuzzerExample.c )
23+
24+
pico_set_program_name(BuzzerExample "BuzzerExample")
25+
pico_set_program_version(BuzzerExample "0.1")
26+
27+
pico_enable_stdio_uart(BuzzerExample 1)
28+
pico_enable_stdio_usb(BuzzerExample 0)
29+
30+
# Add the standard library to the build
31+
target_link_libraries(BuzzerExample pico_stdlib)
32+
33+
# Add any user requested libraries
34+
target_link_libraries(BuzzerExample
35+
hardware_gpio hardware_pwm
36+
)
37+
38+
pico_add_extra_outputs(BuzzerExample)
39+

0 commit comments

Comments
 (0)