daa.c

来自「我编写的软件运行在dsp5000的ccs下」· C语言 代码 · 共 755 行

C
755
字号
 /*********************************************************************************/
/* daa.c
/*
/* This example demonstrates the use of the telephony interface of the DSK.
/* The DAA and handset codecs are setup and then the application waits for an
/* incoming ringer.  On the second ring detected, the line is picked up (off-hook)
/* and a conversation can be carried out between the DAA and handset interfaces.
/* This example also shows how the CALLER-ID enable feature is used if caller-id
/* detection is to be performed.
/*
/*********************************************************************************/

#include <type.h>
#include <board.h>
#include <codec.h>
#include <mcbsp54.h>
#include <daa.h>
#include <math.h>


/*****************************************************************************/
/* Function Prototypes
/*****************************************************************************/

/* This delay routine does not conflict with DSP/BIOS.  It is used in this  */
/* example rather than brd_delay_msec which causes DSP/BIOS conflicts just  */
/* because of this.  If you are not using DSP/BIOS, you can change the code */
/* to use brd_delay_msec. 
                                                  */
#define SAMPLESIZE 400
#define pi 3.14159265358

void dtmf_gen(float dg_coff_r,float dg_coff_c,u16 *dg_out_num,int dg_num); 
char read_key(char chr);	//return the key from keyboard
void delay(s16);
void bringup(char dow);
void send();

