0

I'm trying to use the Sine Table lookup method to find the tone frequency at different step size, but when I'm converting the floating point to integer and use the oscicopte to view the frequncy, it can't display any things on screen.

Does anyone know what's the solution for this issues. Any help is apperaite.

Below is the code:

// use the formula: StepSize = 360/(Fs/f) Where Fs is the Sample frequency 44.1 kHz and f is the tone frequency.
// example: StepSize = 360/(44100/440) = 3.576, since the STM32 doesn't support the floating point, therefore, we have to use the fixed-point format which multiply it by 1000 to be 3575

int StepSize = 3575;  
unsigned int v=0;

signed int sine_table[91] = {
          0x800,0x823,0x847,0x86b,
          0x88e,0x8b2,0x8d6,0x8f9,
          0x91d,0x940,0x963,0x986,
          0x9a9,0x9cc,0x9ef,0xa12,
          0xa34,0xa56,0xa78,0xa9a,
          0xabc,0xadd,0xaff,0xb20,
           0xb40,0xb61,0xb81,0xba1,
           0xbc1,0xbe0,0xc00,0xc1e,
           0xc3d,0xc5b,0xc79,0xc96,
           0xcb3,0xcd0,0xcec,0xd08,
            0xd24,0xd3f,0xd5a,0xd74,
            0xd8e,0xda8,0xdc1,0xdd9,
            0xdf1,0xe09,0xe20,0xe37,
            0xe4d,0xe63,0xe78,0xe8d,
            0xea1,0xeb5,0xec8,0xedb,
            0xeed,0xeff,0xf10,0xf20,
            0xf30,0xf40,0xf4e,0xf5d,
            0xf6a,0xf77,0xf84,0xf90,
            0xf9b,0xfa6,0xfb0,0xfba,
            0xfc3,0xfcb,0xfd3,0xfda,
            0xfe0,0xfe6,0xfec,0xff0,
            0xff4,0xff8,0xffb,0xffd,
            0xffe,0xfff,0xfff};

unsigned int sin(int x){
   x = x % 360;
   if(x <= 90)
      return sine_table[x];
    else if ( x <= 180){
      return sine_table[180 - x];
    }else if ( x <= 270){
      return 4096 - sine_table[x - 180];
    }else{
      return 4096 - sine_table[360 - x];
     }
}

void main(void)
{
while(1){
            v+=StepSize;                // Don't know why it doesn't work in this way. not display anything on screen.
           DAC->DHR12R2 = sin(v/1000);      // DAC channel-2 12-bit Right aligned data
           if (v >= 360) v = 0;
           }
}

enter image description here

But, if I change the StepSize = 3; it shows the frequency:

enter image description here

glts
  • 21,808
  • 12
  • 73
  • 94
user3435575
  • 31
  • 1
  • 8

1 Answers1

0

There are a few issues with your code. But I will start with the one that you asked about.

int StepSize = 3575;  
unsigned int v=0;
while(1){
       v+=StepSize;
       DAC->DHR12R2 = sin(v/1000);
       if (v >= 360) v = 0;
}

The reason why this code doesn't work is that v is always set to 0 at the end of the loop because 3575 is greater than 360. So then you always call sin(3) because 3575/1000 is 3 in integer division.

Perhaps, you should rewrite your last line as if ((v/1000) >= 360) v = 0;. Otherwise, I would rewrite your loop like this

while(1){
       v+=StepSize;
       v/=1000;
       DAC->DHR12R2 = sin(v);
       if (v >= 360) v = 0;
}    

I would also recommend that you declare your lookup table a const. So it would look like

const signed int sine_table[91] = {

Last recommendation is to choose another name for your sin function so as not to confuse with the sin library function. Even though in this case there shouldn't be a problem.

Community
  • 1
  • 1
jayant
  • 2,349
  • 1
  • 19
  • 27