Need help to generate PWM sine wave using MSP430 launchpad

1,628 views
Skip to first unread message

bharadwaj

unread,
Dec 11, 2012, 4:32:16 AM12/11/12
to compute...@googlegroups.com
Dear all,
             I am planning to design a simple pure sine wave inverter using MSP40 launchpad I have no idea how to generate sine wave using PWM. I have followed some link like http://www.msp430launchpad.com/2011/06/simple-launchpad-dac.html this but its not much helpful in obtaining the sine table. I need to generate 50 Hz sine wave but need more pwm sampling points so that the sine wave is more accurate so that it requires a simple capacitor as a filter at the output end. Experts please help me. Thanks in advance.

With regards
Bharadwaaj RA

Bhargava Venkatesh

unread,
Dec 11, 2012, 5:01:50 AM12/11/12
to compute...@googlegroups.com
Hi! 

Could you please share the code that you are using to generate PWM from the launchpad? 

Thanks!



--
 
 

bharadwaj

unread,
Dec 11, 2012, 5:08:58 AM12/11/12
to compute...@googlegroups.com
Am not yet started coding i just gone through the code in the link http://www.msp430launchpad.com/2011/06/simple-launchpad-dac.html am still confused how to create the sine table. If I succeed building one then i can go for the next step.

--
 
 

RajeshKumar Rajendren

unread,
Dec 11, 2012, 5:45:22 AM12/11/12
to compute...@googlegroups.com
Hi Bharadwaj,

Plz refer below links regarding generation of Lookup table for Sin 50Hz wave ...



I Hope it will help u.

Regards,
RajeshKumar R

--
 
 

bharadwaj

unread,
Dec 11, 2012, 6:58:12 AM12/11/12
to compute...@googlegroups.com
Hi Rajesh Kumar,
       I have found this is useful if i have any doubts i will feel free to disturb you guys...
With regards
Bharadwaaj RA

--
 
 

bharadwaj

unread,
Dec 12, 2012, 1:24:19 AM12/12/12
to compute...@googlegroups.com
Hi guys,
            Am trying to generate 16KHz pwm first in the MSP430 launchpad. The code is as following i cant get my frequency stabilized almost 10Hz variation is there please help me out in fixing it

#include <msp430g2553.h>
void calib_clock(void);

int main(void)
{
 WDTCTL = WDTPW + WDTHOLD; // Stop WDT
 calib_clock();
 P1DIR |= BIT2; // P1.2 to output
 P1SEL |= BIT2; // P1.2 to TA0.1
 CCR0 = 125-1; // PWM Period 16KHz
 CCTL1 = OUTMOD_7; // CCR1 reset/set
 CCR1 = 62; // CCR1 PWM duty cycle 50% duty for 16KHz
 TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
 while(1);
 return 0;
}

void calib_clock(void)
{
        int i=0;
         BCSCTL1 = CALBC1_1MHZ;            // Set range
          for(i=0xFFFF;i>0;i--);                        //delay to stabilize
          DCOCTL = CALDCO_1MHZ;            // SMCLK = DCO = 1MHz
          for(i=0xFFFF;i>0;i--);                        //delay to stabilize
}



with regards

Bharadwaaj RA

Renjith Nair

unread,
Dec 12, 2012, 4:29:04 AM12/12/12
to compute...@googlegroups.com
I am trying to add some thing to this to explain how sine wave can be generated from PWM, not sure this is the right way.

- to generate sine wave from PWM you will have to use low pass filter and the input will be PWM output from controller and if you are using DAC to generate sine wave this is not required.

- The PWM output should be like duty cyle is 50% when 0 degree of sine wave and 100% (which is the peak value of sine wave) at 90 degree again bring down duty cycle to 50% at 180 degree of sine wave, for the -ve half cycle duty cycle has to be broght down below 50%, for the peak of negative half cycle  (sine 270)  duty cycle 0% and for 360 degree duty cycle is 50%.

- That is, you generate a pwm signal whose duty cycle will vary from 0 to 100 and then 100 to 0, and the time duration from 0-100-0 will be Time period and the inverse will be the frequency of your sine wave.

