Page 1 of 2

MSP430 2013 PWM Signal Generator

Posted: Wed Jan 24, 2007 9:46 am
by cszym001
Hi
I am trying to make an adjustable frequency adjustable duty cycle PWM generator with the msp 430 2013 microcontroller, however, I cant find any code to get me on my way. I understand that I could use a crystal 33MHz crystal connected to the msp 430 and wire two analog inputs off pots to the A/D converter to control the duty cycle/Frequency. However, programmatically I don't know how to go about it can anyone help?

Posted: Wed Jan 24, 2007 2:49 pm
by OldCow
You do not need a crystal unless you want the frequency to be very accurate. If you do need it, the frequency of the crystal has to be 16MHz or less. 33MHz will not work.

You cannot use ADC to generate PWM. You may use the Timer to do it. See TI code samples.
http://www-s.ti.com/sc/techzip/SLAC081.zip (for assembly code) or
http://www-s.ti.com/sc/techzip/SLAC080.zip (for C code)

Read the "Readme.txt" file first to find what the code sample files are for. Look for file names with"_ta_" (TimerA)

Posted: Tue Jan 30, 2007 10:03 am
by cszym001
Hi I have been making head way. I can now output a PWM signal however I cannot get it to the period I need. I want to go from 1000ms to 10ms. However if I put in the value that I calculated will give me a 1000ms period it comes up with an error any ideas?

Here is my current code:
#include <msp430x20x3.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x04; // P1.2 output
P1SEL |= 0x04; // P1.2 TA1/2 options
CCR0 = 1110502; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCR1 = CCR0/2; // CCR1 PWM duty cycle
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode

_BIS_SR(CPUOFF); // Enter LPM0
}

Posted: Tue Jan 30, 2007 10:37 am
by OldCow
CCR0 is a 16-bit register. The largest number it can hold is 65535 (0xFFFF). 1110502 is mathematically right, but to big for CCR0. You need to reduce the frequency the Timer counts.

Your TACTL setting is using SMCLK/1 to count. You could change TACTL to use SMCLK/8, and thus CCR0 needs to be 138813. But that is still too big.

It appears that you are using SMCLK= DCO/1 and DCO is running at about 1.11MHz. Am I correct? If so, you can change to SMCLK=DCO/4 or DCO/8. Thus CCR0 needs to be 34703 or 17352 respectively for 1000msec.

You can reduce the DCO frequency too. Many ways to kill a cat.

Posted: Tue Jan 30, 2007 2:02 pm
by cszym001
Old Cow
This may seem like a stupid question but how do I set SMCLOCK to DCO/4? In which word do I change which bit?

Posted: Tue Jan 30, 2007 6:28 pm
by OldCow
I think you probably did not touch BCSCTL2. The power-on-reset makes it =0x00. This sets (among other things) SMCLK to be DCO/1. If you change it to "BCSCTL2=0x04;", SMCLK will be DCO/4.

Read the chapter about "Basic Clock Module" in the Users Guide.

Posted: Wed Jan 31, 2007 10:44 am
by OldCow
Did you get what you want?

Just curious, why do you want such low frequency?

Most applications use PWM as a substitute to variable voltage DC, and want the frequency in kHz or higher. They use capacitors to “smooth outâ€

Posted: Wed Jan 31, 2007 11:35 am
by cszym001
Old Cow,
I am an EE at a large company and my application is not a typical PWM application, rather, it is for a R&D and I thought that a microcontroller may have some advantages in this design, so I am trying to familiarize my self with the msp430. And I appreciate, and will continue to acquire your help.

Thanks
Chris

Posted: Wed Jan 31, 2007 1:36 pm
by cszym001
Sweet! I read the users manual like you suggested and I noticed that my DCO clock wasnt on 1 MHz so I adjusted that as well as achieving the range I wanted all the way down to 200mHz that is perfect! Now I have to just accept two analog voltage signals to vary the duty cycle and period and I will be done. You have any recommendations? I am going to read the User guide more carefully before I make any attempts. lol

Thanks Chris

Copy of my code
//This program simply outputs a pulse width modulated signal
//with a minimum frequency of .204 Hz(65534 clock ticks) and a max of approx 400Hz(approx 31 clock ticks )<-- fast as I want to go


#include <msp430x20x3.h>

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

BCSCTL1 = 0x80;
BCSCTL2 |= 0x04;
P1DIR |= 0x04; // P1.2 output
P1SEL |= 0x04; // P1.2 TA1/2 options
CCR0 =32-1; // PWM Period --> needs to be adjustable
CCTL1 = OUTMOD_4; // CCR1 reset/set
CCR1 = CCR0/2; // CCR1 PWM duty cycle 50% -->needs to be adjustable
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode

_BIS_SR(CPUOFF); // Enter LPM0
}

PWM

Posted: Tue Aug 11, 2009 4:19 am
by weglobal
Hi I am designing a circuit for a MPPT controller for a Solar Panel, therefore I need a PWM to control a Fet which is a current control for the battery charger , in the input side I am measure Volts and Amps that comes from the PV throughout the ADC , based on a mathematic algorism it has to control the PWM frequency , any idea how to do that ?

Posted: Tue Aug 11, 2009 11:13 am
by OldCow
I do not know how to design the DC to DC converter for MPPT. But I think, in addition to what you said, you might need to measure the voltage of the battery, and you might need to adjust the duty cycle of PWM.

You need to consider the operating temperature range of MSP430. And you need to be careful about the electronic noise generated that might interfere. If you have the hardware design and correct algorithm to adjust the PWM, writing the code is very simple and I can help you if you want.

PWM for MPPT

Posted: Tue Aug 11, 2009 1:28 pm
by weglobal
Hi and Thanks OldCow
I am using a DC DC TPS61020 from TI which will b doing the dc dc conversion, right afte I will have a FET so that i were I want to controll with PWM , I do also have in the circut a voltage measure for the battery
temperature is not a issue
is there a code that you could think I could use ??
Thanks

Posted: Tue Aug 11, 2009 8:13 pm
by OldCow
Sorry. I do not know what you want the MSP430 to do. There are lots of existing code that do lots of different things. I can only suggest that you search for what you want.

Posted: Mon Jan 04, 2010 11:40 am
by weglobal
OldCow wrote:Sorry. I do not know what you want the MSP430 to do. There are lots of existing code that do lots of different things. I can only suggest that you search for what you want.
Hi , I need to create a PWM out put for a code similar to the above one , the only problem is the code runs something else so there is a IF command
for example
while((Solar <= 81) && (Battery >= 4855))
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start;
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit;

P1OUT &= ~0x08; // Turn LDO OFF
if ((Solar <= 81) && (Battery >= 495) && (i)) // If Solar is less than 0.6 Volts is Dark, so if the battery is more
// than 3 Volts and
{
P1OUT |= 0x04; // Turn LED ON
i = 1; // i = 1;
}
else
{
P1OUT &= ~0x04; // Turn LED OFF;
sun_light = 0; // sun_light = 0;

}


where says "P1OUT |= 0x04; // Turn LED ON", I want the output to be PWM steady

Re: MSP430 2013 PWM Signal Generator

Posted: Sat Apr 28, 2012 4:16 am
by lopez
hello,
I am trying to build a fan with MSP430 and I want the fan speed when the temperature increases augment (with PWM) .....pleas can some1 help me on the code .I want to increase the motor speed when the temperature increases and decrease when the temperature is decreased . thx..