?? dtmf.c
字號:
/*
;***********************************************************************
;* 北京達盛科技有限公司
;* 研 發 部
;*
;* http://www.techshine.com
;*
;*--------------------------------------------- 文件信息 ---------------
;*
*文件名稱:dtmf.c
*文件標示:
*摘 要:本文件內容為用tms320lf2407 dsp做雙音多頻實驗
*************************************************************/
/************************文件預處理***************************/
#include "exp4_2407.h"
#include "math.h"
#define pi 3.1415927
#define Length 102
#define Len 8
/*************************************************************/
/********************全局變量定義與初始化*********************/
double x[Length],xc[Length],xr[Length];
double vk[Len][Length],vs1[Len],vs2[Len],v[3],s[Len],z[Len];
double vkn[Len][Length];
double fr0,fr1,fr2,fr3,fc0,fc1,fc2,fc3,fs,fr,fc;
double m1,m0,max1,max2;
int i,j;
unsigned int da_out;
/*************************************************************/
/*******************函數、子程序聲明與定義********************/
void sys_ini() //系統初始化子程序
{
/*關總中斷*/
asm(" setc INTM");
/*抑制符號位擴展*/
asm(" clrc SXM");
/*累加器中結果正常溢出*/
asm(" clrc OVM");
/*禁止看門狗*/
* WDCR=0x00E8;
/*CLKIN=10M,CLKOUT=40M,使能spi外圍模塊*/
* SCSR1=0x0021;
/*使能spi引腳*/
* MCRB=0xFE3F;
/*io、ram、program都設為0等待讀寫*/
WSGR=0x0649;
/*清除所有中斷標志,"寫1清0"*/
* IFR=0xFFFF;
}
void spi_ini() //spi初始化子程序
{
/*復位spi*/
* SPICCR&=0x007F;
/*CLOCK POLARITY=0,16位數據格式*/
* SPICCR=0x000F;
/*禁止溢出中斷,CLOCK PHASE=1,禁用spi中斷,主模式,使能數據發送*/
* SPICTL=0x000E;
/*清除接收溢出中斷標志*/
* SPISTS=0x0080;
/*波特率為40/4=10M*/
* SPIBRR=0x0000;
/*啟動spi工作*/
* SPICCR|=0x0080;
}
void delay() //延時子程序
{
unsigned int k;
for(k=0;k<50;k++);
}
/*************************************************************/
/*****************中斷服務子程序聲明與定義********************/
interrupt void nothing() //啞中斷子程序
{
return;
}
/*************************************************************/
/**************************主程序*****************************/
void main()
{
int i,p,data;
int inkey,outkeyrow,outkeycol;
char outkey;
int *out = (int*)0x2b00;
int in[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int ks;
fs = 8000;
fr0 = 697.0; fc0 = 1209.0;
fr1 = 770.0; fc1 = 1336.0;
fr2 = 852.0; fc2 = 1477.0;
fr3 = 941.0; fc3 = 1633.0;
sys_ini();
spi_ini();
s[0] = fr0/fs; s[4] = fc0/fs;
s[1] = fr1/fs; s[5] = fc1/fs;
s[2] = fr2/fs; s[6] = fc2/fs;
s[3] = fr3/fs; s[7] = fc3/fs;
sys_ini();
spi_ini();
for (;;)
{
for (ks=10; ks<16; ks++)
{
inkey = in[ks];
if (inkey==0) {fr=fr3; fc=fc1;}
else if (inkey==1) {fr=fr0; fc=fc0;}
else if (inkey==2) {fr=fr0; fc=fc1;}
else if (inkey==3) {fr=fr0; fc=fc2;}
else if (inkey==4) {fr=fr1; fc=fc0;}
else if (inkey==5) {fr=fr1; fc=fc1;}
else if (inkey==6) {fr=fr1; fc=fc2;}
else if (inkey==7) {fr=fr2; fc=fc0;}
else if (inkey==8) {fr=fr2; fc=fc1;}
else if (inkey==9) {fr=fr2; fc=fc2;}
else if (inkey==10) {fr=fr0; fc=fc3;}
else if (inkey==11) {fr=fr1; fc=fc3;}
else if (inkey==12) {fr=fr2; fc=fc3;}
else if (inkey==13) {fr=fr3; fc=fc3;}
else if (inkey==14) {fr=fr3; fc=fc0;}
else if (inkey==15) {fr=fr3; fc=fc2;}
for (i=0; i<Length; i++)
{
xr[i] = sin(2*pi*fr*i/fs);
xc[i] = sin(2*pi*fc*i/fs);
x[i] = 1*(1.0*xc[i]+1.0*xr[i]);
}
for (j=0; j<8; j++)
{
for (i=0; i<Length; i++)
{
da_out=(x[i]+1)*127.5;
/*將數據打包成“從移位寄存器到DAC B數據寄存器
且用數據寄存器同時更新A和B兩個DA的值*/
* SPITXBUF=da_out&0x00ff|0x2500;
delay();
}
}
for(i=0; i<Len; i++)
{
v[2]=v[1]=v[0]=0;
for (j=0; j<Length; j++)
{
v[2] = 2*(cos(2*pi*s[i]))*v[1]-v[0]+x[j];
vk[i][j] = v[2];
v[0] = v[1];
v[1] = v[2];
}
}
for (i=0; i<Len; i++)
{
vs1[i] = vk[i][Length-1];
vs2[i] = vk[i][Length-2];
}
for (i=0; i<Len; i++)
{
m1 = vk[i][Length-1] * vk[i][Length-1];
m0 = vk[i][Length-2] * vk[i][Length-2];
z[i] = m1+m0-2*cos(2*pi*s[i])*vk[i][Length-1]*vk[i][Length-2];
}
max1=0.0;
for (i=0; i<(Len/2); i++)
{
if (z[i] >= max1) { max1 = z[i]; outkeyrow = i+1;}
}
max2=0.0;
for (i=4; i<Len; i++)
{
if (z[i] >= max2) { max2 = z[i]; outkeycol = i-4+1;}
}
/* Show the detceedt Key Number */
if (outkeyrow==1)
{
if (outkeycol==1) outkey = '1';
else if (outkeycol==2) outkey = '2';
else if (outkeycol==3) outkey = '3';
else if (outkeycol==4) outkey = 'A';
}
else if (outkeyrow==2)
{
if (outkeycol==1) outkey = '4';
else if (outkeycol==2) outkey = '5';
else if (outkeycol==3) outkey = '6';
else if (outkeycol==4) outkey = 'B';
}
else if (outkeyrow==3)
{
if (outkeycol==1) outkey = '7';
else if (outkeycol==2) outkey = '8';
else if (outkeycol==3) outkey = '9';
else if (outkeycol==4) outkey = 'C';
}
else if (outkeyrow==4)
{
if (outkeycol==1) outkey = '*';
else if (outkeycol==2) outkey = '0';
else if (outkeycol==3) outkey = '#';
else if (outkeycol==4) outkey = 'D';
}
i=0;
i=0;
}
}
}
/***************************結束******************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -