⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aic23_loopback.c

📁 用dsp tms320f2812 实现的itu g.711音频压缩算法
💻 C
字号:
#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File
#include "aic23cfg.h"
#include "mcbspcfg.h"
#include "myapp.h"
#define AUDIODATA 0x80000
int *pAudio;
unsigned int G711ALawEncode(int nLeft,int nRight);
unsigned char IntToALaw(int nInput);
int ALawToInt(unsigned char nInput);
unsigned int left,right;
int *pWork,nAudioCount;
int i;
unsigned int uWork;
unsigned char cWork;
unsigned int bCodec=0;
int nWork,nWork1;
main()
{
   /* 
       Step 1. Initialize System Control:
       PLL, WatchDog, enable Peripheral Clocks
       This example function is found in the scsl\scsl.src\DSP281x_SysCtrl.c file.
   */    
   InitSysCtrl();
   
   /* 
       Step 2. Initialize TLVAIC23
       Master ,44.1K Hz MICIN input  PHONE output,etc.
       This example function is found in the aic23cfg.c file.
   */    
   AIC23_cfg();
   
   /* 
       Step 3. Clear all interrupts and initialize PIE vector table:
       Disable CPU interrupts 
   */    
   DINT;
   
   /*   
       Step 4. Initialize PIE control registers to their default state.
       The default state is all PIE interrupts disabled and flags
       are cleared.  
       This function is found in the scsl\scsl.src\DSP281x_PieCtrl.c file.
   */   
   InitPieCtrl();
   
   /*

       Step 5. Disable CPU interrupts and clear all CPU interrupt flags:
   */    
   IER = 0x0000;
   IFR = 0x0000;
   
   /*
       Step 6. Initialize the PIE vector table with pointers to the shell Interrupt 
       Service Routines (ISR).  
       This will populate the entire table, even if the interrupt
       is not used in this example.  This is useful for debug purposes.
       The shell ISR routines are found in DSP281x_DefaultIsr.c.
       This function is found in bcsl\bcsl.src\DSP281x_PieVect.c.
   */
   InitPieVectTable();
   
   /*  
       Step 7. Step 2. Initialize McBSP
       Slave ,external clkxr fsxr ,etc.
       This example function is found in the mcbspcfg.c file.
   */
   McBSP_cfg();
   /*   
       Step 8. Enable interrupts required for this example
   */
   EINT;   
   
   /*
       Step 9. IDLE loop .the main functions is wait the interrupt.
   */
   for(;;);
}
interrupt void mcbspRxFifoIsr(void)
{  
   //Uint16 rdata_mcbsp[8];                  // Recieved  chn Data 
   for(i=0; i<8; i++)
   {   
       //rdata_mcbsp[i]=McbspaRegs.DRR1.all;
       left = McbspaRegs.DRR1.all;	// 读入左声道数据
        right = McbspaRegs.DRR2.all;	// 读入右声道数据
        if ( bCodec )
        {
        	uWork=G711ALawEncode(left,right);
        	(*pWork)=uWork;
        	cWork=uWork>>8;
        	left=ALawToInt(cWork);
        	cWork=uWork&0x0ff;
        	right=ALawToInt(cWork);
		}
        McbspaRegs.DXR1.all=left;	
        McbspaRegs.DXR2.all=right;	
        nAudioCount++; pWork++;
        if ( nAudioCount>=1024 )
        {
        	nAudioCount=0;
        	pWork=pAudio;
        }
       /*if ( bCodec )				// 
        {
          
         McbspaRegs.DXR1.all=nWork1;       
         McbspaRegs.DXR2.all=uSound[nSoundNumber];
         
        }
       if(bCodec==0)
        { McbspaRegs.DXR1.all=uSound[nSoundNumber];   
          McbspaRegs.DXR2.all=uSound[nSoundNumber];
          nSoundNumber++;
        }
       if ( nSoundNumber>=SOUNDBUFFERLENGTH )
		 {		
			nSoundNumber=0;			
		 }*/
   }
   McbspaRegs.MFFRX.bit.RXFFOVF_CLEAR=1;   // Clear Overflow flag
   McbspaRegs.MFFRX.bit.RXFFINT_CLEAR=1;   // Clear Interrupt flag
   PieCtrlRegs.PIEACK.all|=0x20;           // Issue PIE ack
}

unsigned int G711ALawEncode(int nLeft,int nRight)
{
	unsigned char cL,cR;
	unsigned int uWork;

	cL=IntToALaw(nLeft);
	cR=IntToALaw(nRight);
	uWork=cL; uWork<<=8; uWork|=cR;
	return(uWork);
}
    
unsigned char IntToALaw(int nInput)
{
	char segment;
	unsigned int i, sign,quant;
	unsigned int absol, temp;
	int nOutput;
	unsigned char cOutput;

	temp=absol=abs(nInput);
	sign=(nInput >= 0)?1:0;
	for(i=0;i<16;i++){
	nOutput=temp&0x8000;
	if(nOutput)break;
	temp<<=1;
	}
	segment=11-i;
	if(segment<=0){
	segment=0;
	quant=(absol>>1)&0x0F;
	}
	else
	quant=(absol>>segment)&0x0F;
	segment<<=4;
	nOutput=segment+quant;
	if(absol>4095) nOutput=0x7F;
	if(sign)
		nOutput^=0xD5;
	else
		nOutput^=0x55;
	cOutput=(unsigned char)nOutput;
	return cOutput;
}

int ALawToInt(unsigned char nInput)
{
	int sign, segment;
	int temp, quant,nOutput;
	
	temp=nInput^0xD5;
	sign=(temp&0x80)>>7;
	segment=(temp&0x70)>>4;
	quant=temp&0x0F;
	quant<<=1;
	if(!segment)
	quant+=1;
	else{
	quant+=33;
	quant<<=segment-1;
	}
	if ( sign )
		nOutput=-quant;
	else
		nOutput=quant;
	return nOutput;
}

⌨️ 快捷键说明

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