/*****************************************************************************/
/* Global variables
/*****************************************************************************/
#define CLKMD *(unsigned int *)0x58
#define PMST *(unsigned int *)0x1d
#define SWWSR *(unsigned int *)0x28
#define SWCR *(unsigned int *)0x2b
HANDLE hDevice;
u16 out_sample[SAMPLESIZE];
s16 noise[SAMPLESIZE]={
-28778	,
-30270	,
12939	,
-5976   ,
21377	,
-23332	,
23707	,
26641	,
-4758	,
-11958	,
-25122	,
29293	,
7941	,
-22570	,
-7976	,
1827	,
31449	,
-13736	,
-2192	,
-32348	,
28941	,
-27180	,
-13838	,
-7478	,
20117	,
2319	,
25735	,
10921	,
-22810	,
-18530	,
-25818	,
-9494	,
8251	,
8595	,
31763	,
14753	,
6721	,
18303	,
18645	,
27815	,
10067	,
-11498	,
11693	,
1395	,
-12236	,
-21430	,
28557	,
5487	,
-26038	,
21205	,
18579	,
25299	,
-14264	,
-11170	,
11007	,
-10758	,
-12938	,
-18676	,
-23892	,
-18626	,
-5280	,
-20322	,
-29432	,
-10936	,
-3564	,
-32250	,
-8310	,
11253	,
-25902	,
-12422	,
-19282	,
9119	,
30027	,
19731	,
-10394	,
257	,
-8364	,
6117	,
-31462	,
-25206	,
-218	,
-27764	,
-25060	,
55	,
14349	,
-23464	,
-864	,
-4380	,
4513	,
-25134	,
10807	,
4147	,
2435	,
-16930	,
11197	,
-18996	,
18323	,
-29618	,
-25630	,
-6318	,
-21338	,
-2304	,
-13292	,
-14938	,
13251	,
-32144	,
9211	,
-8506	,
7637	,
26325	,
30577	,
-30172	,
-8222	,
-3662	,
-29956	,
-6786	,
19405	,
1017	,
32455	,
-30150	,
-5246	,
1173	,
22547	,
-27850	,
1723	,
8079	,
24883	,
2855	,
19237	,
10653	,
-26614	,
23407	,
-28882	,
67	,
-7432	,
-11406	,
-22740	,
-16800	,
17499	,
-8076	,
-15072	,
-25266	,
-15230	,
3565	,
23273	,
-9612	,
18983	,
26203	,
-2314	,
31083	,
6501	,
19625	,
5007	,
14973	,
12595	,
26107	,
20601	,
-25124	,
16219	,
-21496	,
3901	,
19233	,
25329	,
16375	,
-5674	,
-19088	,
30025	,
5553	,
-1034	,
21521	,
4471	,
4029	,
30381	,
18301	,
4669	,
-23662	,
30381	,
-15612	,
-16762	,
-24966	,
-27396	,
4831	,
23655	,
8931	,
10803	,
-26944	,
25223	,
30255	,
-17362	,
-30472	,
37	,
-18658	,
-29112	,
-5624	,
13191	,
5275	,
-13292	,
26809	,
9977	,
-25328	,
18067	,
-24910	,
3135	,
15947	,
15337	,
31125	,
11921	,
3527	,
8605	,
10387	,
29455	,
29071	,
-5812	,
26247	,
-66	,
16789	,
-18464	,
30861	,
16639	,
31109	,
-30902	,
-18156	,
1407	,
-27876	,
-21832	,
1927	,
-5956	,
8461	,
-21166	,
-2996	,
-15372	,
-15806	,
-31892	,
373	,
23971	,
1489	,
-31168	,
-12350	,
3951	,
22329	,
12843	,
-28596	,
-25938	,
-29882	,
8023	,
-28014	,
-7332	,
9587	,
11527	,
18193	,
-13944	,
-4696	,
22333	,
-29088	,
-9894	,
-4432	,
12031	,
-21304	,
2435	,
29945	,
27909	,
-17830	,
16433	,
9721	,
-26888	,
-6266	,
-10608	,
-14336	,
12073	,
28215	,
-11580	,
-11624	,
26307	,
-30876	,
4181	,
24405	,
-13684	,
15665	,
3887	,
-27864	,
-7660	,
29055	,
-24296	,
6223	,
14711	,
-3830	,
11689	,
-16248	,
-12920	,
-5376	,
-15162	,
-6128	,
21145	,
-23276	,
-18536	,
-20258	,
19877	,
-15332	,
-4102	,
-20858	,
30737	,
291	,
-16762	,
6807	,
26075	,
-23800	,
22261	,
10597	,
-6566	,
-27504	,
-19672	,
-26774	,
6011	,
28423	,
5275	,
1827	,
-23708	,
20805	,
-9128	,
18265	,
8391	,
-9378	,
-3716	,
6661	,
3457	,
9899	,
28189	,
-27738	,
23523	,
4707	,
-13614	,
-21738	,
2661	,
-17226	,
16545	,
4579	,
-16932	,
-32664	,
-26246	,
-4824	,
-10700	,
20199	,
10039	,
-31002	,
-2646	,
-16682	,
6401	,
-28712	,
29309	,
-22722	,
-6310	,
2511	,
-21718	,
-24816	,
3729	,
29319	,
-19158	,
14667	,
-12368	,
18177	,
24097	,
21897	,
12291	,
30559	,
-2152	,
23841	,
-4348	,
-7378	,
-6852	,
-15176	,
-21800	,
30291	,
25995	,
-21924	,
-5302	,
-436	,
-29422	,
-6114	,
19259	,
20509	,
-5080	,
29477	,
30877	,
-10598	,
-10464	,
24391	,
17141	,
8515	,
-14074	,
24357	,
28737	,
-29794	,
-10034	,
6337	,
-28544	,
-27380	,
-18150	,
9875	,
-30814	,
295	
};


/*****************************************************************************/
/* MAIN
/*****************************************************************************/