-thats not the end, you need a filter to generate sine wave out of this. May be an RC filter will do that, i am not sure. Why dont you go for DAC which will straight away give you sine wave.

Regards,
Renjith


--
 
 



--
Best Regards,
   Renjith

bharadwaj

unread,
Dec 12, 2012, 4:33:04 AM12/12/12
to compute...@googlegroups.com
Dear Renjith,
            Thanks for your reply. But now am having an issue in just running a normal PWM in a stable way. The code in the previous mail is expected to give 16KHz 50% duty cycle PWM. But there is a little problem somewhere which gives me a slightly varying frequency it varies +/- 10 Hz. Any way thanks for your reply.

With regards
Bharadwaaj RA

On Wed, Dec 12, 2012 at 2:59 PM, Renjith Nair <renj...@gmail.com> wrote:
.

Renjith Nair

unread,
Dec 12, 2012, 5:03:43 AM12/12/12
to compute...@googlegroups.com
I think you can still go ahead, 10 hz variation in 16khz carrier signal will not make any difference in the final 50Hz. That is what I feel. I am interested to know, how are you measuring the frequency?  is it a digital oscilloscope or CRO? or some other device ? Here the deviation is 0.06% I feel there might be some deviation in the result  while measuring with a device as well .

Regards,
Renjith



--
 
 



--
Best Regards,
   Renjith

bharadwaj

unread,
Dec 12, 2012, 5:09:48 AM12/12/12
to compute...@googlegroups.com
Dear Renjith,
Thank you for your reply. I am using a CRO which is working fine when i keep the probes on the calibration 2KHz oscillator built in to it. Am not worried with the deviation from the expected frequency but am worried about the oscillation in the output. I mean its not stable some times its 16.020Khz or 16.010 KHz and varies in between these values.

With regards
Bharadwaaj RA

--
 
 

RajeshKumar Rajendren

unread,
Dec 16, 2012, 9:00:58 AM12/16/12
to compute...@googlegroups.com
Hi Bharadwaaj,

Regarding Clock calibration ...

Plz google for slac485a.zip, Zip archive has some "slac485a\MSP430G2xx3 Code Examples\C\msp430g2xx3_dco_calib.c" Reference code.

SMCLK output is routed to P1.4, It helps to measure the stability of SMCLK.

And regarding PWM, some reference are available like ...

msp430g2xx3_ta_16.c
msp430g2xx3_ta_17.c
msp430g2xx3_ta_19.c
msp430g2xx3_ta_20.c

Let me know the luck.

Regards,
RajeshKumar R


On Wed, Dec 12, 2012 at 3:39 PM, bharadwaj <bhaar...@gmail.com> wrote:
Dear Renjith,
Thank you for your reply. I am using a CRO which is working fine when i keep the probes on the calibration 2KHz oscillator built in to it. Am not worried with the deviation from the expected frequency but am worried about the oscillation in the output. I mean its not stable some times its 16.020Khz or 16.010 KHz and varies in between these values.

With regards
Bharadwaaj RA


On Wed, Dec 12, 2012 at 3:33 PM, Renjith Nair <renj...@gmail.com> wrote:
I think you can still go ahead, 10 hz variation in 16khz carrier signal will not make any difference in the final 50Hz. That is what I feel. I am interested to know, how are you measuring the frequency?  is it a digital oscilloscope or CRO? or some other device ? Here the deviation is 0.06% I feel there might be some deviation in the result  while measuring with a device as well .

Regards,
Renjith


On Wed, Dec 12, 2012 at 3:03 PM, bharadwaj <bhaar...@gmail.com> wrote:
Dear Renjith,
            Thanks for your reply. But now am having an issue in just running a normal PWM in a stable way. The code in the previous mail is expected to give 16KHz 50% duty cycle PWM. But there is a little problem somewhere which gives me a slightly varying frequency it varies +/- 10 Hz. Any way thanks for your reply.

With regards
Bharadwaaj RA

On Wed, Dec 12, 2012 at 2:59 PM, Renjith Nair <renj...@gmail.com> wrote:
.

