This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F2808: Sine wave generation on TMS320F2808

Part Number: TMS320F2808

Hello,

I have been developing sine wave modulator for three phase voltage source inverter

on TMS320F2808 DSP. I have been using CCS v6 for debugging (especially the Graph

tool). I have developed the code which is intended for desired sine wave generation (please

see the attachment). My problem is that the sine wave produced by this code is OK only

at some frequencies (I watch the output sine wave in Graph tool). Now I don't know whether

the problem is in my code or in graph in CCS. Please can anybody look at my code and evaluate

whether it is allright? Below are the graphs for:

1. 50 Hz ~ 32767
2. 49 Hz ~ 32200
3. 45 Hz ~ 30000
4. 25 Hz ~ 16384

Thanks in advance. 

// index, 16 bits, 0-65535
static unsigned short index1 = 0;
static unsigned short index2 = 0;
// desired output frequency, 50 Hz ~ 32767
static unsigned short frequency = 32767;
// temporary variable
static short temp = 0;


// table with sine values in Q1.15
// table covers full period <0, 2*pi> with 129 values (due to linear interpolation)
// angle step in table (2*pi)/128 ~ 2.8 °
// normalized angle step (2*pi)/128*1/(2*pi)*65536 = 512 
static short sine_table[] = {
	     0,   1608,   3212,   4808,
	  6393,   7962,   9512,  11039,
	 12539,  14010,  15446,  16846,
	 18204,  19519,  20787,  22005,
	 23170,  24279,  25329,  26319,
	 27245,  28105,  28898,  29621,
	 30273,  30852,  31356,  31785,
	 32137,  32412,  32609,  32728,
	 32767,  32728,  32609,  32412,
	 32137,  31785,  31356,  30852,
	 30273,  29621,  28898,  28105,
	 27245,  26319,  25329,  24279,
	 23170,  22005,  20787,  19519,
	 18204,  16846,  15446,  14010,
	 12539,  11039,   9512,   7962,
	  6393,   4808,   3212,   1608,
	     0,  -1608,  -3212,  -4808,
	 -6393,  -7962,  -9512, -11039,
	-12539, -14010, -15446, -16846,
	-18204, -19519, -20787, -22005,
	-23170, -24279, -25329, -26319,
	-27245, -28105, -28898, -29621,
	-30273, -30852, -31356, -31785,
	-32137, -32412, -32609, -32728,
	-32767, -32728, -32609, -32412,
	-32137, -31785, -31356, -30852,
	-30273, -29621, -28898, -28105,
	-27245, -26319, -25329, -24279,
	-23170, -22005, -20787, -19519,
	-18204, -16846, -15446, -14010,
	-12539, -11039,  -9512,  -7962,
	 -6393,  -4808,  -3212,  -1608,
	 0
};

// angle step for desire output frequency
static unsigned short delta_phase = 0;
// phase accumulator
static unsigned short phase = 0;
// output sine wave
static short sine_wave = 0;


// angle step for desired output frequency which is set by frequency variable
// for one interrupt with frequency fs the angle step in radians is 2*pi*f/fs
// so the normalized angle step is f/fs*65536 
// for desired 50 Hz and fs=12000 Hz: 50/12000*65536 = 273
// 50 Hz is set as 32 767
delta_phase = ((((unsigned long)frequency)*273) >> 15);


// increment phase accumulator with angle step
phase += delta_phase;


// "low" index
// phase to index conversion 
// 128 values in table so only 7 highest bits set the index 
// phase accumulator is 16 bit so >> 9
index1 = phase >> 9;
// "high" index
index2 = index1 + 1;

// retrieve the sine value from the table
// linear interpolation in the sine values table
temp = (*(sine_table + index2) - *(sine_table + index1));
// low 9 bits of the phase accumulator are "fractional" so & 0x1FF
// "full" angle step in table is 512 so >> 9
temp = (short)(((long)temp)*((phase & 0x1FF) >> 9));
temp += *(sine_table + index1);
// voltage to frequency ratio
sine_wave = (((long)frequency*temp) >> 15);


  • Hi user4318460,

    Which frequencies are working and failing? Is there a low or high frequency threshold where your code starts to fail, or is it just random frequencies that don't work? What does the CCS graph look like in the working and not working cases?
  • Hi Devin,

    I have appended the output graphs to my first message.
    I have found that the only two frequencies which are
    displayed allright are 50 Hz (~32767) and 25 Hz (~16384).
    I have been using the graph tool in following manner.
    I have defined circular buffer with 480 elements which
    is filled in 12 kHz ISR. I have passed on the address of the
    buffer's first element to the graph tool along with the
    sampling frequency 12 kHz and length.

  • Hi user'460

    Looking through your code, you have the number '273' in this line of code:

    delta_phase = ((((unsigned long)frequency)*273) >> 15);

    According to the comments, this is based on a calculation for 50Hz. Does this need to change for each frequency?
  • Hello Devin,
    the value 273 is fixed normalized angle step for 50 Hz and sampling frequency equal 12 kHz. The desired output frequency
    is set according to relation 50 Hz~32767. I tested the conversion between desired frequency and delta_phase and it is alright.
  • Hi user'460

    How long do you fill up the circular buffer before graphing? I'd suggest maybe setting a breakpoint that triggers after the buffer is full. You can then examine the first capture in the graph tool, or manually in the memory browser and see if you have any discontinuities.

    If everything looks good on the first pass, maybe keep going and break a few more times when the buffer is full. You can again check the buffer contents in the memory browser and graphing tool to see how the algorithm is failing.