void main()
{
    s16 cnt;
    u16 data;
    u16 silent_count;
	u16 count=0;
	u16 i;
	
	if (brd_init(100))
		return;
	
	CLKMD=0x0000;
    while(CLKMD&0x01);
    CLKMD=0x0f003;
    PMST=0x6830;
    SWWSR=0x7000;
    SWCR=0x01;
    
    cnt = 2;

    /* blink the leds a couple times */
	while ( cnt-- )
	{
		brd_led_toggle(0);
		/* brd_delay_msec(1000); */
		delay(1000);
		brd_led_toggle(1);
		/* brd_delay_msec(1000); */
		delay(1000);
		brd_led_toggle(2);
		/* brd_delay_msec(1000); */
		delay(1000);
	}
	
    /* Set-up DAA codec */
    hDevice = codec_open(DAA_CODEC);
    codec_dac_mode(hDevice, CODEC_DAC_15BIT);          /* DAC in 15-bit mode */
    codec_adc_mode(hDevice, CODEC_ADC_15BIT);
    codec_ain_gain(hDevice, CODEC_AIN_6dB);            /* 6dB gain on analog input to ADC */
    codec_aout_gain(hDevice, CODEC_AOUT_MINUS_6dB);
    codec_sample_rate(hDevice,SR_8000); 

    /* Set-up HANDSET codec */
    hDevice = codec_open(HANDSET_CODEC);
    codec_dac_mode(hDevice, CODEC_DAC_15BIT);
    codec_adc_mode(hDevice, CODEC_ADC_15BIT);
    codec_ain_gain(hDevice, CODEC_AIN_6dB);            /* 6dB gain on analog input to ADC */
    codec_aout_gain(hDevice, CODEC_AOUT_MINUS_6dB);
    codec_sample_rate(hDevice,SR_8000);

    /* intializize DAA to default setting (off-hook, no caller ID) */
    daa_init();

    /* wait for incoming ringer 

start:
    brd_led_disable(0);
    brd_led_disable(1);
    brd_led_disable(2);


    /* wait for initial ring 
    silent_count = 0;
    while (!daa_ring_detect())
    {
        brd_delay_msec(5);
        silent_count++;

        /* toggle LED 10x a second 
        if (silent_count == 20)
        {
            brd_led_toggle(0);
            silent_count = 0;
        }
    }


    /* enable caller ID path 
    brd_led_disable(0);
    brd_led_enable(1);
    daa_cid(DAA_CID_ENABLE);

    /* wait for 1st ring to end 
    silent_count = 0;
    do
    {
        brd_delay_msec(5);
        if (!daa_ring_detect())
            silent_count++;
        else silent_count = 0;
    } while (silent_count < 600);


    /* disable caller ID path 
    daa_cid(DAA_CID_DISABLE);

    brd_led_disable(1);
    brd_led_enable(2);

    /* wait for 2nd ring 
    silent_count = 0;
    do
    {
        brd_delay_msec(5);
        if (!daa_ring_detect())
            silent_count++;
        else break;
    } while (silent_count < 1200);

    /* check if phone was hung up before 2nd ring and restart 
    if (silent_count >= 1200) goto start;

    brd_led_disable(2);

    /* go off-hook */
    daa_offhook();
	delay(1000);
	delay(1000);
	delay(1000);
    /* enable all LEDs */
    brd_led_enable(0);
    brd_led_enable(1);
    brd_led_enable(2);
    //dial out
    bringup('8');
    send();
    bringup('7');
    send();
    bringup('1');
    send();
    bringup('5');
    send();
    bringup('3');
    send();
       //以下是通话状态
    while (1)
    {
        /* Check for sample from DAA */
       for(i=0;i<SAMPLESIZE;i++)
       {
             if (MCBSP_RRDY(DAA_CODEC))
                {
                   /* Read DAA sample and write to handset codec */
                   data = *(volatile u16*)DRR1_ADDR(DAA_CODEC);
                   if(data<300)
                   *(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = noise[i]/500;
                   else
                   *(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = data;
                   //*(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = noise[i]/800;
                }
             //daa上有数据,则把数据放到data,再把data放到喇叭里。 
             /* Check for sample from handset */
             if (MCBSP_RRDY(HANDSET_CODEC))
                {
                    /* Read hadset codec sample and write to DAA */
                    data = *(volatile u16*)DRR1_ADDR(HANDSET_CODEC);
                    if(data<300)
                    *(volatile u16*)DXR1_ADDR(DAA_CODEC) = noise[i]/500;
                    else
                    *(volatile u16*)DXR1_ADDR(DAA_CODEC)=data;
                    //*(volatile u16*)DXR1_ADDR(DAA_CODEC) =noise[i]/800;
                    //if (!daa_ring_detect())
                    //break;
                }
        }
        //如果ad50接收串口准备好,把数据发送到麦克。
    }

    return;
}


void delay(s16 period)
{
    int i, j;
    
    for(i=0; i<period; i++)
    {
        for(j=0; j<period>>1; j++);
    }
}
void dtmf_gen(float dg_coff_r,float dg_coff_c,u16* dg_out_num,int dg_num)
{
	int dg_i;
	for(dg_i=0;dg_i<dg_num;dg_i++)
		dg_out_num[dg_i]=(u16)(16384*(2+sin(dg_coff_r*dg_i)+sin(dg_coff_c*dg_i)));
}//dtmf_gen                                                         
char read_key(char chr)	//return the key from keyboard
{
	return chr;
}//read_key
void bringup(char dow)
 { 
   char key;  	
 //while(1)	//scan the keyboard all the time , do nothing
		
			key=read_key(dow);	//read_key
			//delay(50);	//delay 50ms, eliminate dithering
			if(key==read_key(dow))
			{
				switch (key)			 	  
				{
   					case '0': {		
								dtmf_gen(2*pi*941/8000,2*pi*1336/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '1': {	  	
								dtmf_gen(2*pi*697/8000,2*pi*1209/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '2': {	  	
								dtmf_gen(2*pi*697/8000,2*pi*1336/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '3': {	  	
								dtmf_gen(2*pi*697/8000,2*pi*1477/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '4': {	  	
								dtmf_gen(2*pi*770/8000,2*pi*1209/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '5': {	  	
								dtmf_gen(2*pi*770/8000,2*pi*1336/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '6': {	  	
								dtmf_gen(2*pi*770/8000,2*pi*1477/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '7': {	  	
								dtmf_gen(2*pi*852/8000,2*pi*1209/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '8': {	  	
								dtmf_gen(2*pi*852/8000,2*pi*1336/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '9': {	  	
								dtmf_gen(2*pi*852/8000,2*pi*1477/8000,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '*': {	  	
								dtmf_gen(2*pi/8000*941,2*pi/8000*1209,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case '#': {	  	
								dtmf_gen(2*pi/8000*941,2*pi/8000*1477,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case 'A': {	  	
								dtmf_gen(2*pi/8000*697,2*pi/8000*1633,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case 'B': {	  	
								dtmf_gen(2*pi/8000*770,2*pi/8000*1633,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case 'C': {	  	
								dtmf_gen(2*pi/8000*852,2*pi/8000*1633,out_sample,SAMPLESIZE);
								break;
		 					   }
   					case 'D': {	  	
								dtmf_gen(2*pi/8000*941,2*pi/8000*1633,out_sample,SAMPLESIZE);
								break;
		 					   }
					default :  {	  		
			   						 
								break;
								}
				}
			
	
		}
}

void send()
{
    u16 i;
    u16 data;
    for(i=0;i<SAMPLESIZE;i++)
   { //*(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = out_sample[i];
     while (!MCBSP_RRDY(HANDSET_CODEC)) {};
     data = *(volatile u16*)DRR1_ADDR(HANDSET_CODEC);
    *(volatile u16*)DXR1_ADDR(DAA_CODEC) = (u16)(out_sample[i]/2);//是否需要查询一下?
   }
   delay(1000);
   //delay(1000);
   //delay(1000);
   //delay(1000);
   //delay(1000);
   
   /* for(i=0;i<1000000;i++)
    {
    
    *(volatile u16*)DXR1_ADDR(HANDSET_CODEC) =i;
    }*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?