--
 
 



--
Best Regards,
   Renjith

--
 
 

--
 
 

bharadwaj

unread,
Dec 17, 2012, 1:10:37 AM12/17/12
to compute...@googlegroups.com
Dear Rajesh,

                   I was exactly using the same method used in that code to calibrate my clock but no luck with that. Regarding PWM am planning to run it by myself using timers because it gives more flexibility. I will share the code as soon as its ready.

With regards
Bharadwaaj RA
--
 
 

RajeshKumar Rajendren

unread,
Dec 17, 2012, 2:44:24 AM12/17/12
to compute...@googlegroups.com
Hi Bharadwaaj,

Do u mean ...

DCO -> SMCLK -> P1.4 ( Expected Clk Rate 1Mhz ).

Clk rate measured in Port P1.4 is not stable ?

Regards,
RajeshKumar R


--
 
 

bharadwaj

unread,
Dec 17, 2012, 2:45:24 AM12/17/12
to compute...@googlegroups.com
YEAH

Paritosh

unread,
Mar 20, 2013, 11:11:08 AM3/20/13
to compute...@googlegroups.com
Hi

Can you please share your final code. I am also using MSP430 launchpad and I need to generate a sine burst signal with 5 cycles. Is it difficult to generate a sine burst signal? Please help...

bharadwaj

unread,
Mar 20, 2013, 12:10:06 PM3/20/13
to compute...@googlegroups.com
Tomorrow  i will send you the code

--
You received this message because you are subscribed to the Google Groups "Computer Club of India" group.
To unsubscribe from this group and stop receiving emails from it, send an email to computerclubi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

bharadwaj

unread,
Mar 22, 2013, 2:21:22 AM3/22/13
to compute...@googlegroups.com

Hi,
   This is the working final code for sine wave generation using MSP430.

With regards
Bhardwaaj RA

PS: sorry for the delay
sine.c

Naresh

unread,
Jan 27, 2014, 9:35:57 PM1/27/14
to compute...@googlegroups.com
HI bhardwaaj,
                      i am using tms320f280270 micro contoller to generate sine wave..but i dnt know code of its.please tell me code of this,or related sites 
thanks&best regards,
Message has been deleted

Hari Krishna Malladi

unread,
Oct 3, 2016, 7:02:21 AM10/3/16
to compute...@googlegroups.com
Can you specify what output you are getting? We can help debug it from there.

Hari Krishna Malladi

On Sun, Oct 2, 2016 at 12:50 AM, Betül BAKAR <betulb...@gmail.com> wrote:
Hi,

Iam working on sine wave generator and compile your code but generated wave is not sine wave. Is it require filter? I connected capacitor at the output end but not generated purely again.Please help me.

Thanks in advance.

Betul

22 Mart 2013 Cuma 08:21:22 UTC+2 tarihinde bhaaradwaaj yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to computerclubin+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Anil Kumar Pugalia

unread,
Oct 16, 2016, 10:38:39 AM10/16/16
to compute...@googlegroups.com

Share your code.

Regards
Anil
Passion: http://sysplay.in (Playing with Systems)
Betül BAKAR wrote on Wednesday 05 October 2016 06:26 PM:
Thank you for your reply. My Output is only 3.5 V DC. I compile sine code which is share above. Output pin is P1.0 , right? I am looking result using Dewesoft.I don't understand what problem is. 
3 Ekim 2016 Pazartesi 14:02:21 UTC+3 tarihinde Hari Krishna Malladi yazdı:

Hari Krishna Malladi

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Computer Club of India" group.
To unsubscribe from this group and stop receiving emails from it, send an email to computerclubi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bharadwaj

unread,
Oct 16, 2016, 11:58:53 AM10/16/16
to compute...@googlegroups.com

Yes it requires a filter. Normal rc low pass filter will work.


On 02-Oct-2016 06:30, "Betül BAKAR" <betulb...@gmail.com> wrote:
Hi,

Iam working on sine wave generator and compile your code but generated wave is not sine wave. Is it require filter? I connected capacitor at the output end but not generated purely again.Please help me.

