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, //开机和弦
POLY_OFF = 2 //关机和弦
}Tone_Type; //蜂鸣器声音类型
typedef struct
{
FREQ_Type Freq; //频率
u8 OSCTime; //振荡持续时间,最小单位为10ms
u8 PWRTime; //供电持续时间,最小单位为10ms
} TONE_Def; //音调结构体
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}};//开机和弦音
const TONE_Def Tone3[] = {{FREQ_2K9, 20, 20},{FREQ_2K6, 20, 20},{FREQ_2K3, 210, 10},{FREQ_NO, 0, 0}};//关机和弦音
TONE_Def * pTone;
static u8 BuzzerStatus = 0;
//蜂鸣器启动,需要发声时调用
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执行一次
void BuzzerCtrl(void)
{
static TONE_Def Tone;
switch (BuzzerStatus)
{
case 0:
Tone = *pTone;
if (Tone.Freq != FREQ_NO) //非结束符
{
//先判断供电持续时间
if (Tone.PWRTime != 0)
{
Tone.PWRTime --;
BeepPwrOn();
}
else
{
BuzzerStatus = 2;
break;
}
//再判断振荡持续时间
if (Tone.OSCTime != 0)
{
Tone.OSCTime --;
BEEP_SetFreq(Tone.Freq);
BEEP_On();
}
else
{
BeepPwrOff();
BuzzerStatus = 2;
break;
}
//判断完成,开始递减计时
BuzzerStatus = 1;
}
else /* Tone.Freq == FREQ_NO */ //是结束符
{
BuzzerStatus = 2;
}
break;
case 1:
if (Tone.PWRTime != 0)
{
Tone.PWRTime --;
}
else
{
BeepPwrOff();
}
if (Tone.OSCTime != 0)
{
Tone.OSCTime --;
}
else
{
BEEP_Off();
pTone ++; //取下一个音调
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} //停止
}; |