• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » MSP430™ Microcontrollers » MSP430 Ultra-Low Power 16-bit Microcontroller Forum » MSP430F5438A Sine Wave Generator Using Timer B
Share
MSP430™ Microcontrollers
  • Forum
  • Announcements
  • E2E Wiki
Options
  • Subscribe via RSS
MSP430 Resources
  • MSP430 Product Folder
  • MSP-EXP430G2 - MSP430 LaunchPad Value Line Development kit
  • MSP430 Getting Started Guide
  • MSP430 Microcontroller Projects
  • More Resources >
  • Forums

    MSP430F5438A Sine Wave Generator Using Timer B

    • Alexander Coffin
      Posted by Alexander Coffin
      on May 01 2012 18:07 PM
      Prodigy70 points

      Hey all,
      I have tried getting this code to work and filter it so it is a smooth sine wave using an active 2nd order Butterworth filter or a passive RC filter and I get the attached photo. My issues is that my math is not acting correctly. I have been able to get a constant 50Hz signal out but cannot get a smooth sine wave to exist. When I have been using a look-up table the frequency will not stay constant so right now the array I have set is only at one constant output value.

      Here is the math I have been using:

      Output Freq= Clock Frequency /(PWM clock ticks * size of array)

      PWM Frequency = (Clock frequency / PWM clock ticks)

      Using either of the filters I have been able to get this at a constant Frequency but unable to get a smooth wave out. Here is the code I have been using.

      /*
      * Simple DAC for the MSP430F5438A
      * Author: Alexander Coffin
      * Date:
      * Code set at 650 to allow a constant 50Hz output. Variation is done in PWM output values
      *
      ************************************************************************/

      #include <msp430f5438a.h>

      unsigned char counter; // Current location in wave array
      // Wave range is from 0-255. 255 will give an output around 1v pk-pk(all above 0)
      //100 gives 250mv pk-pk(split between both)
      //150:
      //200: 800mv pk-pk(half and half)
      unsigned char wave[4] =
      {
      100, 100, 100, 100
      };


      unsigned int i; // Used for 'for' loops.

      void main(void)
      {
      WDTCTL = WDTPW + WDTHOLD; // Stop WDT

      P4DIR |= BIT1; // P4.1 output
      counter = 0; // Reset counter

      // Initialize Timer
      TB0CCTL0 = CCIE; // CCR0 interrupt enabled
      TB0CCTL1 = CCIE; // CCR1 interrupt enabled
      TB0CCR0 = 650; // Set PWM period to 650 clock ticks
      TB0CCR1 = wave[counter]; // Set first duty cycle value
      TB0CTL = TBSSEL_1 + MC_1 + TBIE + TBCLR; // ACLK, upmode, enable interrupt, clear TB1R

      _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
      }

      /**
      * TimerB0 interrupt service routine
      **/
      #pragma vector=TIMER0_B0_VECTOR
      __interrupt void TIMER0_B0_ISR(void)
      {
      P4OUT |= BIT1; // Set P4.0

      TB0CCR1 = wave[counter]; // Set next duty cycle value
      counter += 1; // Add Offset to CCR0
      if ( counter == 4) // If counter is at the end of the array
      {
      counter = 0; // Reset counter
      }
      }

      /**
      * TimerA1 Interrupt Vector (TAIV) handler
      **/
      #pragma vector=TIMER0_B1_VECTOR
      __interrupt void TIMER0_B1_ISR(void)
      {
      switch( TBIV )
      {
      case 2: // CCR1 interrupt
      P4OUT &= ~BIT1; // Clear P4.0 to determine duty cycle.
      break;
      default:
      break;
      }
      }

      If this code can be configured to allow for a steady wave to be produced I need to know how a long with how I could possibly build in a DC step.

      Thanks!

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Jens-Michael Gross
      Posted by Jens-Michael Gross
      on May 02 2012 13:08 PM
      Guru139900 points

      I don't think the code can do what you want.

      PWM means you're '1' for some time and '0' for the rest of one PWM cycle.

      However, what you code does is to set the cycle time to 651(!) timer ticks. So the timer runs from 0 to 100, triggers TIMER0_B1_ISR which will invert the port pin, then counts to 650, triggers TIMER0_B0_ISR, which changes the TBCCR1 settign to (currently) the same as before. Then counts to 0, triggering TIMER0_B1_ISR again, but this time you don't handle the itnerrupt (it is case 14 or so, timer overflow, since you did set TBIE). When the timer continued to 100 again, and the output is toggled.

      However, if you do it this way, the port pin is only toggled with a duty cycle of 50% (toggled each time the timer reaches 100 on its way to 650). With 651*2 being the cycle time. Changing the value for TBCCR1 only shifts the offset to TBR==0 (the 'phase') but has no effect on the duty cycle and therefore not on the output signal.

      To change the duty cycle, you'll have to change the output on two points of a cycle. E.g. reset it on the B1_ISR and set it in the B0_ISR. Then TBCCR1 determines the amount of ticks the output is on during a full cycle of 651 ticks. However, the ISR latency will not allow for high precision and may also introduce quirks when you get near 0 or 100% duty cycle.
      Hint: read abotu the OUTMODE bits. The hardware PWM does this in hardware without any ISR. But you should change the DC then inside the B0_ISR to synchronize it.

      Anyway: PWM output is always rectangular. You can filter it to a sinus-like signal by a low pass with ~ 1/2 of the PWM frequency, or to a DC voltage (equivalent to the Duty cycle) with a low-pass with a very low cutoff frequency. But then you cannot change the output signal fast, as these changes will be low-passed too.

      If you want to 'form' the output wave by constantly changing the PWM DC, you'll need a PWM frequency much higher than the output frequency (at least factor 4, better more) and a low-pass filter with a cutoff frequency slightly above the desired output frequency.

      _____________________________________
      Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
      If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on May 02 2012 17:23 PM
      Mastermind6840 points

      Alexander Coffin
      unsigned char wave[4] =
      {
      100, 100, 100, 100
      };

       Hi Alexander, if you wish to generate a sinusoidal signal I think you need some sinusoidal shaped samples, more than 4 and not constant.

       

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Luis RC
      Posted by Luis RC
      on May 02 2012 17:30 PM
      Expert6010 points

      Hi Alexander,

       

      I'm attaching a couple of examples I tested some time ago.

      - One of them uses a 2 timers, one used to generate the PWM, and the other as a time base which triggers the DMA automatically to send the next sample, so there's no CPU intervention

      - The other one uses the same timer to generate the PWM and update the duty cycle on every period interrupt

       

      I'm also attaching a picture of the sine wave at 100Hz using an external RC filter. Note that you can achieve better resolutions and faster frequencies depending on the number of steps in the sine wave and the PWM frequency.

       

      I hope you find it useful.

      Regards,

      Luis R

       

      7115.msp430_test_sine_wave_pwm.c

      5444.msp430_test_sine_wave_pwm_sw.c

      msp430f5438a msp430f5xx TimerB
      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use