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.

MSP430FR2355: Generating a sine wave with Smart Analog Combo (SAC)

Part Number: MSP430FR2355

Tool/software: Code Composer Studio

How could I generate sine wave with the help of MSP430FR2355.I am able to get triangular wave with the help of this code but I want to generate the sine wave with the help of the DAC.please suggest me regarding this issues

#include <msp430.h>

unsigned int DAC_data=0;

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watch dog timer

  P1SEL0 |= BIT1;                           // Select P1.1 as OA0O function
  P1SEL1 |= BIT1;                           // OA is used as buffer for DAC

  PM5CTL0 &= ~LOCKLPM5;                     // Disable the GPIO power-on default high-impedance mode
                                            // to activate previously configured port settings

  // Configure reference module
  PMMCTL0_H = PMMPW_H;                      // Unlock the PMM registers
  PMMCTL2 = INTREFEN | REFVSEL_2;           // Enable internal 2.5V reference
  while(!(PMMCTL2 & REFGENRDY));            // Poll till internal reference settles

  SAC0DAC = DACSREF_1 + DACLSEL_2 + DACIE;  // Select int Vref as DAC reference
  SAC0DAT = DAC_data;                       // Initial DAC data
  SAC0DAC |= DACEN;                         // Enable DAC

  SAC0OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
  SAC0OA |= OAPM;                            // Select low speed and low power mode
  SAC0PGA = MSEL_1;                          // Set OA as buffer mode
  SAC0OA |= SACEN + OAEN;                    // Enable SAC and OA

  // Use TB2.1 as DAC hardware trigger
  TB2CCR0 = 100-1;                           // PWM Period/2
  TB2CCTL1 = OUTMOD_6;                       // TBCCR1 toggle/set
  TB2CCR1 = 50;                              // TBCCR1 PWM duty cycle
  TB2CTL = TBSSEL__SMCLK | MC_1 | TBCLR;     // SMCLK, up mode, clear TBR

  __bis_SR_register(LPM3_bits + GIE);        // Enter LPM3, Enable Interrupt
}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = SAC0_SAC2_VECTOR
__interrupt void SAC0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(SAC0_SAC2_VECTOR))) SAC0_ISR (void)
#else
#error Compiler not supported!
#endif
{
  switch(__even_in_range(SAC0IV,SACIV_4))
  {
    case SACIV_0: break;
    case SACIV_2: break;
    case SACIV_4:
        DAC_data++;
        DAC_data &= 0xFFF;
        SAC0DAT = DAC_data;                 // DAC12 output positive ramp
        break;
    default: break;
  }
}

  • Hello Amit,

    In future posts, please use a more descriptive title than "CCS/MSP430FR2355: CCS/MSP430FR2355". We've noticed that nearly all your previous posts also use this title, and while you may be getting help in each post, it will be challenging for other community members who may have the same question to find your posts. I've changed this title and one other thread to be more descriptive.

    Regarding the sine wave generation, have you read through the MSP430FR2355 LaunchPad™ Development Kit (MSP-EXP430FR2355) User's Guide? I took a quick look and searched for "sine" and discovered that the Out-of-Box demo code already supports this. Plus, you can also find that source code linked in the User's Guide. That's a good place to start.

    Regards,

    James

  • While I am sure the example will help, you can just look at this section of the code:

    switch(__even_in_range(SAC0IV,SACIV_4))

      {
        case SACIV_0: break;
        case SACIV_2: break;
        case SACIV_4:
            DAC_data++;
            DAC_data &= 0xFFF;
            SAC0DAT = DAC_data;                 // DAC12 output positive ramp
            break;
        default: break;
      }
    And figure out that you need to change the value of DAC_data.
    I would create a lookup table and load it based on the value of DAC_data:
    SAC0DAT = lookup_table[DAC_data];
    You can exploit the symmetries of the sin function to keep the table a reasonable size.
  • Now I am able to generate Sine Wave with the help of this code with the frequency of about 200 Hz but I want to generate the Sine Wave which has frequency of MHz that means in the range of visible light.So how could we generate the Sine

    #include <msp430.h>
    
    //unsigned int DAC_data=0;
    unsigned  long int  counter;
    unsigned   long int wave[128]={0x800,0x864,0x8c9,0x92d,0x990,0x9f2,0xa53,0xab2,
                                   0xb10,0xb6c,0xbc5,0xc1d,0xc72,0xcc4,0xd13,0xd5f,
                                   0xda8,0xded,0xe2f,0xe6d,0xea7,0xedd,0xf0e,0xf3b,
                                   0xf64,0xf88,0xfa8,0xfc3,0xfd9,0xfea,0xff6,0xffe,
                                   0x1000,0xffe,0xff6,0xfea,0xfd9,0xfc3,0xfa8,0xf88,
                                   0xf64,0xf3b,0xf0e,0xedd,0xea7,0xe6d,0xe2f,0xded,
                                   0xda8,0xd5f,0xd13,0xcc4,0xc72,0xc1d,0xbc5,0xb6c,
                                   0xb10,0xab2,0xa53,0x9f2,0x990,0x92d,0x8c9,0x864,
                                   0x800,0x79c,0x737,0x6d3,0x670,0x60e,0x5ad,0x54e,
                                   0x4f0,0x494,0x43b,0x3e3,0x38e,0x33c,0x2ed,0x2a1,
                                   0x258,0x213,0x1d1,0x193,0x159,0x123,0xf2,0xc5,
                                   0x9c,0x78,0x58,0x3d,0x27,0x16,0xa,0x2,
                                   0x0,0x2,0xa,0x16,0x27,0x3d,0x58,0x78,
                                   0x9c,0xc5,0xf2,0x123,0x159,0x193,0x1d1,0x213,
                                   0x258,0x2a1,0x2ed,0x33c,0x38e,0x3e3,0x43b,0x494,
                                   0x4f0,0x54e,0x5ad,0x60e,0x670,0x6d3,0x737,0x79c,
    };
    
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop watch dog timer
    
      P1SEL0 |= BIT1;                           // Select P1.1 as OA0O function
      P1SEL1 |= BIT1;                           // OA is used as buffer for DAC
    
      PM5CTL0 &= ~LOCKLPM5;                     // Disable the GPIO power-on default high-impedance mode
                                                // to activate previously configured port settings
    
      // Configure reference module
      PMMCTL0_H = PMMPW_H;                      // Unlock the PMM registers
      PMMCTL2 = INTREFEN | REFVSEL_2;           // Enable internal 2.5V reference
      while(!(PMMCTL2 & REFGENRDY));            // Poll till internal reference settles
    
      SAC0DAC = DACSREF_1 + DACLSEL_2 + DACIE ;  // Select int Vref as DAC reference
      SAC0DAT = wave[counter];                  // Initial DAC data
      SAC0DAC |= DACEN;                         // Enable DAC
    
      SAC0OA = NMUXEN + PMUXEN + PSEL_1 + NSEL_1;//Select positive and negative pin input
      SAC0OA |= OAPM;                            // Select low speed and low power mode
      SAC0PGA = MSEL_1;                          // Set OA as buffer mode
      SAC0OA |= SACEN + OAEN;                    // Enable SAC and OA
    
      // Use TB2.1 as DAC hardware trigger
      TB2CCR0 = 1000-1;                           // PWM Period/2
      TB2CCTL1 = OUTMOD_6;                       // TBCCR1 toggle/set
      TB2CCR1 = 500;                              // TBCCR1 PWM duty cycle
      TB2CTL = TBSSEL__SMCLK | MC_1 | TBCLR;     // SMCLK, up mode, clear TBR
    
      __bis_SR_register(LPM3_bits + GIE);        // Enter LPM3, Enable Interrupt
    }
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector = SAC0_SAC2_VECTOR
    __interrupt void SAC0_ISR(void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(SAC0_SAC2_VECTOR))) SAC0_ISR (void)
    #else
    #error Compiler not supported!
    #endif
    {
      switch(__even_in_range(SAC0IV,SACIV_4))
      {
        case SACIV_0: break;
        case SACIV_2: break;
        case SACIV_4:
            //DAC_data++;
            //DAC_data &= 0xFFF;
            //SAC0DAT = DAC_data;   // DAC12 output positive ramp
            SAC0DAT=wave[counter];
            wave[counter]&=0xFFF;
            counter += 1;
            if ( counter == 128)         // If counter is at the end of the array
            {
                counter = 0;            // Reset counter
            }
    
    
            break;
        default: break;
      }
    }
    
    

    Wave in the frequency of that range with very precision?

  • Per Data Sheet (SLASEC4B) Table 5-26, the DAC settling time (code to code) is 5us, so 200ksps is about as fast as the DAC can go. Divide by 10 points/cycle for anything resembling fidelity, and that's about 20kHz max. You won't achieve MHz.

    Unsolicited: Visible light frequency is in the THz range.

    [Edit: To answer your next question: I was able to reach about 100ksps (enough for me at the time) using an interrupt-driven method similar to yours. The next (faster) step would be to not use interrupts, but instead poll the DACIFG bit in a spin loop in main(). (Keep the timer trigger, though.) At high rates, you shouldn't expect your program to do much else.]

**Attention** This is a public forum