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.

MSP430FG437 Sine Wave Generator

Other Parts Discussed in Thread: MSP430FG437

We have developed MSP430FG437 based sine wave generator using sine table.

 

// This is the working code

 

#include <msp430fg437.h>
#include <math.h>

void DAC0_Initial(void);

float sine_value[] = {0.00000, 0.08715, 0.17364, 0.25881,
                      0.34202, 0.42261, 0.50000, 0.57357,
                      0.64278, 0.70710, 0.76604, 0.81915,
                       0.86602, 0.90630, 0.93969, 0.96592,
                      0.98480, 0.99619, 1.00000, 0.99619,
                      0.98480, 0.96592, 0.93969, 0.90630,
                      0.86602, 0.81915, 0.76604, 0.70710,
                      0.64278, 0.57357, 0.50000, 0.42261,
                      0.34202, 0.25881, 0.17364, 0.08715, 
                      0.00000, -0.087155, -0.17364, -0.25881, 
                      -0.34202, -0.422618, -0.50000, -0.57357,
                       -0.64278, -0.707106, -0.76604, -0.81915,
                      -0.86602, -0.906307, -0.93969, -0.96592,
                      -0.98480, -0.996194, -1.00000, -0.99619,
                      -0.98480, -0.965925, -0.93969, -0.90630,
                      -0.86602, -0.819152, -0.76604, -0.70710,
                       -0.64278, -0.573576, -0.50000, -0.42261,
                      -0.34202, -0.258819, -0.17364, -0.08715,
                      };

void main(void)
{
   int j;
   
   WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
  
   DAC0_Initial();
     
   j=0;
  
   while(1)
   {
        DAC12_0DAT = (int)(2047*sine_value[j++])+ 2048;
        j = j%72;       
    }


}

void DAC0_Initial()
{
  ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
  DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Internal ref gain 1
  DAC12_0DAT =  0x0800; 
}

 

  • A nice starting point for those who want to do something similar.

    There are, however, several things to consider:

    1) FP arithmetics is slooooooow. Since you always multiply these values with 2047, cast the result to int and add 2048, why don't you just put the resulting INT values into the array? It's also much smaller to store 72 INTs rather than 72 floats.

    2) % operator is acutally a division and therefore slooow too. A nested for loop would be faster.

    3) the timing completely depends on MCLK speed and may be even influenced (halted) by any ISR. So while the utput will be a sine-shaped waveform, the frequency is uncertain and it may even be distorted as soon as the code grows more complex and implements ISRs.

  • Hi,

          I am trying to build an application similar to volume control buttons that we have in phone. I mean when we press a volume minus button we hear a tone , on pressing volume minus again we hear tone of same frequency with low volume and etc. Inverse with Volume plus. Is there any way , where I can have three look up tables for sine wave. where I can use Look up table 1 for min volume, look up table 2 for medium and look up 3 for max. Is there any way to generate this look up table? Thanks in advance,

    Regards,

    Sri.

  • sri-sri said:
    Is there any way to generate this look up table?

    I usually do this kind of job in Excel.

    Then I export it into a comma separated value file (just x numbers with a comma fter, except for the last) and then include it into the code using

    unsigned int table[]={
    #include "table.csv"
    };

  • Is it necessary to use lookup table only. You can use timer also for the same function.

    Timer concept will be better as compared to lookup table. But still if you need lookup table for this volume control then

    you can develop a small application in VB or C# and play with the lookup table to collect different waveforms along with their respective lookup table.

  • Nitin Gupta62774 said:
    Is it necessary to use lookup table only. You can use timer also for the same function.

    If you want to output a square wave signal, you only need a timer, right. You can do it with a timer only. It then could be filtered by a low-pass filter externally. Different frequencies aren't a problem too. Different volumes, however, aren't possible with plain timer output. It then would require software and DAC output. Then the time ris only controlling the moment when to change the value form 0 to x or back (x is scaled between 0 and 2047 for volume). However, the human ear is very sensitive to frequencies. The jitter caused by different reaction times to the tiemr interrupt might be audible.

    If you want to output a sine wave (or any othe rcomplex signal), a table and maybe timer-controlled DMA to the DAC is the fastest way. The MSP doesn't have the power to calculate the values in realtime.

  • Hi,

          Thank you very much for your valuable suggestions,

            

    Jens-Michael Gross said:
    Different volumes, however, aren't possible with plain timer output.

    Yes, I don't think this is doable. One option here is to vary the duty cycle of square wave with 1khz fixed frequency, but the problem in here is, you will find  tone are different even though the frequency is same. So I taught it is better to play a sine wave from any of the look up table with 1Khz frequency and different volume levels.

    TO do this, I have generated a sine wave tone in AUDACITY s/w with volume 0.3, 0.5 and 0.8. Saved as .wav file. Converted it in to Intel Hex and then Included themin the project as MIchael jens suggested. or you can load them directly to Flash of micro controller or can load in EEProm and read from I2C.

    The other way which is still more simpler is to modulate a 1 khz square wave with 62.5 khz square wave with Exor function. This results in a similar sine wave tone. if you increase the duty cycle of the 62.5 khz wave you will be able to generate same tone with different volume.

    Thanking you all again for your suggestions, hope this would be help full to some one who deals with similar issue.

    Regards,

    Sri.

**Attention** This is a public forum