Hey. I need to generate a variable frequency sinewave using DAC. I have implement sine wave look up table and phase accumulator:
I havent implemented it as I do not have my hardware yet.
I have one concern though - The microprocessor that I am using is stm32f412 and the DAC chip -
http://www.ti.com/general/docs/supp...&gotoUrl=http://www.ti.com/lit/gpn/dac121s101.
1. What clock frequency would I need to choose to generate 1Hz - 20Khz sine wave?
The DAC chip is connected to my microcontroller through SPI interface and the clock is set to 50Mhz.
2. I am also unsure about phase accumulator resolution. Does it have to match DAC resolution? Most websites say that most common phase accumulator use 24bit or 48bit resolution and I chose to use 12bit because thats my DAC resolution.
C:
void SINE_LOOKUP_TABLE(){
const int BUFF_SIZE = 4096; // size of output buffer (samples)
const int Fs = 32.768; // sample rate (Hz)
const int LUT_SIZE = 1024; // lookup table size
int16_t LUT[LUT_SIZE]; // our sine wave LUT
for (int i = 0; i < LUT_SIZE; ++i)
{
LUT[i] = (int16_t)sinf(((float)i *360)/1024) *2047;
} // fill LUT with 16 bit sine wave sample values
}
//INPUT WHAT FREQUENCY YOU WANT
void SINE_VARIABLE_FREQUENCY(int frequency){
int16_t buff[BUFF_SIZE]; // output buffer
const int BUFF_SIZE = 4096; // size of output buffer (samples)
const int frequency = 1000; // frequency we want to generate (Hz)
const int LUT_SIZE = 1024; // lookup table size
const int Fs = 32.768; // sample rate (Hz)
const float delta_phi = (float) frequency / (float) Fs * LUT_SIZE;
// phase increment
float phase = 0.0f; // phase accumulator
// generate buffer of output
for (int i = 0; i < BUFF_SIZE; ++i)
{
int phase_i = (int)phase; // get integer part of our phase
buff[i] = LUT[phase_i]; // get sample value from LUT
phase += delta_phi; // increment phase
if (phase >= (float)LUT_SIZE) // handle wraparound
phase -= (float)LUT_SIZE;
}
}
I have one concern though - The microprocessor that I am using is stm32f412 and the DAC chip -
http://www.ti.com/general/docs/supp...&gotoUrl=http://www.ti.com/lit/gpn/dac121s101.
1. What clock frequency would I need to choose to generate 1Hz - 20Khz sine wave?
The DAC chip is connected to my microcontroller through SPI interface and the clock is set to 50Mhz.
2. I am also unsure about phase accumulator resolution. Does it have to match DAC resolution? Most websites say that most common phase accumulator use 24bit or 48bit resolution and I chose to use 12bit because thats my DAC resolution.
Attachments
-
261.4 KB Views: 7