Thanks in advance.

Betul

22 Mart 2013 Cuma 08:21:22 UTC+2 tarihinde bhaaradwaaj yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to computerclubin+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bharadwaj

unread,
Oct 16, 2016, 12:05:00 PM10/16/16
to compute...@googlegroups.com

Also if u have a oscilloscope output images it will be helpful.

Anil Kumar Pugalia

unread,
Oct 17, 2016, 12:41:13 AM10/17/16
to compute...@googlegroups.com

But as the output is DC, i.e. 0 frequency, there is something fundamentally missing in the waveform generation itself.

Is Dewesoft like oscilloscope?

Regards
Anil
Passion: http://sysplay.in (Playing with Systems)
For more options, visit https://groups.google.com/d/optout.

bharadwaj

unread,
Oct 17, 2016, 12:46:56 AM10/17/16
to compute...@googlegroups.com
I guess you guys are trying out this code

Is Dewesoft like oscilloscope?
yes

#include  <msp430g2553.h>

volatile unsigned char counter = 0 ; // Current location in wave array

void calib_clock(void);

/*volatile unsigned int wave[]={6, 12, 18, 24, 29, 35, 41, 47, 53,
 59, 64, 70, 76, 81, 87, 93, 98, 104, 109,
 115, 120, 126, 131, 136, 141, 147, 152, 157, 162,
 167, 172, 176, 181, 186, 190, 195, 199, 204, 208,
 212, 216, 220, 224, 228, 232, 236, 239, 243, 246,
 249, 253, 256, 259, 262, 265, 267, 270, 272, 275,
 277, 279, 281, 283, 285, 287, 289, 290, 292, 293,
 294, 295, 296, 297, 298, 299, 299, 299, 300, 300,
 300, 300, 300, 299, 299, 299, 298, 297, 296, 295,
 294, 293, 292, 290, 289, 287, 285, 283, 281, 279,
 277, 275, 272, 270, 267, 265, 262, 259, 256, 253,
 249, 246, 243, 239, 236, 232, 228, 224, 220, 216,
 212, 208, 204, 199, 195, 190, 186, 181, 176, 172,
 167, 162, 157, 152, 147, 141, 136, 131, 126, 120,
 115, 109, 104, 98, 93, 87, 81, 76, 70, 64,
 59, 53, 47, 41, 35, 29, 24, 18, 12, 6};*/
volatile unsigned int wave[] = {24,47,70,93,         115,136,157,176,195,
212,228,243,256,267,   277,285,292,296,299,
300,299,296,292,285,   277,267,256,243,228,
212,195,176,157,136,   115,93,70,47,24};
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
calib_clock();
P1DIR |= BIT0; // P1.0 output
counter = 0; // Reset counter
// Initialize Timer
CCTL0 = CCIE; // CCR0 interrupt enabled
CCTL1 = CCIE; // CCR1 interrupt enabled
CCR0 = 641; // Set PWM period to 256 clock ticks
CCR1 = wave[counter+1]; // Set first duty cycle value
TACTL = TASSEL_2 + MC_1 + ID_2  + TACLR; // SMCLK, upmode, enable interrupt, clear TA1R
_BIS_SR(LPM0_bits + GIE);
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
if(counter<38)
{
P1OUT |= BIT0; // Set P1.0
}
CCR1 = wave[counter++]; // Set next duty cycle value

if (counter == 38) // If counter is at the end of the array
{
counter = 0; // Reset counter
}
}

// Timer_A2 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER1_A1_VECTOR
__interrupt void Timer_A1(void)
{
switch( TA0IV )
{
case 10:
P1OUT ^= 0x01;
break;
default: break;
}
}


void calib_clock(void)
{
int i=0;
 BCSCTL1 = CALBC1_8MHZ_;            // Set range
 for(i=0xFFFF;i>0;i--); //delay to stabilize
 DCOCTL = CALDCO_8MHZ_;            // SMCLK = DCO = 8MHz
 for(i=0xFFFF;i>0;i--); //delay to stabilize
}


Reply all
Reply to author
Forward
0 new messages