# DTMF generation using C language.

Status
Not open for further replies.

#### djc

Hi,
I have been asked to generate DTMF signals. I am using Atmega 16 and code vision AVR. Language is C. Can anybody guide. Please its urgent. DTMF decoding is not necessary.

#### doraemon

##### Super Moderator
Staff member
Hello!

You have to give it back tomorrow at the university?
Basically you make a circular buffer and you read it at various
speeds by incrementing your pointers differently for each of the
frequencies. And you add them 2 by 2 depending on the combination.
Nothing hard in this.

Dora.

#### djc

Hi,

Thank you ud23 and doraemon. I apologize for word urgent. However i have to give it as soon as possible. I am not getting the point frequency addition. Do i have to add two frequencies and generate third one or generate interrupt with two back to back different frquencies.

#### ud23

DTMF means dual tone multiple frequencies. So you can do it with the single timer refer the second link it has sample C code.

#### djc

Thanx ud23. However it's difficult for me to understand somebody Else's logic. What i did is i loaded OCR1AH and OCR1AL with 4 hex values, first two for lower frequencies and another two for higher frequencies and expecting interrupt to occur at specified values in OCR1A and expecting phone with caller id to detect these frequencies. Here is my program. I think i am going terribly wrong.
Code:
#asm("sei")
while (1)
{
while(phn_10==0){
i=1;
//
OCR1AH=0x2c;
OCR1AL=0xd4;
OCR1AH=0x19;
OCR1AL=0xd8;
//                while(phn_11==1){

//                      master=1;
//                      delay_ms(1000);
//                      master=0;
//            }
}
i=0;
#asm("cli  ")
};

#### ud23

i don't get what are you trying to do with the code. You need to make a look up table for different key tones.

#### djc

yes that logic is totally wrong. can you tell me what is he value of Aa and Bb in equation 1 in PDF in second link to generate DTMF frequency.

- - - Updated - - -

yes that logic is totally wrong. can you tell me what is he value of Aa and Bb in equation 1 in PDF in second link to generate DTMF frequency. How to use 'k' factor in this equation...
f(t) = (Aa sin (2pi fat) + Bb sin (2pi fb t))

(Aa/Bb) = k 0.7<k<.9

#### ud23

Code:
[syntax=c]
char hexaKeys[ROWS][COLS] = {
{'D','#','0','*'},
{'C','9','8','7'},
{'B','6','5','4'},
{'A','3','2','1'}
};

//Order of dtmf are : 0123456789 * #
const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477,1209 ,1477  };
const int DTMF_freq2[] = {  941,  697,  697,  697,  770,  770,  770,  852,  852,  852, 941 ,941   };

// here you need to put the frequency generation values for your controller in above array definition for example  OCR1AH=0x2c; OCR1AL=0xd4;

void playDTMF(uint8_t number, long duration)
{
freq1.play(DTMF_freq1[number], duration);
freq2.play(DTMF_freq2[number], duration);
}

if (customKey){

switch (customKey) {
case '0':
playDTMF(0, 2ms);
break;
case '1':
playDTMF(1, 2ms);
break;
case '2':
playDTMF(2, 2ms);
break;
case '3':
playDTMF(3, 2ms);
break;
case '4':
playDTMF(4, 2ms);
break;
case '5':
playDTMF(5, 2ms);
break;
case '6':
playDTMF(6, 2ms);
break;
case '7':
playDTMF(7, 2ms);
break;
case '8':
playDTMF(8, 2ms);
break;
case '9':
playDTMF(9, 2ms);
break;
case '*':
playDTMF(10, 2ms);
break;
case '#':
playDTMF(11,2ms);
break;

default:   //just in case the character read has no value at all
delay(1);
}
[/syntax]

Last edited:

#### djc

there are 15 phone lines. there is no keypad on phones. so dtmf detection is not an issue. only DTMF generation. How does 'freq.play' function working in above program. I pardon if i bother you too much.

#### ud23

actually it is API of arduino to generate the square pulse for tone generation.Sorry i forget to mention you can replace it with the your PWM generation code,But don't use the function in the ISR.

Code:
freq1_play(int16 freq,int long duration)
{

//here your timer register allocation, and delay routine for ms

}

#### djc

Sorry to bother you again. However i am not getting how to generate look up table. I don't have any function in code vision AVR. for dtmf generation.

#### djc

Thanx UD3,

It's been so many days. But i am not succeeded yet. So i switched to Fast AVR. It has inbuilt DTMF generation function. But i will keep trying using C.

#### andre_teprom

##### Super Moderator
Staff member
I did not take a look on the code above, however I can´t see much issues in generating DTMF tones, if there is available 2 timers to store the fundamental frequency of each tone. Just reload the timer register accordingly.

Status
Not open for further replies.