-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buzzer.c
279 lines (261 loc) · 7.19 KB
/
Buzzer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* @file Buzzer.c
* @author Leo Liu
* @brief on board buzzer
* @version 1.0
* @date 2022/07/25
*
* @copyright Copyright (c) 2022
*
*/
#include "Buzzer.h"
void Buzzer_Init(void);
void Buzzer_Play_Song(void);
Buzzer_Func_t Buzzer_Func = Buzzer_Func_GroundInit;
#undef Buzzer_Func_GroundInit
void Buzzer_On(uint16_t Volume, uint16_t Duration);
void Buzzer_Choose_Note(uint16_t Note);
Buzzer_t Buzzer;
uint16_t Mezase_Pokemon_Master[500][2];
void Buzzer_Play_Song(void)
{
for(int i = 0; i < 500; i++)
{
Buzzer_Choose_Note(Mezase_Pokemon_Master[i][0]);
Buzzer_On(NORMAL_VOLUME,Mezase_Pokemon_Master[i][1]);
}
}
//Enable timer's PWM function
void Buzzer_Init(void)
{
HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1);
}
//Turn on buzzer for set amount of duration
void Buzzer_On(uint16_t Volume, uint16_t Duration)
{
__HAL_TIM_SetCompare(&htim12, TIM_CHANNEL_1, Volume);
HAL_Delay(Duration);
__HAL_TIM_SetCompare(&htim12, TIM_CHANNEL_1, 0);
}
//Choose note, constants are calculated for desired frequency of each note
void Buzzer_Choose_Note(uint16_t Note)
{
switch(Note)
{
case 0:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,0);
break;
}
case Low_Do:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,3821);
break;
}
case Low_Do_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,3608);
break;
}
case Low_Re:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,3404);
break;
}
case Low_Re_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,3214);
break;
}
case Low_Mi:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,3033);
break;
}
case Low_Fa:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2863);
break;
}
case Low_Fa_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2703);
break;
}
case Low_So:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2550);
break;
}
case Low_So_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2408);
break;
}
case Low_La:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2272);
break;
}
case Low_La_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2145);
break;
}
case Low_Ti:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,2024);
break;
}
case Mid_Do:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1910);
break;
}
case Mid_Do_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1804);
break;
}
case Mid_Re:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1702);
break;
}
case Mid_Re_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1607);
break;
}
case Mid_Mi:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1516);
break;
}
case Mid_Fa:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1431);
break;
}
case Mid_Fa_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1351);
break;
}
case Mid_So:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1275);
break;
}
case Mid_So_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1204);
break;
}
case Mid_La:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1135);
break;
}
case Mid_La_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1073);
break;
}
case Mid_Ti:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,1011);
break;
}
case High_Do:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,955);
break;
}
case High_Do_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,902);
break;
}
case High_Re:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,850);
break;
}
case High_Re_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,804);
break;
}
case High_Mi:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,757);
break;
}
case High_Fa:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,715);
break;
}
case High_Fa_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,676);
break;
}
case High_So:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,637);
break;
}
case High_So_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,602);
break;
}
case High_La:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,567);
break;
}
case High_La_N:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,536);
break;
}
case High_Ti:
{
__HAL_TIM_SET_AUTORELOAD(&htim12,505);
break;
}
}
}
uint16_t Mezase_Pokemon_Master[500][2] =
{
{Low_La,125},{0,20},{Low_La,125},{0,20},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},
{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,375},{0,20},{Low_La,250},{Mid_Do,250},
{0,20},{Mid_Do,125},{0,20},{Mid_Do,250},{Low_Ti,375},{0,750},
{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},
{Mid_Do,125},{Low_La,250},{0,20},{Low_La,125},{Mid_Do,125},{Low_La,250},{0,20},{Low_La,125},
{Mid_Mi,250},{0,20},{Mid_Mi,250},{Mid_Re,125},{Mid_Do,250},{Low_Ti,375},{0,750},
{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},
{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},{Mid_Do,125},{Low_La,125},
{0,20},{Low_La,250},{Mid_Do,250},{0,20},{Mid_Do,125},{Mid_Re,250},{Low_Ti,250},{0,500},
{Low_La,125},{0,20},{Low_La,125},{Mid_Do,125},{Low_La,375},{Mid_Mi,250},{Mid_Re,250},{Mid_Do,125},{Low_La,375},{0,250},
{Low_La,125},{0,20},{Low_La,125},{Mid_Do,250},{Low_La,250},{0,500},{Mid_La,250},{Mid_So,250},{Mid_Mi,250},{Mid_Re,250},
{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,250},{Mid_Fa_N,250},
{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,500},{0,500},
{Mid_Do_N,125},{0,20},{Mid_Do_N,125},{0,20},{Mid_Do_N,250},{0,20},{Mid_Do_N,125},{0,20},{Mid_Do_N,250},{0,20},{Mid_Do_N,375},{0,250},
{Low_La,125},{0,20},{Low_La,125},{0,20},{Low_La,125},{Mid_Do_N,125},{Low_Ti,250},{Mid_Mi,500},{0,1250},
{Mid_Do_N,125},{0,20},{Mid_Do_N,125},{0,20},{Mid_Do_N,250},{0,20},{Mid_Do_N,125},{0,20},{Mid_Do_N,125},{0,20},{Mid_Do_N,125},{0,20},
{Mid_Do_N,375},{0,250},{Low_La,125},{0,20},{Low_La,125},{0,20},{Low_La,125},{Mid_Do_N,125},{Low_Ti,500},{0,1250},
{Low_Ti,250},{Low_La,250},{Low_Fa_N,250},{Low_La,250},{Low_Ti,250},{Mid_Do_N,250},{Low_La,500},{0,250},{Low_La,250},{0,20},{Low_La,250},
{0,20},{Low_La,250},{Mid_Mi,250},{Mid_Do_N,500},{0,250},{Low_La,250},{Mid_Do_N,250},{0,20},{Mid_Do_N,250},{Low_Ti,250},{Low_La,250},{Low_So_N,250},
{Low_La,250},{Low_Ti,250},{Low_La,250},{Low_Ti,250},{Low_La,250},{Low_So_N,250},{Low_La,250},{Low_Ti,250},{0,250},
{Mid_Do_N,250},{Mid_Re,250},{Mid_Mi,250},{Mid_La,250},{0,20},{Mid_La,250},{Mid_Ti,250},{Mid_So_N,250},{Mid_Fa_N,250},{0,20},{Mid_Fa_N,250},
{Mid_Mi,250},{Mid_Fa_N,250},{Mid_Mi,250},{0,20},{Mid_Mi,250},{Mid_Do_N,250},{Low_La,250},{0,250},
{Low_La,250},{Low_Ti,250},{Mid_Do_N,250},{0,20},{Mid_Do_N,250},{0,20},{Mid_Do_N,250},{Mid_Re,250},{Mid_Mi,250},{0,20},{Mid_Mi,250},{Mid_Re,250},
{Mid_Do_N,250},{0,20},{Mid_Do_N,250},{Low_Ti,500},{0,750},
{Mid_Do_N,250},{Mid_Re,250},{Mid_Mi,250},{Mid_La,250},{0,20},{Mid_La,250},{Mid_Ti,250},{Mid_So_N,250},{Mid_Fa_N,250},{Mid_Fa_N,250},{Mid_Mi,250},
{Mid_Fa_N,250},{Mid_Mi,250},{0,20},{Mid_Mi,250},{Mid_Do_N,250},{Low_La,250},{0,20},{Low_La,250},{Mid_Do_N,250},{Low_Ti,250},{Low_La,500},
{Low_Ti,250},{Mid_Do_N,250},{Low_Ti,500},{Low_La,500},{0,20},{Mid_La,3000},
};