您的位置:首页 > 编程语言 > C语言/C++

DTMF编码

2016-06-12 09:45 369 查看
DTMF编解码器在编码时将击键或数字信息转换成双音信号并发送,解码时在收到的DTMF信号中检测击键或数字信息的存在性。一个DTMF信号由两个频率的音频信号叠加构成。这两个音频信号的频率来自两组预分配的频率组:行频组或列频组。每一对这样的音频信号唯一表示一个数字或符号。电话机中通常有16个按键,其中有10个数字键0~9和6个功能键*、#、A、B、C、D。由于按照组合原理,一般应有8种不同的单音频信号。因此可采用的频率也有8种,故称之为多频,又因它采用从8种频率中任意抽出2种进行组合来进行编码,所以又称之为“8中取2”的编码技术。根据CCITT的建议,国际上采用的多种频率为697Hz、770Hz、852Hz、941Hz、1209Hz、1336Hz、1477Hz和1633Hz等8种。用这8种频率可形成16种不同的组合,从而代表16种不同的数字或功能键

#define HIGH 62

#define LOW 60

int IsSysTimeOut(struct timeval endTime, struct timeval delay)

{

struct timeval timenow;

gettimeofday(&timenow,NULL);

if ((endTime.tv_sec > timenow.tv_sec)||((endTime.tv_sec == timenow.tv_sec)&&(endTime.tv_usec > timenow.tv_usec))) {

if (endTime.tv_sec - timenow.tv_sec > delay.tv_sec) {

//printf("return A\n");

return 1;

}

return 0;

} else {

//printf("return B\n");

return 1;

}

}

void kone_freq(int high, int low)

{

int cnt = 0;

struct timeval timer;

struct timeval timeout;

int h = 0, l = 0;

struct timeval tv;

int f = 1, f1=1;

struct timeval endtime;

struct timeval endtime_high;

struct timeval delay;

struct timeval delay_high;

delay.tv_sec = 0;

delay.tv_usec =low/2;

delay_high.tv_sec = 0;

delay_high.tv_usec =high/2;

gettimeofday(&timer,NULL);

gettimeofday(&endtime,NULL);

gettimeofday(&endtime_high,NULL);

endtime.tv_sec += delay.tv_sec;

endtime.tv_usec += delay.tv_usec;

endtime_high.tv_sec += delay_high.tv_sec;

endtime_high.tv_usec += delay_high.tv_usec;

timeout.tv_sec = 10000;

timeout.tv_usec = 50000;

timer.tv_sec += timeout.tv_sec;

timer.tv_usec += timeout.tv_usec;

APP_TRACE("last %d us!!!!", timeout.tv_usec);

while (!IsSysTimeOut(timer, timeout)) {

if (IsSysTimeOut(endtime, delay)) {

if (f) {

set_gpio(LOW, 1);

f = 0;

gettimeofday(&endtime,NULL);

endtime.tv_sec += delay.tv_sec;

endtime.tv_usec += delay.tv_usec;

} else {

set_gpio(LOW, 0);

f = 1;

gettimeofday(&endtime,NULL);

endtime.tv_sec += delay.tv_sec;

endtime.tv_usec += delay.tv_usec;

}

}

if (IsSysTimeOut(endtime_high, delay_high)) {

if (f1) {

set_gpio(HIGH, 1);

f1 = 0;

gettimeofday(&endtime_high,NULL);

endtime_high.tv_sec += delay_high.tv_sec;

endtime_high.tv_usec += delay_high.tv_usec;

} else {

set_gpio(HIGH, 0);

f1 = 1;

gettimeofday(&endtime,NULL);

endtime_high.tv_sec += delay_high.tv_sec;

endtime_high.tv_usec += delay_high.tv_usec;

}

}

}

}

void kone_dtmf(char c)

{

switch( c) {

case '*':

{

kone_freq(802, 1060);

break;

}

case '0':

{

kone_freq(724, 1060);

break;

}

case '1':

{

kone_freq(802, 1420);

break;

}

case '2':

{

kone_freq(724, 1420);

break;

}

case '3':

{

kone_freq(642, 1420);

break;

}

case '4':

{

kone_freq(802, 1280);

break;

}

case '5':

{

kone_freq(724, 1280);

break;

}

case '6':

{

kone_freq(642, 1280);

break;

}

case '7':

{

kone_freq(802, 1160);

break;

}

case '8':

{

kone_freq(724, 1160);

break;

}

case '9':

{

kone_freq(642, 1160);

break;

}

case '#':

{

kone_freq(642, 1060);

break;

}

case 'A':

{

kone_freq(621, 1420);

break;

}

case 'B':

{

kone_freq(621, 1280);

break;

}

case 'C':

{

kone_freq(621, 1160);

break;

}

case 'D':

{

kone_freq(621, 1060);

break;

}

default:

{

break;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 编码