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;
}
}
}
#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 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- ASP编码必备的8条原则
- XML指南——XML编码
- C#中字符串编码处理
- ExtJS中文乱码之GBK格式编码解决方案及代码
- 程序员趣味读物 谈谈Unicode编码
- 文本文件编码方式区别
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法