DSPRelated.com
Forums

codec running at different frequencies

Started by sahana rao June 5, 2002
hello

i am trying to program the onchip codec TLC320AD50C on
TMS320VC5402. the program i have written is shown
below. here i am setting the sample rate of the codec
depending on the input frequency. but it works only
for some frequency can anyone tell me what is the
problem in the code.

#include <stdio.h>
#include <stdlib.h>
#include <type.h>
#include <board.h>
#include <codec.h>
#include <mcbsp54.h>
#include <intr.h>
#include "LookUpTable.c"

#define TRUE 1
#define FALSE 0
#define LEFT 0
#define RIGHT 1
#define BOTH 2
#define BUF_SIZE 8192
#define MAX_FREQ 550 /*frequency of the sine
wave*/

/*User defined functions*/
void fnSetSampleFreq(void);
void fnChangeToneFreq(void);
void output_sample(int data);
void blink_LEDs();
void init_A50CODEC(CodecSampleRate sample_rate);
static void fnGenSineTable(signed int *pBuf, unsigned
int size, unsigned int ch);
static void fnDataTransmit(unsigned int size);

/*Global variables*/
unsigned int toneFreq = MAX_FREQ;

/*used to generate sine wave using a table lookup*/
typedef struct
{
u32 offset;
u16 delta;
} Sine, PSine;

Sine sig = {0, 0}; // Create a Sine
data variable
signed int toneBuf[2][BUF_SIZE]; // Create two
buffers, so we are always
// writing to a
different one than we
// are playing from.
signed int *pBuf[2];
unsigned uPt = 0;
unsigned int ch;
int bufNum=0;
unsigned uHcheck,uDcheck;
void fnCodecReady(void);
HANDLE hHandset,hTelHandset;
unsigned int sampleFreq;
CodecSampleRate samplerate;
/*end*/

void main()
{
int k;
uPt = 0;
CPLD_CTRL1_REG = CPLD_CTRL1_DEFAULT;
port0 = CPLD_CTRL1_REG;
brd_init(40); /*Initialize the DSK board at
CLKSPD@MHZ*/
blink_LEDs(); /*Turn on LEDs to verify program
operation*/
for(k = 0;k < BUF_SIZE;k++)
{
toneBuf[0][k] = 0;
toneBuf[1][k] = 0;
}
fnChangeToneFreq();
while(1)
{
fnCodecReady();
}
}

void fnCodecReady(void)
{
uHcheck = MCBSP_XRDY(HANDSET_CODEC);
uDcheck = MCBSP_XRDY(DAA_CODEC);
if(uHcheck == 1)
{
fnDataTransmit(BUF_SIZE);
}
}

static void fnDataTransmit(unsigned int size)
{
/*Read sample from the secondary buffer and write to
handset codec*/
output_sample(toneBuf[!bufNum][uPt]);
uPt = uPt+2;
if(uPt >= size)
{
uPt = 0;
}
bufNum ^= 1;
}

void output_sample(int data)
{
*(volatile u16*)DXR1_ADDR(HANDSET_CODEC) = data;
*(volatile u16*)DXR1_ADDR(DAA_CODEC) = data;
}

void fnChangeToneFreq(void)
{
fnSetSampleFreq();
init_A50CODEC(samplerate); /*Init DSK CODEC Fs
depending on tone freq*/

/* This calculation tells us how much over we have to
move every sample within the
sample table. Example:

If sampleFreq = 8000, and toneFreq = 2000, then
sig.delta = 64. Thus, we will
look at sine_table data at index 0, 64, 128, 192,
etc...*/
sig.delta = (((float)toneFreq / sampleFreq) *
4096);
/*This creates the initial sine wave buffer*/
fnGenSineTable(toneBuf[0],BUF_SIZE,BOTH);

}

void init_A50CODEC(CodecSampleRate sample_rate)
{
/* Open Handset Codec */
hHandset = codec_open(HANDSET_CODEC); /* Acquire
handle to codec */

/* Open DAA Codec */
hTelHandset = codec_open(DAA_CODEC);

/* Set Handset codec parameters */
codec_dac_mode(hHandset, CODEC_DAC_15BIT);
/* DAC in 15-bit mode */
codec_adc_mode(hHandset, CODEC_ADC_15BIT);
/* ADC in 15-bit mode */
codec_ain_gain(hHandset, CODEC_AIN_0dB);
/* 0dB gain on analog input to ADC */
codec_aout_gain(hHandset, CODEC_AOUT_MINUS_0dB);
/* -0dB gain on analog output from DAC */
codec_sample_rate(hHandset,sample_rate);
/* sampling rate > 2Fs */

/* Set DAA codec parameters */
codec_dac_mode(hTelHandset, CODEC_DAC_15BIT);
/* DAC in 15-bit mode */
codec_adc_mode(hTelHandset, CODEC_ADC_15BIT);
/* ADC in 15-bit mode */
codec_ain_gain(hTelHandset, CODEC_AIN_0dB);
/* 0dB gain on analog input to ADC */
codec_aout_gain(hTelHandset,
CODEC_AOUT_MINUS_0dB); /* -0dB gain on analog
output from DAC */
codec_sample_rate(hTelHandset,sample_rate);
}

void fnSetSampleFreq(void)
{
if(toneFreq >= 0.001 && toneFreq <= 700)
{
sampleFreq = 2000;
samplerate = SR_2000;
}
if(toneFreq > 700 && toneFreq <= 1000)
{
sampleFreq = 3200;
samplerate = SR_3200;
}
if(toneFreq > 1000 && toneFreq <= 2000)
{
sampleFreq = 5333;
samplerate = SR_5333;
}
if(toneFreq > 2000 && toneFreq <= 3000)
{
sampleFreq = 8000;
samplerate = SR_8000;
}
if(toneFreq > 3000 && toneFreq <= 4000)
{
sampleFreq = 9142;
samplerate = SR_9142;
}
if(toneFreq > 4000 && toneFreq <= 5000)
{
sampleFreq = 10667;
samplerate = SR_10667;
}
if(toneFreq > 5000)
{
sampleFreq = 16000;
samplerate = SR_16000;
}

}
void blink_LEDs()
{
int cnt=2;

while ( cnt-- )
{
brd_led_toggle(BRD_LED0);
brd_delay_msec(500);
brd_led_toggle(BRD_LED1);
brd_delay_msec(500);
brd_led_toggle(BRD_LED2);
brd_delay_msec(500);
}
}

static void fnGenSineTable(signed int *pBuf, unsigned
int size, unsigned int ch)
{
unsigned int i = 0;
signed int temp;

for (i=0; i<size; i++)
{
/*If we are past the end of the table, go to the
beginning.*/
while ((float)sig.offset > (TABLE_SIZE - 1))

sig.offset = (float)(sig.offset - (TABLE_SIZE
- 1));

temp = (sine_table[(int)sig.offset]) ;

/*Store the current calculated value into the
primary buffer*/
if ((ch == LEFT) || (ch == BOTH) )
pBuf[i] = temp;
else
pBuf[i] = 0;

++i;

if ( (ch == RIGHT) || (ch == BOTH) )
pBuf[i] = temp;
else
pBuf[i] = 0;

sig.offset += sig.delta; /*Calculate the next
offset value*/
}
}

thanks
sahana ________________________________________________________________________
Everything you always wanted to know about cars and bikes,now
at: http://in.autos.yahoo.com/cricket/tracker.html