DAC in Audio Codec Shield

DAC in Audio Codec Shield

Postby liner » Fri Jun 29, 2012 2:13 am

Hi all,

I just want to use the DAC in the Audio Codec Shield in a way that the Arduino uno sets a digital number and we can get an analogue output.

Any approaches of how to start?

Thankx
liner
 
Posts: 6
Joined: Fri Jun 29, 2012 2:11 am

Re: DAC in Audio Codec Shield

Postby guest » Fri Jun 29, 2012 9:39 am

that should be pretty easy
but be sure to take your output
from the line_out solder pads
and not the headphone

the headphone output is only AC voltages

in all of the examples there is a line like this
AudioCodec_data(&left_in, &right_in, left_out, right_out);
you write your value to left_out or right_out
depending upon where you have the wire connected
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: DAC in Audio Codec Shield

Postby liner » Thu Jul 05, 2012 1:48 am

Thank you, guest.

Just what I needed. It is quite simple in fact, I just did not understand well that function.
liner
 
Posts: 6
Joined: Fri Jun 29, 2012 2:11 am

Re: DAC in Audio Codec Shield

Postby vickyjungade » Tue Sep 18, 2012 3:35 am

What is acceptable value (range or discrete) as an argument to the function AudioCodec_data for giving out a voltage say 3.5V or 5V? I tried passing different values for left_out as well as right_out - 125, 500 or 32767. But none of these could give me any analog out (DAC out was zero).

I tried sine_generator sketch from the examples and could see sine wave on a scope (HP_Out); adjustable freq & amplitude with the mods - so nothing wrong with the hardware setup.

Inside sine_generator -
I am not able to understand the co-relation between values in the look up table (sinetable.inc) & the generated wave.
I would also like to know how pgm_read_word_near function works?
The Arduino IDE does not show this function in orange color like others. How is that???
I tried passing values from the lookup directly to this function, but that does not work as well.
vickyjungade
 
Posts: 6
Joined: Tue Sep 18, 2012 3:33 am
Location: Dombivli, Mumbai, Maharashtra, India

Re: DAC in Audio Codec Shield

Postby guest » Tue Sep 18, 2012 11:15 am

the headphone out is AC
so if you want a DC voltage that doesnt change with time
you will need to use the line_out pads

the DAC goes to 3.3v
and this is at 0x7fff = 32767

program_read_word_near() is a C function for reading values from internal flash program memory. there isnt an arduino command to do this. all it does, is fetch a value that has been stored into the program memory, at a location specified by the value that you give to the function. if you take these values and send them to left_out and right_out, they will appear at the output.

the values are signed integers, so 0x8000 = -32768 = 0v. 0x0000 = 0 = 1.65v. 0x7fff = +32767 = 3.3v. if you want to convert between signed and unsigned you can do this:
Code: Select all
unsigned int i = 0; // declare variable
signed int j = 0; // declare variable
i = j + 0x8000; // i is now an unsigned version of j
j = i + 0x8000; // j is now a signed version of i


note that its the same code to go from signed to unsigend, and from unsigned to signed.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: DAC in Audio Codec Shield

Postby vickyjungade » Sat Sep 22, 2012 2:43 am

Ok, so we tried to substitute the call to DAC as AudioCodec_data(&left_in, &right_in, 58773, 58773); or AudioCodec_data(&left_in, &right_in, 0x7fff, 0x7fff);

& tried to read with a scope from the pad labelled OUT from its R & L. see attachment.

Yet we are not getting any voltage.

suppose we try the original sine wave program, we can read sine wave as before.

please help me out!


see we modified only 1 line:
Code: Select all
/*
sine_generator.pde
guest openmusiclabs 7.7.11
this program creates a sinewave of variable frequency and
amplitude, presented at both left and right outputs. there
isnt any interpolation, so you only get 256 discrete frequencies
across the span of 44Hz to 10kHz.
*/

// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1kHz sample rate
#define ADCS 2 // use both ADCs

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>

// create data variables for audio transfer
// even though there is no input needed, the codec requires stereo data
int left_in = 0; // in from codec (LINE_IN)
int right_in = 0;
int left_out = 0; // out to codec (HP_OUT)
int right_out = 0;

// create variables for ADC results
// it only has positive values -> unsigned
unsigned int mod0_value = 0;
unsigned int mod1_value = 0;

// create sinewave lookup table
// PROGMEM stores the values in the program memory
// it is automatically included with AudioCodec.h
PROGMEM  prog_int16_t sinewave[]  = {
  // this file is stored in AudioCodec.h and is a 1024 value
  // sinewave lookup table of signed 16bit integers
  // you can replace it with your own waveform if you like
  #include <sinetable.inc>
};
unsigned int location; // lookup table value location


void setup() {
  // call this last if you are setting up other things
  AudioCodec_init(); // setup codec and microcontroller registers
}

void loop() {
  while (1); // reduces clock jitter
}

