Академический Документы
Профессиональный Документы
Культура Документы
page 1 of 22
Purpose
The purpose of this reference design is to present a method for using a PIC32 microcontroller to
generate DTMF tones using digital filter technology. The technique that will be demonstrated in this
reference design uses the power of digital signal processing that requires only 64 bytes of memory and a
minimal amount of processor computation. The performance of this approach clearly demonstrates that
generating multiple tone signals requires a minimum of microprocessor resources.
Equipment List
1. chipKITTM Pro MX7 processor board with USB cable
2. Pmod-DA2 Dual Channel D to A converter
3. PmodKYPD 16 Button Keypad
4.
Software Resources
1. MPLAB X Integrated Development Environment (IDE)\
2. MPLAB XC32 C/C++ Compiler
3. XC32 C/C++ Compiler Users Guide
References
1. chipKITTM Pro MX7 Board Reference Manual
2. C Programming Reference
page 2 of 22
6.
7. Steven W. Smith, The Scientist and Engineers Guide to Digital Signal Processing
DTMF
Touch tone dialing was introduced by AT&T for telephones in 1963 as a replacement for rotary dialsi.
The primary advantage of touch tone dialing over rotary dialing is that the dialing information can be
carried in the audio band where as the dialing information for rotary dialing is DC. The rotary phones
(circa 1891-present day) created pulses in the voltage supplied by the telephone exchange to power the
telephone. Rotary dials are mechanical devices that use a cam to actuate a mechanical switch. Over time
the mechanics of the rotary dial would break or begin to operate so slowly that the pulses could no
longer be accurately decoded. Conventional touch tone operated telephones have 12 keys: 0 through 9
as well as the * and # keys. The full implementation of the touch tone key pad as shown in Figure 1
contains 16 keys. The additional four keys are designated A through D. Some phone systems use the
additional keys for phone system management and control.
page 3 of 22
in a tone band width of 25Hz based on the row one frequency to 59 Hz for the column four. This has
ramifications when designing a digital filter to identify the tone pairs when a key is pressed.
770 Hz
852 Hz
941 Hz
DTMF signals in the form of wave files can be generated using a web-based audio generator. However,
this technique for generating DTMF tones requires 16 memory arrays each containing a minimum of 12
integers or a total of 384 bytes of memory. Figure 2 shows the series of outputs that are required to
generate the minimum and maximum frequency DTMF tones as well as the sum that would be needed
to generate the tones for the D key. The series of 12 outputs needs to be repeated generated at an
8KHz rate. Observe that initial output of each series is zero. This is a result of synthesizing the series
using a sine function. When repeating this sequence of output, this initial output may not
mathematically follow the last output in the series. This discontinuity both spreads the spectrum of the
tone frequencies and well as generating harmonic as illustrated in Figure 3 and Figure 4. Depending on
the amount of frequency spreading generated, the resulting signal can exceed the DTMF specification
provided above.
8 KHz DTMF Tone Samples
Amplitude
2
1
0
1
2
10
Sample
ROW 1 Tone
COL 4 Tone
Sum of Tones
Figure 2. Sample plots for the lowest and highest DTMF tone frequencies
page 4 of 22
0.05
0.04
0.03
0.02
0.01
0
660
680
700
720
740
Frequency - Hz
DFT of ROW 1
0.05
0.04
0.03
0.02
0.01
0
3
1.610
1.6510
1.710
1.7510
1.810
Frequency - Hz
DFT of COL 4
page 5 of 22
PIC32 Microprocessor
Analog
Input
Analog Antialiasing
Filter
Sample and
Hold
Analog to
Digital
Converter
(ADC)
DSP
Algorithm
(Software)
Digital to
Analog
Converter
(DAC)
Analog
Output Filter
Analog
Output
One mathematical representation of a digital filter algorithm is expressed a ratio of the output to the
inputs that are expressed by two polynomials in z inverse as shown in Equations 1. The term z-1
represents a delay of one sample period while the ai and bi coefficients are uniquely determined based
on the requirements of the digital filter. Procedures for determining these polynomial coefficients will
be addressed below.
Equation 1.
Equating X(z)*z-i to x(n-i) and Y(z)*z-k to y(n-k), Equation 1 can be rewritten as shown in Equation 2 which
is the general form of a recursive difference equation. The expression x(n-i) represents the input sample
that occurred i samples earlier.
Equation 2.
Equation 2 is a recursive equation that requires present and past inputs as well as past outputs.
Recursive equations are referred to as an infinite impulse response (IIR) filters because the feedback of
past outputs gives the filter infinite memory.
If a filter is generated that results in all coefficient ak = 0 for k > 0, then the filter has no memory and is
referred to as a finite impulse response (FIR) filters. The outputs of FIR filters are a function of the
coefficients that multiple present and past inputs. Once an input exceeds the number of delays equal to
the order of the numerator polynomial in Equation 2, the input is forgotten hence inputs have finite
persistence.
page 6 of 22
denominator to equal zero hence the output of the filter will be infinite when excited at the frequency
corresponding to the angle from the real axis. Poles outside the units circle indicate that the filter is
unstable and the amplitude of the oscillations will increase toward infinity. Poles just inside the unit
circle will cause the filter to oscillate however the amplitude is bounded (fixed maximum). We will take
advantage of this characteristic to make the eight oscillators needed for the DTMF tones.
ej1
=p,
z=1 0
20
=0,
z=1
40
60
0.5
1
1
Amplitude
Imaginary
0.5
0.5
0.5
Real
80
0
Frequency - Hz
Note that poles and zeros that do not lie on the real axis exist in complex conjugate pairs. The effect of
zeros on the filter response can be clearly noted on Figure 7. The z-plane frequency, , relates to the
real-world signal frequency by noting that = p for f = the Nyquist rate which is half of the sampling
rate in samples per second.
page 7 of 22
poles presented in Table III that lie on the unit circle are generated using Equation 3. The filter gain for
each oscillator, also listed in Table II, are scale factors needed to make the peak amplitudes equal to 1.
Figure 8 shows the eight pairs of complex conjugate pole pairs are plotted on a z-plane graph.
Equation 3
Table II. Normalized oscillator frequencies
2 p Fr1
Fs
2 p Fr2
R2 =
Fs
2 p Fr3
R3 =
Fs
2 p Fr4
R4 =
Fs
R1 =
= 0.547
= 0.605
= 0.669
= 0.739
2 p Fc1
Fs
2 p Fc2
C2 =
Fs
2 p Fc3
C3 =
Fs
2 p Fc4
C4 =
Fs
C1 =
= 0.95
= 1.049
= 1.16
= 1.283
Table III. Complex conjugate pole pairs and transfer function gain for the row and column tones
Row frequency poles pairs
e
PR1 =
e
e
PR2 =
e
e
PR3 =
e
PR4 =
0.854 0.52i
=
1 R1
0.854 0.52i
1 R2
0.823 0.569i
=
1 R2
0.823 0.569i
1 R3
0.784 0.62i
=
1 R3
0.784 0.62i
1 R1
0.739 0.674i
=
1 R4
0.739 0.674i
GR1=0.520
e
PC1 =
e
GR2=0.569
PC2 =
GR3=0.620
1 R4
GR4=0.676
e
PC3 =
e
e
PC4 =
e
0.582 0.813i
=
1 C1
0.582 0.813i
1 C1
0.498 0.867i
=
1 C2
0.498 0.867i
1 C3
0.399 0.917i
=
1 C3
0.399 0.917i
1 C4
0.284 0.959i
=
1 C4
0.284 0.959i
GC14=0.813
1 C2
GC2=0.871
GC3=0.917
GC4=0.959
page 8 of 22
=jp
0.733
Imaginary
0.367
=p,
0
z=1
=0,
z=1
0.367
0.733
1.1
1.1
=jp
0.733
0.367
0.367
0.733
1.1
Real
Equation 4
Each of the eight digital oscillator filters have unique values for the coefficient a1 and Gi.
Equation 5
To generate the two tones associated with a particular key pressed, two oscillating filters are activated
and the output added together before being sent to the digital to analog converter.
page 9 of 22
16 Button Keypad
A Digilent Pmod-KYPD 16-key keypad is used to indicate which of the 16 tone pairs is to be generated. As
shown in Figure 9 the # and * keys are not represented on the keypad. The code for this reference
designs associates the conventional * with the keypad F key and the conventional # key with the
keypad E key. The Pmod-KYPD 16 provides four inputs and four outputs. The keys are scanned using a
walking zero pattern for the column outputs and reading the row inputs. Since the Pmod connectors on
the chipKIT Pro MX7 are not connected consecutively to the PIC32 IO port pins, individual IO must be
managed in a bit-bang fashion. CAUTION: Do not attempt to use the JD connector on the chipKIT Pro
MX7 for the keypad interface as two of the pins also connect to the RS232 FTDI IC on the processor
board. This reference design uses the chipKIT Pro MX7 JF port. The driver code for the keypad interface
is provided in files keypad.c and keypad.h. The function, keypad_init, must be call prior to calling
the function read_keypad which when called will return values in the row and column variable
associated with a pressed key. This function returns a NO_KEY or one of the key identifiers.
The read_keypad function also displays the key that is pressed on the LCD.
page 10 of 22
Digital Oscillator
The Table IV lists the functions in the dtmf_gen.c file and briefly describes their functionality.
Following Table IV, the code for these functions is included to illustrate the simplicity of operation. All
functions listed as static are private (exclusive) to outer functions in this file.
The Timer 1 ISR is set to generate an interrupt each 0.125ms which generates the 8KHz sample rate. The
call to generate a new analog output is disabled only when the filter coefficients change. When there is
no button pressed on the 16 button keypad, the row and column tone filter coefficients are set to zero.
The analog output is offset to one half of the the maximum output. If the output is to drive a speaker, a
DC blocking capacitor must be used.
Constant definitions are provided by the support header file dtmf_gen.h
Table IV. dtmf_gen.c functions
Listing
Number
1
2
3
4
5
6
Function name
void start_tones(int row, int col);
static void select_filters(int row, int col);
static void gen_tones(void);
static void DA2(WORD CH1, WORD CH2);
static void pwm_analog(int ana);
void __ISR(_TIMER_1_VECTOR, IPL2SOFT)
Timer1Handler(void)
Description
Selects the filter coefficients and starts oscillation
Selects filter coefficients
IIR filter that generates the analog outputs
Analog output using PmodDA2
Analog output using PWM (Timer 2)
Analog output timer (Timer 1)
Listing 1
/* Select oscillator constants according to row and column number */
static void select_filter(int row, int col)
{
switch(row)
{
case 1:
row_filter.a0 = filt_r1[0];
row_filter.a1 = filt_r1[1];
row_filter.a2 = filt_r1[2];
break;
case 2:
row_filter.a0 = filt_r2[0];
row_filter.a1 = filt_r2[1];
row_filter.a2 = filt_r2[2];
break;
case 3:
row_filter.a0 = filt_r3[0];
row_filter.a1 = filt_r3[1];
row_filter.a2 = filt_r3[2];
break;
case 4:
page 11 of 22
= filt_r4[0];
= filt_r4[1];
= filt_r4[2];
= 0;
= 0;
= 0;
= filt_c1[0];
= filt_c1[1];
= filt_c1[2];
= filt_c2[0];
= filt_c2[1];
= filt_c2[2];
= filt_c3[0];
= filt_c3[1];
= filt_c3[2];
= filt_c4[0];
= filt_c4[1];
= filt_c4[2];
= 0;
= 0;
= 0;
Listing 2
/* ------------------- Set Oscillator frequency ------------------------ */
void start_tones(int row, int col)
{
tones_active = FALSE;
/* Disable Timer ISR oscillator */
select_filter(row, col);
/* Set filter coefficients */
x[0] = row_filter.a1;
x[1] = 0;
x[2] = 0;
page 12 of 22
tones_active = TRUE;
Listing 3
/* --------------- Tone generator IIR Filter code ------------ */
static void gen_tones(void)
{
int ch1, ch2 = 2048;
/* Set Channel 2 for DC output */
/* Row oscillator IIR filter */
x[2] = x[1];
/* Shift row channel outputs and compute new output */
x[1] = x[0];
x[0] = (row_filter.a1 * x[1] + row_filter.a2 * x[2]) >> 8;
/* Column oscillator IIR filter */
y[2] = y[1];
/* Shift column channel outputs and compute new output */
y[1] = y[0];
y[0] = (col_filter.a1 * y[1] + col_filter.a2 * y[2]) >> 8;
/* Sum and add offset to signal */
ch1 = (y[0] + x[0]) + 2048;
if(ch1 <
ch1
if(ch1 >
ch1
0)
= 0;
4097)
= 4097;
Listing 4
/* -------------------------- Output to PmodDA2 ---------------------------- */
/* Bit bang SPI because it outputs to two analog channels simultaneously */
static void DA2(WORD CH1, WORD CH2)
{
int i;
PULSE_SCLK();
/* Reset DAC */
PULSE_SYNC();
for(i = 15; i >= 0; i--)
/* Shift out data bits MSB first */
{
CHAN_A = (CH1 >> i) & 0x01;
CHAN_B = (CH2 >> i) & 0x01;
PULSE_SCLK();
page 13 of 22
Listing 5
/* --------------------------- PWM update ------------------------------ */
static void pwm_analog(int ana)
{
SetDCOC3PWM(ana);
/* Set PWM ratio */
}
Listing 6
/* ------------------ Time 1 Interrupt Service Routine ----------------- */
void __ISR(_TIMER_1_VECTOR, IPL2SOFT) Timer1Handler(void)
{
if(tones_active == TRUE)
/* Call IIR filter only if turned on */
gen_tones()
INTClearFlag( INT_T1 );
/* Clear the Timer interrupt flag */
}
http://www.analog.com/library/analogDialogue/archives/43-09/EDCh%208%20filter.pdf
http://www.maximintegrated.com/en/app-notes/index.mvp/id/1795
page 14 of 22
st
Figure 11. DAC and RC (100k / 0.33uF) 1 order filtered PWM output
page 15 of 22
10
20
0
110
210
310
410
Frequency - Hz
Figure 12. Output 3rd order filter frequency response
3
2
1
0
Time - ms
Filtered DAC
Filtered PWM
Figure 13. High order analog filtered DAC and PWM outputs
Code Performance
The frequency accuracy signals generated by the IIR oscillator algorithm are shown in Table V. The
measurements were made using the frequency output of a Fluke 867B Graphical Multimeter. The results
show that the worse frequency case is less than 0.18% making the accuracy greater than 99%. The
measured error is 10 times less than the DTMF standard requires.
page 16 of 22
Row
1
2
3
4
Column
1
2
3
4
Standard
697
770
852
941
IIR Measured
697.48
770.37
853.50
941.78
% Error
0.07%
0.05%
0.18%
0.08%
1209
1336
1477
1633
1208.4
1335.5
1477.5
1630.6
-0.05%
-0.03%
0.03%
-0.14%
Table VI provides a performance measure for this implementation of the DTMF signal. Although the
PIC32 processor has software floating point capability, the signal generating IIR filter shown in Listing 3
uses integer mathematics for improved speed performance. All filter constants are scaled by 256. For
example, the row 1 filter constants are defined by the declaration:
int filt_r1[3] = {K_R1, 437, 256}; /* R1 */
K_R1 is the scale numerator coefficient listed in Table IV as GR1. The second element in the array is the
scaled a1 coefficient as described by Equation 5. The last element in the coefficient array is the scaled
value of 1 used for the a2 coefficient.
The actual IIR filter is implemented with the line of C code shown below. Since the scale factor is a
power of 2, the multiplication and division by 256 is implemented with right and left binary shift
operations both of which require only 150ns to implement.
x[0] = (row_filter.a1 * x[1] - row_filter.a2 * x[2]) >> 8;
The run-time execution performance reported in Table 6 shows that the bit-banged SPI output is by far
the slowest executing process. Figure 14 and Figure 15 show the timing waveforms for a DAC update
cycle. The trace labeled LEDA is the duration of time needed to execute the program code of Listing 3
for the two IIR filters.
Table VI. Task code execution timing
Task
Sample Rate
Interrupt latency
IIR Filters
SPI Analog Output
Execution Time
8KHz
1.03 s
1.03 s
9.94 s
page 17 of 22
Figure 15. Expanded view of the time required for decoding and LCD update.
page 18 of 22
Figure 16. DAC output for two-tone generation when sampled at 8KHz
page 19 of 22
Figure 17. Fourier series of a digitally generated two DTMF tones showing the sidebands create by
modulation due to sampling when using a DAC.
Final Remarks
The IIR oscillator provides very accurate frequency signal generation with a minimal amount of
computational burden. The time to compute an analog output is a faster than the bit-banged SPI DAC
interface by a factor of 10. This can be significantly reduced by using a different DAC device.
page 20 of 22
The tradeoff between using a PWM output and a DAC depends the degree of analog output filtering.
The PWM requires significantly more analog electronics to achieve that same degree of digital
modulation rejection as a DAC output. The DAC used in this design requires three IO pins whereas the
PWM requires only one.
A discontinued Digilent PmodAMP1 was used for this project. This module had the advantage of using a
DC blocking capacitor to remove the DAC and PWM DC bias. Future designs will consider using the
PmodAMP3 which combines a power amplifier with a stereo DAC using an I2S serial interface.
page 21 of 22
PC
PModCLP
2 X 16
Character
LCD
DEBUG USB
(5 V PWR)
PG.12
PG.13
PG.14
PG.15
PModKYPD
JF
RD9 JD1
RD0 JD2
RC4 JD3
RC10 JD4
GND JD5
3.3V JD6
BTN1
BTN2
BTN3
1 - SYNC
2 -DINA
3 - DINB
4 - SCLK
6 - Gnd
7 - Vcc
VOUTA -1
N/C - 3
VOUTA -2
N/C - 4
Gnd - 6
Vcc - 7
1-LEFT
2-N/C
3-RIGHT
4-N/C
5-GND
6-Vcc
PmodAMP1
LED1
LED2
LED3
LED4
Pmod-DAC2
MPLAB
IDE
LEFT
DAC Audio
GND
Output
RIGHT
PG.6
PG.7
PA.0
RD2 JD8
OC3
100K
33nF
PWM Audio
Output
GND JD11
i
ii
http://en.wikipedia.org/wiki/Rotary_dial
http://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling#.23.2C_.2A.2C_A.2C_B.2C_C.2C_and_D
page 22 of 22