1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
#ifndef __BUZZER_H
#define __BUZZER_H
#include "common.h"
#include "beep.h"
typedef enum
{
MONO = 0, //單音
POLY_ON = 1, //開(kāi)機(jī)和弦
POLY_OFF = 2 //關(guān)機(jī)和弦
}Tone_Type; //蜂鳴器聲音類型
typedef struct
{
FREQ_Type Freq; //頻率
u8 OSCTime; //振蕩持續(xù)時(shí)間,最小單位為10ms
u8 PWRTime; //供電持續(xù)時(shí)間,最小單位為10ms
} TONE_Def; //音調(diào)結(jié)構(gòu)體
void BuzzerStart(Tone_Type ToneType);
void BuzzerCtrl(void);
#endif /* __BUZZER_H */ |
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 |
#include "buzzer.h"
const TONE_Def Tone1[] = {{FREQ_2K6, 100, 20},{FREQ_NO, 0, 0}};//單音
const TONE_Def Tone2[] = {{FREQ_2K3, 20, 20},{FREQ_2K6, 20, 20},{FREQ_2K9, 210, 10},{FREQ_NO, 0, 0}};//開(kāi)機(jī)和弦音
const TONE_Def Tone3[] = {{FREQ_2K9, 20, 20},{FREQ_2K6, 20, 20},{FREQ_2K3, 210, 10},{FREQ_NO, 0, 0}};//關(guān)機(jī)和弦音
TONE_Def * pTone;
static u8 BuzzerStatus = 0;
//蜂鳴器啟動(dòng),需要發(fā)聲時(shí)調(diào)用
void BuzzerStart(Tone_Type ToneType)
{
switch (ToneType)
{
case MONO:
pTone = Tone1;
break;
case POLY_ON:
pTone = Tone2;
break;
case POLY_OFF:
pTone = Tone3;
break;
default:
pTone = Tone1;
break;
}
BuzzerStatus = 0;
}
//蜂鳴器控制,每10ms執(zhí)行一次
void BuzzerCtrl(void)
{
static TONE_Def Tone;
switch (BuzzerStatus)
{
case 0:
Tone = *pTone;
if (Tone.Freq != FREQ_NO) //非結(jié)束符
{
//先判斷供電持續(xù)時(shí)間
if (Tone.PWRTime != 0)
{
Tone.PWRTime --;
BeepPwrOn();
}
else
{
BuzzerStatus = 2;
break;
}
//再判斷振蕩持續(xù)時(shí)間
if (Tone.OSCTime != 0)
{
Tone.OSCTime --;
BEEP_SetFreq(Tone.Freq);
BEEP_On();
}
else
{
BeepPwrOff();
BuzzerStatus = 2;
break;
}
//判斷完成,開(kāi)始遞減計(jì)時(shí)
BuzzerStatus = 1;
}
else /* Tone.Freq == FREQ_NO */ //是結(jié)束符
{
BuzzerStatus = 2;
}
break;
case 1:
if (Tone.PWRTime != 0)
{
Tone.PWRTime --;
}
else
{
BeepPwrOff();
}
if (Tone.OSCTime != 0)
{
Tone.OSCTime --;
}
else
{
BEEP_Off();
pTone ++; //取下一個(gè)音調(diào)
BuzzerStatus = 0;
}
break;
default:
break;
}
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
const TONE_Def Tone4[] = { //兩只老虎(兩只老虎兩只老虎 跑得快 跑得快)
{FREQ_2K, 25, 25},//1
{FREQ_2K3, 25, 25},//2
{FREQ_2K6, 25, 25},//3
{FREQ_2K, 25, 25},//1
{FREQ_2K, 25, 25},//1
{FREQ_2K3, 25, 25},//2
{FREQ_2K6, 25, 25},//3
{FREQ_2K, 25, 25},//1
{FREQ_2K3, 25, 25},//2
{FREQ_2K6, 25, 25},//3
{FREQ_2K9, 50, 25},//4 稍有停頓
{FREQ_2K3, 25, 25},//2
{FREQ_2K6, 25, 25},//3
{FREQ_2K9, 100, 25},//4 和弦效果
{FREQ_NO, 0, 0} //停止
}; |