📄 tone.c
字号:
/*
* Copyright 2005 by Spectrum Digital Incorporated.
* All rights reserved. Property of Spectrum Digital Incorporated.
*/
/*
* ======== tone.c ========
*
* This example uses the PCM3002 codec module of the DSK5416 Board Support
* Library to generate a 1KHz sine wave on the audio outputs for 5 seconds.
* The sine wave data is calculated and stored in an array called sinetable.
* The codec operates at 48KHz by default. Since the sine wave table has 48
* entries per period, each pass through the inner loop takes 1 millisecond.
* 5000 passes through the inner loop takes 5 seconds.
*
* Please see the DSK5416 help file for more detailed information.
*/
#include "tonecfg.h"
#include "dsk5416.h"
#include "dsk5416_pcm3002.h"
#include <math.h>
#define SINE_TABLE_SIZE 48
#define PI ( ( double )3.1415927 )
#define SINE_MAX 0x7FFE
#define HD_SIZE 81
#define HD_MAX 0x0FFE
int sinetable[SINE_TABLE_SIZE];
double hdi[HD_SIZE];
int hd[HD_SIZE];
DSK5416_PCM3002_Config setup = {
0x01ff, // Set-Up Reg 0 - Left channel DAC attenuation
0x01ff, // Set-Up Reg 1 - Right channel DAC attenuation
0x04A2, // Set-Up Reg 2 - Various ctl e.g. power-down modes
0x0000 // Set-Up Reg 3 - Codec data format control
};
/*
* InitSineTable() - Initialize the sine wave table data
*/
void InitHdTableLowPass( void )
{
int i;
double increment= 0;
double radian = 0;
increment = 0.15*PI;
// radian= increment * (-(HD_SIZE-1)/2);
for ( i = 0 ; i < HD_SIZE ; i++ )
{
if(i!=40){
radian=increment*(i-40);
hdi[i] =sin(radian)/radian;
}
else
hdi[i]=1.0;
}
for(i=0;i<HD_SIZE;i++)
{
hd[i]=(int)(hdi[i]*84);
}
}
void InitHdTableHighPass( void )
{
int i;
double increment= 0;
double radian = 0;
increment = 0.125*PI;
// radian= increment * (-(HD_SIZE-1)/2);
for ( i = 0 ; i < HD_SIZE ; i++ )
{
if(i!=40){
radian=increment*(i-40);
hdi[i] =sin(radian)/radian;
}
else
hdi[i]=-7.0;
}
for(i=0;i<HD_SIZE;i++)
{
hd[i]=(int)(-hdi[i]*84);
}
}
void InitHdTableBandPass( void )
{
int i;
double increment1= 0;
double increment2= 0;
double radian1 = 0;
double radian2 = 0;
increment1 = 0.425*PI;
increment2 = 0.0375*PI;
// radian= increment * (-(HD_SIZE-1)/2);
for ( i = 0 ; i < HD_SIZE ; i++ )
{
if(i!=40){
radian1=increment1*(i-40);
radian2=increment2*(i-40);
hdi[i] =(sin(radian1)-sin(radian2))/(i-40)/PI;
}
else
hdi[i]=0.3875;
}
for(i=0;i<HD_SIZE;i++)
{
hd[i]=(int)(hdi[i]*509);
}
}
void InitHdTableBandBlock( void )
{
int i;
double increment1= 0;
double increment2= 0;
double radian1 = 0;
double radian2 = 0;
increment1 = 0.3*PI;
increment2 = 0.125*PI;
// radian= increment * (-(HD_SIZE-1)/2);
for ( i = 0 ; i < HD_SIZE ; i++ )
{
if(i!=40){
radian1=increment1*(i-40);
radian2=increment2*(i-40);
hdi[i] =(sin(radian1)-sin(radian2))/(i-40)/PI;
}
else
hdi[i]=-0.825;
}
for(i=0;i<HD_SIZE;i++)
{
hd[i]=(int)(-hdi[i]*509);
}
}
/*
* UserTask() - The main user task
*/
void UserTask()
{
}
void UserTask1()
{
DSK5416_PCM3002_CodecHandle hCodec;
int i,j;
Int16 tempval;
int tempin[HD_SIZE];
int tempout;
/* Initialize the sinetable[] */
// InitHdTableLowPass();
// InitHdTableHighPass();
// InitHdTableBandPass();
InitHdTableBandBlock();
/* Start the codec */
hCodec = DSK5416_PCM3002_openCodec( 0, &setup );
DSK5416_PCM3002_setFreq(hCodec,24000);
/* Generate a 1KHz sine wave for 5 seconds */
for (;;)
{
while(!DSK5416_PCM3002_read16(hCodec,&tempval));
while(!DSK5416_PCM3002_read16(hCodec,&tempval));
for(i=0;i<HD_SIZE-1;i++)
tempin[i]=tempin[i+1];
tempin[HD_SIZE-1]=tempval/500;
tempout=0;
for(j=0;j<HD_SIZE;j++)
{
tempout+=tempin[j]*hd[HD_SIZE-1-j];
}
while(!DSK5416_PCM3002_write16(hCodec,tempval));
while(!DSK5416_PCM3002_write16(hCodec,tempout));
}
/* Close the codec */
// DSK5416_PCM3002_closeCodec( hCodec );
}
void main()
{
/* Call BSL init */
DSK5416_init();
UserTask1();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -