forked from borishim/rotarydial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtmf.c
283 lines (256 loc) · 8.05 KB
/
dtmf.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
280
281
282
283
//*****************************************************************************
// Title : Pulse to tone (DTMF) converter
// Author : Boris Cherkasskiy
// http://boris0.blogspot.ca/2013/09/rotary-dial-for-digital-age.html
// Created : 2011-10-24
//
// Modified : Arnie Weber 2015-06-22
// https://bitbucket.org/310weber/rotary_dial/
// NOTE: This code is not compatible with Boris's original hardware
// due to changed pin-out (see Eagle files for details)
//
// Modified : Matthew Millman 2018-05-29
// http://tech.mattmillman.com/
// Cleaned up implementation, modified to work more like the
// Rotatone product.
//
// Modified : borishim 2019-04-18
// https://github.com/borishim/rotarydial
// Modified for use with 8 MHz crystal.
// Move sinus array from data memory to program memory.
//
// Modified : Jens B. 2020-01-03
// https://github.com/jnsbyr/rotarydial
// Support 4 and 8 MHz crystals.
// Make output high impedance when not used.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
// DTMF generator logic is loosely based on the AVR314 app note from Atmel
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include "dtmf.h"
static void dtmf_enable_pwm(void);
//************************** SIN TABLE *************************************
// Samples table : one period sampled on 128 samples and
// quantized on 7 bit
//**************************************************************************
const uint8_t auc_sin_param[NUM_SAMPLES] PROGMEM = {
64, 67, 70, 73,
76, 79, 82, 85,
88, 91, 94, 96,
99, 102, 104, 106,
109, 111, 113, 115,
117, 118, 120, 121,
123, 124, 125, 126,
126, 127, 127, 127,
127, 127, 127, 127,
126, 126, 125, 124,
123, 121, 120, 118,
117, 115, 113, 111,
109, 106, 104, 102,
99, 96, 94, 91,
88, 85, 82, 79,
76, 73, 70, 67,
64, 60, 57, 54,
51, 48, 45, 42,
39, 36, 33, 31,
28, 25, 23, 21,
18, 16, 14, 12,
10, 9, 7, 6,
4, 3, 2, 1,
1, 0, 0, 0,
0, 0, 0, 0,
1, 1, 2, 3,
4, 6, 7, 9,
10, 12, 14, 16,
18, 21, 23, 25,
28, 31, 33, 36,
39, 42, 45, 48,
51, 54, 57, 60
};
//*************************** x_SW ***************************************
// Fck = Xtal/prescaler
// Table of x_SW (excess 8): x_SW = ROUND(8 * N_samples * f * 510 / Fck)
//**************************************************************************
// high frequency
// 1209hz ---> x_SW = 79
// 1336hz ---> x_SW = 87
// 1477hz ---> x_SW = 96
// 1633hz ---> x_SW = 107
//
// low frequency
// 697hz ---> x_SW = 46
// 770hz ---> x_SW = 50
// 852hz ---> x_SW = 56
// 941hz ---> x_SW = 61
//
// | 1209 | 1336 | 1477 | 1633
// 697 | 1 | 2 | 3 | A
// 770 | 4 | 5 | 6 | B
// 852 | 7 | 8 | 9 | C
// 941 | * | 0 | # | D
#if (CLOCK_SOURCE == 0 || F_CPU == 8000000L)
// 8 MHz clock
const uint8_t auc_frequency[12][2] =
{
{ 44, 31 }, // 0
{ 39, 23 }, // 1
{ 44, 23 }, // 2
{ 48, 23 }, // 3
{ 39, 25 }, // 4
{ 44, 25 }, // 5
{ 48, 25 }, // 6
{ 39, 28 }, // 7
{ 44, 28 }, // 8
{ 48, 28 }, // 9
{ 39, 31 }, // *
{ 48, 31 }, // #
};
#elif (F_CPU == 4000000L)
// 4 MHz clock
const uint8_t auc_frequency[12][2] =
{
{ 87, 61 }, // 0
{ 79, 46 }, // 1
{ 87, 46 }, // 2
{ 96, 46 }, // 3
{ 79, 50 }, // 4
{ 87, 50 }, // 5
{ 96, 50 }, // 6
{ 79, 56 }, // 7
{ 87, 56 }, // 8
{ 96, 56 }, // 9
{ 79, 61 }, // *
{ 96, 61 }, // #
};
#endif
volatile uint32_t _g_delay_counter; // Delay counter for sleep function
volatile uint8_t _g_stepwidth_a; // step width of high frequency
volatile uint8_t _g_stepwidth_b; // step width of low frequency
volatile uint16_t _g_cur_sin_val_a; // position freq. A in LUT (extended format)
volatile uint16_t _g_cur_sin_val_b; // position freq. B in LUT (extended format)
void dtmf_init(void)
{
TIMSK = _BV(TOIE0); // TCNT0 overflow interrupt enabled
TCCR0A = _BV(WGM00) | _BV(WGM01); // Fast 8 Bit PWM
TCCR0B = _BV(CS00); // System clock without prescaling
OCR0A = 0; // Use max. clock frequency
TCNT0 = 0; // Reset TCNT0
_g_stepwidth_a = 0x00;
_g_stepwidth_b = 0x00;
_g_cur_sin_val_a = 0;
_g_cur_sin_val_b = 0;
_g_delay_counter = 0;
}
// Generate DTMF tone, duration x ms
void dtmf_generate_tone(int8_t digit, uint16_t duration_ms)
{
if (digit >= 0 && digit <= DIGIT_POUND)
{
// Standard digits 0-9, *, #
_g_stepwidth_a = auc_frequency[digit][0];
_g_stepwidth_b = auc_frequency[digit][1];
dtmf_enable_pwm();
// Wait x ms
sleep_ms(duration_ms);
}
else if (digit == DIGIT_BEEP)
{
// Beep ~1000Hz (66)
_g_stepwidth_a = 66;
_g_stepwidth_b = 0;
dtmf_enable_pwm();
// Wait x ms
sleep_ms(duration_ms);
}
else if (digit == DIGIT_BEEP_LOW)
{
// Beep ~500Hz (33)
_g_stepwidth_a = 33;
_g_stepwidth_b = 0;
dtmf_enable_pwm();
// Wait x ms
sleep_ms(duration_ms);
}
else if (digit == DIGIT_TUNE_ASC)
{
_g_stepwidth_a = 34; // C=523.25Hz
_g_stepwidth_b = 0;
dtmf_enable_pwm();
sleep_ms(duration_ms / 3);
_g_stepwidth_a = 43; // E=659.26Hz
sleep_ms(duration_ms / 3);
_g_stepwidth_a = 51; // G=784Hz
sleep_ms(duration_ms / 3);
}
else if (digit == DIGIT_TUNE_DESC)
{
_g_stepwidth_a = 51; // G=784Hz
_g_stepwidth_b = 0;
dtmf_enable_pwm();
sleep_ms(duration_ms / 3);
_g_stepwidth_a = 43; // E=659.26Hz
sleep_ms(duration_ms / 3);
_g_stepwidth_a = 34; // C=523.25Hz
sleep_ms(duration_ms / 3);
}
// Stop DTMF transmitting
TCCR0A &= ~_BV(COM0A1); // Disable PWM output via OC0A/PB0
TCCR0A &= ~_BV(COM0A0);
PORTB &= ~_BV(PIN_PWM_OUT); // Set PB0 to low/high impedance
DDRB &= ~_BV(PIN_PWM_OUT); // Make PB0 an high impedance input
_g_stepwidth_a = 0;
_g_stepwidth_b = 0;
}
// Enable PWM output by configuring compare match mode - non inverting PWM
static void dtmf_enable_pwm(void)
{
DDRB |= _BV(PIN_PWM_OUT); // Make PB0 an output
TCCR0A |= _BV(COM0A1); // Enable PWM output via OC0A/PB0
TCCR0A &= ~_BV(COM0A0);
}
// Timer overflow interrupt service routine
ISR(TIMER0_OVF_vect)
{
uint8_t sin_a;
uint8_t sin_b;
// A component (high frequency) is always used
// move Pointer about step width ahead
_g_cur_sin_val_a += _g_stepwidth_a;
// normalize Temp-Pointer
uint16_t tmp_sin_val_a = (int8_t)(((_g_cur_sin_val_a + 4) >> 3) & (0x007F));
sin_a = pgm_read_byte(&(auc_sin_param[tmp_sin_val_a]));
// B component (low frequency) is optional
if (_g_stepwidth_b > 0)
{
// move Pointer about step width ahead
_g_cur_sin_val_b += _g_stepwidth_b;
// normalize Temp-Pointer
uint16_t tmp_sin_val_b = (int8_t)(((_g_cur_sin_val_b + 4) >> 3) & (0x007F));
sin_b = pgm_read_byte(&(auc_sin_param[tmp_sin_val_b]));
}
else
{
sin_b = 0;
}
// calculate PWM value: high frequency value + 3/4 low frequency value
OCR0A = (sin_a + (sin_b - (sin_b >> 2)));
_g_delay_counter++;
}
// Wait x ms
void sleep_ms(uint16_t msec)
{
_g_delay_counter = 0;
set_sleep_mode(SLEEP_MODE_IDLE);
while (_g_delay_counter <= msec * T0_OVERFLOW_PER_MS)
{
sleep_mode();
}
}