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