📄 dtmf_encode.cpp
字号:
/* dtmf_encode.c - DTMF Encoder Functions This file is part of DTMFkit Copyright (c) 2004 Joseph Battaglia <sephail@atnum.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */#include "stdafx.h"#include <stdio.h>#include <ctype.h>#include <math.h>//#include <unistd.h>#include "dtmf_encode.h"#define M_PI (3.14159265358979323846)
/* Buffer size */#define BUF_SIZE 10000/* The following #defines are for code readibility when using dtmf_encode_tones[][] */#define DIGIT 0#define FREQ1 1#define FREQ2 2/* DTMF digit frequency pairs */int dtmf_encode_tones[][3] = { /* digit, freq1, freq2 */ {'1', 697, 1209}, {'2', 697, 1336}, {'3', 697, 1477}, {'A', 697, 1633}, {'4', 770, 1209}, {'5', 770, 1336}, {'6', 770, 1477}, {'B', 770, 1633}, {'7', 852, 1209}, {'8', 852, 1336}, {'9', 852, 1477}, {'C', 852, 1633}, {'*', 941, 1209}, {'0', 941, 1336}, {'#', 941, 1477}, {'D', 941, 1633}, { -1, -1, -1}};/* Generate a DTMF tone or ',' for pause of length msec with specified sample rate, channels, and bits */ int dtmf_encode( int digit, int msec, int sample_rate, int bits, int channels , char* buffer_all)
{ unsigned char buffer[BUF_SIZE]; double add1, add2, c1=0, c2=0; short bufpos = 0, freq1, freq2, i, length,length1, val, val1, val2;
char buffer2[20];
// itoa( digit, buffer2, 10 );
digit += 0x30;//= buffer2[0]; /* Query lookup table for DTMF digit frequencies */ for(i = 0; dtmf_encode_tones[i][DIGIT] != -1; i++)
{ if((dtmf_encode_tones[i][DIGIT]) == toupper(digit))
{ freq1 = dtmf_encode_tones[i][FREQ1]; freq2 = dtmf_encode_tones[i][FREQ2]; } } /* Otherwise generate a pause */ if(digit == ',')
{ freq1 = 0; freq2 = 0; } /* If the tone could not be found, return */ if(digit != ',' && freq1 == 0)
{ return(ERR_INVALID_DIGIT); } /* Calculate values for waveform generation */ add1 = (double)freq1 / sample_rate; add2 = (double)freq2 / sample_rate; length = (msec * sample_rate) / 1000; length1 = (msec * sample_rate) / 1000;
// c1 = add1;
// c2=add2; while(--length >= 0)
{ /* Generate waveform */ val1 = (int)(sin(c1 * 2 * M_PI) * 16383); val2 = (int)(sin(c2 * 2 * M_PI) * 16383); val = val1 + val2; /* Loop for each channel */ for(i = 0; i < channels; i++)
{ if(bits == 8)
{ /* Convert to 8-bit unsigned */ buffer[bufpos++] = 128 + (val >> 8); } if(bits == 16)
{ /* Convert to 16-bit signed (LE) */ buffer[bufpos++] = val & 0xff; buffer[bufpos++] = (val >> 8) & 0xff; } } /* Add waveform generation values */ c1 += add1; c2 += add2; } memcpy(buffer_all, (const char*)buffer,length1); return(0); }/* calls dtmf_encode() for each character in a string */int dtmf_encode_string( char *string, int msec, int sample_rate, int bits, int channels, int verbose)
{ int i, retval; for(i = 0; string[i] != '\0'; i++)
{ /* Generate tone for each digit */ //retval = dtmf_encode(string[i], msec, sample_rate, bits, channels); //if(retval && retval != ERR_INVALID_DIGIT)
// return(retval); /* Add a pause between digits (to comply with standards) */ //retval = dtmf_encode(',', 50, sample_rate, bits, channels); //if(retval)
// return(retval); } return(0); }
DTMF FAQ - Telephone Tone Dialing chips V1.20
(filipg: More additions have been made from contributions)
Contents:
1) About the Author
2) Why I wrote this FAQ
3) Short introduction to DTMF
4) Decoding DTMF
5) List of DTMF-receiver chips
6) An alternative approach: DSP
7) If you live in USA read this section !
7.1) Can I build my own DAA?
8) Addresses
9) Conclusion/About the Author
10) DTMF in C (on a PC) by Kirk Hobart
--------------------------------------------------------------------------------
1) About the Author
Author : T.H. Tsim
Version: 1.06
Date : August 25th, 1994
E-mail : tsjoit@solist.htsa.hva.nl
Contributors: Terry Lalonde, Dbsnow, Andrew R. Ghali, Brad Yearwood, Gary Schaum, John Fields, Neil Harrison, Arkady Horak-Systems
--------------------------------------------------------------------------------
2) Why I wrote this FAQ
Hi! After reading sci.electronics for a couple of years, I've discovered that many people are interested in decoding DTMF-signals for various applications. A frequently asked question (FAQ) concerning this subject is: "What IC should I use in order to decode DTMF signals?"
--------------------------------------------------------------------------------
3) Short introduction to DTMF
DTMF means: Dual Tone Multi-Frequency, There is no baseband multiplexing done on DTMF signals. The signal generated by a DTMF encoder is a direct algebraic summation, in real time, of the amplitudes of two sine (cosine) waves of different frequencies. i.e. pressing '1' will send a tone made by adding 1209 Hz and 697 Hz to the other end of the line.
(From the Editor)
Here's a more detailed explanation of what DTMF is:
Q: What frequencies do touch tones use for which numbers?
A: The touch tone system uses pairs of tones to represent the various keys. There is a "low tone" and a "high tone" associated with each button (0 through 9, plus * (star) and # (octothorpe or pound symbol). The low tones vary according to what horizontal row the tone button is in, while the high tones correspond to the vertical column of the tone button.
The tones and assignments are as follows:
1 2 3 A : 697 Hz
4 5 6 B : 770 Hz
(low tones)
7 8 9 C : 852 Hz
* 0 # D : 941 Hz
---- ---- ---- ----
1209 1336 1477 1633 Hz
(high tones)
or:
1209 Hz 1336 Hz 1477 Hz 1633 Hz
ABC DEF
697 Hz 1 2 3 A
GHI JKL MNO
770 Hz 4 5 6 B
PRS TUV WXY
852 Hz 7 8 9 C
oper
941 Hz * 0 # D
or:
1 697+1209
2 697+1336
3 697+1477
4 770+1209
5 770+1336
6 770+1477
7 852+1209
8 852+1336
9 852+1477
0 941+1336
* 941+1209
# 941+1477
A 697+1633
B 770+1633
C 852+1633
D 941+1633
When the 4 button is pressed, the 770 Hz and 1209 Hz tones are sent together. The telephone central office will then decode the number from this pair of tones.
The tone frequencies were designed to avoid harmonics and other problems that could arise when two tones are sent and received. Accurate transmission from the phone and accurate decoding on the telephone company end are important. They may sound rather musical when dialed (and representations of many popular tunes are possible), but they are not intended to be so.
The tones should all be +/- 1.5% of nominal. The high frequency tone should be at least as loud, and preferably louder than the low frequency. It may be as much as 4 db louder. This factor is referred to as "twist." If a Touchtone signal has +3db of twist, then the high frequency is 3 db louder than the low frequency. Negative twist is when the low frequency is louder.
Q: What are the A, B, C and D touch tone keys used for?
Why are they not found on touch tone phone sets?
A: These are extensions to the standard touch-tones (0-9, *, #) which originated with the U.S. military's Autovon phone network. The original names of these keys were FO (Flash Override), F (Flash), I (Immediate), and P (Priority) which represented priority levels that could establish a phone connection with varying degrees of immediacy, killing other conversations on the network if necessary with FO being the greatest priority, down to P being of lesser priority. The tones are more commonly referred to as the A, B, C and D tones respectively, and all use a 1633 Hz as their high tone.
Nowadays, these keys/tones are mainly used in special applications such as amateur radio repeaters for their signalling/control. Modems and touch tone circuits tend to include the A, B, C and D tones as well. These tones have not been used for general public service, and it would take years before these tones could be used in such things as customer information lines; such services would have to be compatible with the existing 12-button touch tone sets in any case.
--------------------------------------------------------------------------------
4) Decoding DTMF
There are many ways to detect and decode these DTMF tones. One idea could be an eight sharp-tuned filter combination with detection circuits. Needless to say, this is very impractical, considering the various ICs (Integrated Circuits or 'chips') made by different manufacturers all over the world.
Most of these ICs do not require more than one (inexpensive) 3.58 MHz x-tal or resonator and the power circuitry. Usually the output is 4-bit binary + 1 strobe.
--------------
l l- d3
l DTMF l- d2
signal in -l Decoder l- d1 4-bit binary out
l chip l- d0
l l- strobe
--------------
Note: This figure is merely for decoration, it does not imply package information
Nice text to read about DTMF and telephony can be found in:
"Understanding Telephone Electronics"
Howard W.Sams & Co
ISBN: 0-672-27018-18
--------------------------------------------------------------------------------
5) List of DTMF-receiver chips
The companies are listed in a random order:
Silicon Systems Inc.
E-Mail for Info: info@ssi1.com
WWW: http://www.ssi1.com
SSI202 DIP-18 [*]
SSI203 DIP-18
SSI204 DIP-14
Features of the SSI 75T201/2/3 Integrated DTMF receiver:
CMOS circuits
Central office quality
NO front-end band-splitting filters required
Single, low tolerence, 12 volt supply
Detects either 12 or 16 Standard DTMF digits
Uses inexpensive 3.579545 MHz crystal for reference
excellant speech immunity
Output in either 4-bit hexadecimal code or binary coded 2-of-8
22 pin DIP package for high system density
Synchronous or handshake interface
Three state outputs
The SSI 75T202/203 features single low tolerance, 5VDC supply
[*]: For those in the USA, see the addresses section for this chip.
We have a pinout for the SSI-202 from WDUNCKEL1:
Data 1 =| |= Data 2
HEX/B =| S |= DATA 4
ENABL =| S |= DATA 8
IN1633 =| I |= CLRDATA VALID
VP =| |= DATA VALID
NC =| 2 |= ATB (CLOCK OUT)
GND =| 0 |= X IN
X ENBL =| 2 |= X OUT
ANALOGIN=| |= GND
A 10 Meg resistor accross the colorburst crystal is neccessary. Pins 5,2,3,8 are tied high and 4,15,9,7,10 are tied to ground. Data output on d1-d8 will be valid when data valid goes high.
All req. 3.58 MHz (3.579545 MHz) crystal
(From John Fields)
Here's a bombshell for you: It doesn't have to be a color burst crystal! As long as the encoder and the decoder are working on the same clock freq, and the freq isn't too high, (or maybe too low) the decoder will decode the tone pair sent by the encoder.
[back to the original FAQ]
A neat and well documented design + PCB layout by Tom Miller KA1JQW, can be found in "The ARRL handbook for radio amateurs", ISBN: 0-87259-169-7
Chrystal Semiconductor Corp.
CS20x family
OKI
MSM6843 DIP-28, 3.58, 4b1s
Application note in OKI Telecom LSI data book
Motorola
MC145436
MC68HC05F5
(From Arkady Horak-Systems)
I spent about the last 14 months of my life working on an integrated DTMF receiver here at Motolora and I would like to have you add the part to the DTMF-list. The receiver is part of a 6805 8-bit microcontroller and is called the MC68HC05F5. Here is a quick feature list:
MC68HC05 based core micro
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -