-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.cpp
123 lines (90 loc) · 4 KB
/
sound.cpp
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
//----------------------------------------------------------------------------
//Ìîäóëü ãåíåðàöèè çâóêîâûõ ñèãíàëîâ
//----------------------- Èñïîëüçóåìûå ðåñóðñû: ------------------------------
//Çâóêîâîé èçëó÷àòåëü (áåç âñòðîåííîãî ãåíåðàòîðà) ÷åðåç òðàíçèñòîðíûé êëþ÷
//ïîäêëþ÷åí ê âûâîäó PB13 (âûõîä òàéìåðà TIM1_CH1N). Àêòèâíûé óðîâåíü - ÂÛÑÎÊÈÉ.
//----------------------------------------------------------------------------
#include "main.h"
#include "sound.h"
//------------------------------- Êîíñòàíòû: ---------------------------------
#define TIM1_PRESCALER 10 //ïðåñêàëåð òàéìåðà
#define TIM1_PERIOD 2400 //íà÷àëüíûé ïåðèîä òàéìåðà
//--------------------------- Ñòàíäàðòíûå çâóêè: -----------------------------
#define BEEP_F 1000 //÷àñòîòà ñòàíäàðòíîãî çâóêà, Ãö
#define BEEP_D 100 //äëèòåëüíîñòü ñòàíäàðòíîãî çâóêà, ìñ
#define TICK_F 1000 //÷àñòîòà êîðîòêîãî çâóêà, Ãö
#define TICK_D 40 //äëèòåëüíîñòü êîðîòêîãî çâóêà, ìñ
#define HIGH_F 1600 //÷àñòîòà âûñîêîãî çâóêà, Ãö
#define HIGH_D 100 //äëèòåëüíîñòü âûñîêîãî çâóêà, ìñ
#define CLICK_F 400 //÷àñòîòà êîðîòêîãî çâóêà îøèáêè, ÊÃö
#define CLICK_D 20 //äëèòåëüíîñòü êîðîòêîãî çâóêà îøèáêè, ìñ
#define BELL_F 360 //÷àñòîòà çâóêà îøèáêè, Ãö
#define BELL_D 300 //äëèòåëüíîñòü çâóêà îøèáêè, ìñ
//----------------------------------------------------------------------------
//----------------------------- Êëàññ TSound: --------------------------------
//----------------------------------------------------------------------------
//----------------------------- Êîíñòðóêòîð: ---------------------------------
TSound::TSound(void)
{
SoundMode = SND_ON;
SoundTimer = new TSoftTimer();
SoundTimer->Oneshot = 1;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //âêëþ÷åíèå òàêòèðîâàíèÿ TIM1
TIM1->PSC = TIM1_PRESCALER - 1; //çàãðóçêà ïðåñêàëåðà
TIM1->ARR = TIM1_PERIOD - 1; //ïåðèîä
TIM1->CCMR1 = TIM_CCMR1_OC1M_0 * 3; //toggle output
TIM1->CCR1 = 0; //ðåãèñòð ñðàâíåíèÿ
TIM1->BDTR |= TIM_BDTR_MOE; //main output enable
TIM1->CR1 = TIM_CR1_CEN; //ðàçðåøåíèå òàéìåðà
Pin_SND.Init(AF_PP_2M); //íàñòðîéêà ïèíà
}
//---------------------------- Ãåíåðàöèÿ çâóêà: ------------------------------
void TSound::Execute(void)
{
if(SoundTimer->Over())
TIM1->CCER &= ~TIM_CCER_CC1NE;
}
//--------------------------- Âûêëþ÷åíèå çâóêà: ------------------------------
void TSound::Off(void)
{
TIM1->CCER &= ~TIM_CCER_CC1NE;
}
//-------------------- Óñòàíîâêà ðåæèìà ãåíåðàöèè çâóêà: ---------------------
void TSound::SetMode(SndMode_t m)
{
SoundMode = m;
}
//---------------------- Íîðìàëüíàÿ ãåíåðàöèÿ çâóêà: -------------------------
//Ðàáîòàåò, åñëè óñòàíîâëåí ðåæèì SND_ON.
//f - ÷àñòîòà f [Ãö]
//d - äëèòåëüíîñòü d [ìñ]
void TSound::PlayNormal(uint16_t f, uint16_t d)
{
if(SoundMode == SND_ON)
PlayAlarm(f, d);
}
//---------------------- Àâàðèéíàÿ ãåíåðàöèÿ çâóêà: --------------------------
//Ðàáîòàåò, åñëè óñòàíîâëåí ðåæèì SND_ON èëè SND_ALARM.
//f - ÷àñòîòà f [Ãö]
//d - äëèòåëüíîñòü d [ìñ]
void TSound::PlayAlarm(uint16_t f, uint16_t d)
{
if(SoundMode != SND_OFF)
{
SoundTimer->Start(d);
TIM1->ARR = (SYSTEM_CORE_CLOCK / TIM1_PRESCALER / 2) / f - 1;
TIM1->CCER = TIM_CCER_CC1NE;
}
}
//--------------------------- Ñòàíäàðòíûå çâóêè: -----------------------------
void TSound::Beep(void) { PlayNormal(BEEP_F, BEEP_D); }
void TSound::Tick(void) { PlayNormal(TICK_F, TICK_D); }
void TSound::High(void) { PlayNormal(HIGH_F, HIGH_D); }
void TSound::Click(void) { PlayNormal(CLICK_F, CLICK_D); }
void TSound::Bell(void) { PlayNormal(BELL_F, BELL_D); }
void TSound::Alarm(void) { PlayAlarm(BEEP_F, BEEP_D); }
void TSound::Alert(void) { PlayAlarm(HIGH_F, HIGH_D); }
void TSound::ABell(void) { PlayAlarm(BELL_F, BELL_D); }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------