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

📄 dac_api.c

📁 采用32位嵌入式芯片SPCE3200(凌阳)设计的开发板,具有多媒体功能。其中包含了网络、SD卡、TV、UART等是全部十个C程序。
💻 C
字号:
//====================================================================================
//文 件 名:DAC_API.c
//功能描述: DAC驱动程序(API)
//维护记录:
//			2007.01.17	V0.1	by wangtao <wangtao@sunnorth.com.cn>
//====================================================================================
#include "SPCE3200_Register.h"
#include "SPCE3200_Constant.h"
#include "DAC_API.h"

unsigned int DAC_Get_PCM_Free_Bytes(void);

unsigned short DAC_TempPCM[DAC_PCM_BUFFER_SIZE];
unsigned int DAC_temp_rp=0;
unsigned int DAC_temp_wp=0;
unsigned short DAC_FIFOArray[4096];
unsigned int DAC_FIFO_Offset=0;
unsigned char  DAC_FIFOCounter=0;
unsigned char  DAC_Output_Type;
unsigned char  DAC_IRQ_Flag;

#ifdef FOR_DAC_DEBUG
unsigned int DAC_SampleCount=0;
#endif

void DAC_CheckFIFOEnough(void);

//=============================================================
//语法格式:	void DAC_FillSoftFIFO(void)
//实现功能:	填充软件FIFO
//参数:		无
//返回值:		无
//=============================================================
void DAC_FillSoftFIFO(void)
{
	unsigned short TempValue=0;
	unsigned short TempCounter=0;

  		if(DAC_Output_Type == Stereo)
		{

			for(TempCounter=0;TempCounter<2048;TempCounter++)
			{
				TempValue = DAC_TempPCM[DAC_temp_rp];
				TempValue ^= 0x8000;
				DAC_FIFOArray[TempCounter+DAC_FIFO_Offset] = TempValue;
			    DAC_temp_rp++;
				if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
					DAC_temp_rp = 0;
			}
		}
		else if(DAC_Output_Type == Left_Only)
		{

			for(TempCounter=0;TempCounter<1024;TempCounter++)	// for stereo
			{
				TempValue = DAC_TempPCM[DAC_temp_rp];
				TempValue ^= 0x8000;
				DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
				DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;
			    DAC_temp_rp += 2;
				if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
					DAC_temp_rp = 0;
			}
		}
		else if(DAC_Output_Type == Right_Only)
		{

			for(TempCounter=0;TempCounter<1024;TempCounter++)	// for stereo
			{
				TempValue = DAC_TempPCM[DAC_temp_rp+1];
				TempValue ^= 0x8000;
				DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
				DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;
				DAC_temp_rp += 2;
				if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
					DAC_temp_rp = 0;
			}
		}
		else if(DAC_Output_Type == MonoToStereo)
		{

			for(TempCounter=0;TempCounter<1024;TempCounter++)
			{
				TempValue = DAC_TempPCM[DAC_temp_rp];
				TempValue ^= 0x8000;
				DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
				DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;

				if(DAC_temp_rp!=DAC_temp_wp)
				{
				  DAC_temp_rp++;
				  #ifdef FOR_DAC_DEBUG
                    DAC_SampleCount++;
                  #endif
				}
				else
				{
					DAC_temp_rp = DAC_temp_wp;
				}

				if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
					DAC_temp_rp = 0;
			}
		}
		else
		{
			//
		}


	if(DAC_FIFOCounter != 0)
	{
		DAC_FIFO_Offset = 0;
		DAC_FIFOCounter = 0;
	}
	else
	{
		DAC_FIFO_Offset += 2048;
		DAC_FIFOCounter = 1;
	}

	return;
}

//=============================================================
//语法格式:	void DAC_Write_PCM_Data(unsigned short *PCMPtr,unsigned int PCMSizes)
//实现功能:	填充PCM缓冲区
//参数:		PCMPtr: PCM数据首地址
//              PCMSizes: 填充大小
//返回值:		无
//=============================================================
void DAC_Write_PCM_Data(unsigned short *PCMPtr,unsigned int PCMSizes) //PCMSizes unit: bytes
{
	unsigned int i;

	for(i=0;i<(PCMSizes/2);i++)
	{
		DAC_TempPCM[DAC_temp_wp++] = PCMPtr[i];

		if(DAC_temp_wp == DAC_PCM_BUFFER_SIZE)
		{
			DAC_temp_wp = 0;
		}
	}


}

//=============================================================
//语法格式:	unsigned int DAC_Get_PCM_Free_Bytes(void)
//实现功能:	得到PCM缓冲区空余大小
//参数:		无
//返回值:		空余大小
//=============================================================
unsigned int DAC_Get_PCM_Free_Bytes(void)
{
	unsigned int space;

	space = DAC_temp_rp - DAC_temp_wp;
	if(space < 0)
		space += DAC_PCM_BUFFER_SIZE;

	if(space == 0)
	{
		return ((DAC_PCM_BUFFER_SIZE*2)-2);
	}

	return ((space*2)-2);
}

//=============================================================
//语法格式:	void DAC_EnableSoftCh(unsigned int Samplerate)
//实现功能:	使能软件通道
//参数:		Samplerate: 采样率
//返回值:		无
//=============================================================
void DAC_EnableSoftCh(unsigned int Samplerate)
{
	unsigned int TempValue;

	DAC_Output_Type=MonoToStereo;

	DAC_temp_rp=0;
    DAC_temp_wp=0;
    DAC_FIFO_Offset=0;
    DAC_FIFOCounter=0;

//	*P_DAC_BUFFER_SA = 0;
	*P_DAC_FIFOBA_LOW = ((unsigned int)DAC_FIFOArray);			//Set start address of buffer for DAC soft channel
	*P_DAC_FIFOBA_HIGH = ((unsigned int)DAC_FIFOArray) >> 16;

	TempValue = 27000000/Samplerate - 1;					//Set sampling rate of current PCM file
	*P_DAC_SAMPLE_CLK = TempValue;


	*P_DAC_INT_STATUS = 0x4000 | 0x0004 | 0x0003;			//Enable software channel as stereo, 8Kbytes buffer
	*P_DAC_MODE_CTRL2  = (0x0608 | D_VolSel_1 | 0x1003); // for both channel
}

//=============================================================
//语法格式:	void DAC_CheckFIFOEnough(void)
//实现功能:	等待FIFO中断
//参数:		无
//返回值:		无
//=============================================================
void DAC_CheckFIFOEnough(void)
{
   DAC_IRQ_Flag=0;
   while(DAC_IRQ_Flag==0);
}

//=============================================================
//语法格式:	void DAC_DisableSoftCh(void)
//实现功能:	关闭软件通道
//参数:		无
//返回值:		无
//=============================================================
void DAC_DisableSoftCh(void)
{
	*P_DAC_INT_STATUS = 0;					//Stop DAC software channel
	*P_DAC_MODE_CTRL2  = 0;
}

⌨️ 快捷键说明

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