-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtone_generator.cpp
67 lines (59 loc) · 2.01 KB
/
tone_generator.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
#include "Arduino.h"
#include "tone_generator.h"
#include "definitions.h"
/*
* SQUARE WAVE SIGNAL GENERATION
*
* baud_adj lets you to adjust or fine tune overall baud rate
* by simultaneously adjust the 1200 Hz and 2400 Hz tone,
* so that both tone would scales synchronously.
* adj_1200 determined the 1200 hz tone adjustment.
* tc1200 is the half of the 1200 Hz signal periods.
*
* ------------------------- -------
* | | |
* | | |
* | | |
* ---- -------------------------
*
* |<------ tc1200 --------->|<------ tc1200 --------->|
*
* adj_2400 determined the 2400 hz tone adjustment.
* tc2400 is the half of the 2400 Hz signal periods.
*
* ------------ ------------ -------
* | | | | |
* | | | | |
* | | | | |
* ---- ------------ ------------
*
* |<--tc2400-->|<--tc2400-->|<--tc2400-->|<--tc2400-->|
*
*/
const float baud_adj = 0.975;
const float adj_1200 = 1.0 * baud_adj;
const float adj_2400 = 1.0 * baud_adj;
unsigned int tc1200 = (unsigned int)(0.5 * adj_1200 * 1000000.0 / 1200.0);
unsigned int tc2400 = (unsigned int)(0.5 * adj_2400 * 1000000.0 / 2400.0);
void set_toneValue_1200() {
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(tc1200);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(tc1200);
}
void set_toneValue_2400() {
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(tc2400);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(tc2400);
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(tc2400);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(tc2400);
}
void set_toneValue(bool toneValue) {
if (toneValue)
set_toneValue_1200();
else
set_toneValue_2400();
}