// timer1 interrupt routine - all data processed here
ISR(TIMER1_COMPA_vect, ISR_NAKED) { // dont store any registers

  // &'s are necessary on data_in variables
  AudioCodec_data(&left_in, &right_in, 58773, 58773);
 
  // create some temporary variables
  // these tend to work faster than using the main data variables
  // as they arent fetched and stored all the time
  int temp1;
  int temp2;
 
  // create a variable frequency and amplitude sinewave
  // fetch a sample from the lookup table
  temp1 = pgm_read_word_near(sinewave + location);
  // step through table at rate determined by mod1
  // use upper byte of mod1 value to set the rate
  // and have an offset of 1 so there is always an increment.
  location += 1 + (mod1_value >> 8);
  // if weve gone over the table boundary -> loop back
  // around to the other side.
  location &= 0x03ff; // fast way of doing rollover for 2^n numbers
                      // otherwise it would look like this:
                      // if (location >= 1024) {
                      // location -= 1024;
                      // }
 
  // set amplitude with mod0
  // multiply our sinewave by the mod0 value
  MultiSU16X16toH16(temp2, temp1, mod0_value);
  // our sinewave is now in temp2
  left_out = temp2; // put sinusoid out on left channel
  right_out = -temp2; // put inverted version out on right chanel

  // get ADC values
  // & is required before adc variables
  AudioCodec_ADC(&mod0_value, &mod1_value);

  reti(); // dont forget to return from the interrupt
}

Attachments
2012-09-22 15.05.58.jpg
2012-09-22 15.05.58.jpg (92.77 KiB) Viewed 22852 times
vickyjungade
 
Posts: 6
Joined: Tue Sep 18, 2012 3:33 am
Location: Dombivli, Mumbai, Maharashtra, India

Re: DAC in Audio Codec Shield

Postby guest » Sat Sep 22, 2012 11:15 am

heres how you can use the codecshield dac

Code: Select all
/*
dac_out_using_main.pde
guest openmusiclabs 9.22.12
this program allows the user to update the dac output in the main loop.
it also allows the user to use unsigned values.  but, it does not update
the sample at every sample period, so it may not work well for playing
back wavetables or other synchronous data.
*/


// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 0 // no ADCs are being used

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>

// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;

void setup() {
  AudioCodec_init(); // setup codec registers
  // call this last if setting up other parts
}

void loop() {
  unisgned int i; // do whatever calculating you want
  left_out = i +  0x8000; // convert to signed value
}

// timer1 interrupt routine
ISR(TIMER1_COMPA_vect) {
  // &'s are necessary on data_in variables
  AudioCodec_data(&left_in, &right_in, left_out, right_out); 
}


AudioCodec_data() can only take variables, not constant numbers.
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: DAC in Audio Codec Shield

Postby guest » Sat Sep 22, 2012 12:28 pm

also
if you want to tell me a bit more about what youre doing
i might be able to help more
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: DAC in Audio Codec Shield

Postby vickyjungade » Sun Sep 23, 2012 12:04 am

We could write through the DAC properly. Thanks!

We are implementing a PID as described in http://en.wikipedia.org/wiki/PID_controller. Any suggestions?

The ADC is to read the process variable, DAC is to transmit the control value.

Please explain how to:

0. Read the ADC properly. Which pin to connect the process variable signal? We could not correctly read a DC voltage (or sine) through function AudioCodec_data, (argument &left_in or &right_in) when connected to IN as shown in attachment.
1. set the interval of the ISR that calls the read & write functions, as we have to write our functionality in loop()
2. use the 24 bit ADC & 32 bit DAC for accuracy. Please explain how to? What is the 'maximum resulution' of ADC & DAC that can be used , i mean 16 bit or 18 bit or 24 bit. We need a ADC/DAC with max code width, hence this card!
1. the range for left_in & right_in is -32768 to 32767. Also the range for left_out & right_out is the same. Besides the defination of AudioCodec_data shows that these parameters are int. Why so?
2. the AudioCodec_readme.txt says, for the function AudioCodec_ADC - "the ADC is only a 10bit ADC". Why so?
3. Also, how to read & write, ADC & DAC by calls in loop function without using interrupt
Attachments
2012-09-23 16.30.56.jpg
2012-09-23 16.30.56.jpg (99.13 KiB) Viewed 22839 times
vickyjungade
 
Posts: 6
Joined: Tue Sep 18, 2012 3:33 am
Location: Dombivli, Mumbai, Maharashtra, India

Re: DAC in Audio Codec Shield

Postby guest » Sun Sep 23, 2012 12:40 pm

a PID controller sounds awesome

for that you will want to do all the processing in the ISR. it is important that the data gets processed in a synchronous fashion, and you could do it in the main loop, but it is easier and faster to do it in the ISR. here is some code that can get you started. your input signal will need to go from 0v to 3.3v.

Code: Select all
/*
dc_controller.pde
guest openmusiclabs 9.23.12
this takes in a dc value from the codecshield, processes it, and
sends out a dc value on the codecshield.
*/


// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 0 // no ADCs are being used
#define ADCHPD 1 // set the codecshield to DC input

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>

// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;
int last_value = 0;
int difference = 0;
int integral = 0;
int position = 0;

void setup() {
  AudioCodec_init(); // setup codec registers
  // call this last if setting up other parts
}

void loop() {
  while (1); // reduces clock jitter
}

// timer1 interrupt routine - all data processed here
ISR(TIMER1_COMPA_vect, ISR_NAKED) { // dont store any registers

  // &'s are necessary on data_in variables
  AudioCodec_data(&left_in, &right_in, left_out, right_out);
 
  difference = left_in - last_value; // create a difference signal
  position = left_in; // create a position signal
  integral +=  left_in; // create an integral signal
  last_value = left_in; // backup the last value
  // do the math here for pid
 
  // dont forget to return from interrupt
  reti();
}
guest
Site Admin
 
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Next

Return to Audio Codec Shield

Who is online

Users browsing this forum: No registered users and 0 guests


cron