Jump to content
Sign in to follow this  

PWM from 2 Analog input in MSP430G2231 [asking for opinion]

Recommended Posts

I've been post the same topic before (http://43oh.com/forum/viewtopic.php?f=8&t=887) but unable to make it work using that algorithm. So, I try another way like this thanks to this blog (http://blog.suspended-chord.info/?c=17) :


#include "msp430g2231.h"

// Variable Definitions
volatile int voltage; // Voltage value from next measurement
volatile int current; // Current value from next measurement

void main (void){
   WDTCTL = WDTPW + WDTHOLD;			// kill watchdog timer

   BCSCTL1 = CALBC1_1MHZ;				// Set range
   DCOCTL = CALDCO_1MHZ;				// SMCLK = DCO = 1MHz  

  ADC10CTL0 = ADC10ON + ADC10SHT_0 + SREF_0; // ACD10 on, 4 clock cycles per sample, Use Vcc/Vss references
  ADC10AE0 |= BIT0 + BIT1 ;                   // P1.0 or P1.1 ADC option select 

   P1DIR |= BIT2;                            // P1.2 = output
   P1SEL |= BIT2;                            // P1.2 = TA1 output
   TACCTL1 = OUTMOD_7;                       // TACCR1 reset/set
   TACTL = TASSEL_2 + MC_1;                  // SMCLK, upmode
  TACCR0 = 1024-1;                          // PWM Period

voltage = 32;
current = 16;
TACCR1 = voltage*current*0.002;         // TACCR1 PWM Duty Cycle

   while (1){
voltage = analogRead(INCH_0);
current = analogRead(INCH_1);
TACCR1 = voltage*current*0.002;         // TACCR1 PWM Duty Cycle	

* Reads ADC 'chan' once using AVCC as the reference.
int analogRead(unsigned int pin) {
 ADC10CTL1 = ADC10SSEL_3 + pin;// Select SMCLK and pin
 ADC10CTL0 |= ENC + ADC10SC; // enable and start conversion
 while (1) {
   if ((ADC10CTL1 ^ ADC10BUSY) & ((ADC10CTL0 & ADC10IFG)==ADC10IFG)) { // ensure conversion is complete
     ADC10CTL0 &= ~(ADC10IFG +ENC); // disable conversion and clear flag
 return ADC10MEM;


I try to compile and debug it, and nothing goes wrong. Then, since I don't have an oscilloscope with me right now, I try to connect the PWM output (pin P1.2) to a LED. Then I vary the analog input (P1.0 and P1.1) and the LED brightness change. So, I guess it's working.


So, I just want to know what you guys think of it. Do I make it right? And I want to make it as efficient as possible (low power consumption), but apparently I don't really know how to use the interrupt. So, any suggestions and corrections are greatly appreciate.



Share this post

Link to post
Share on other sites

Wow, epic timing!

I was searching for a simple ADC example to change colors in a red-green led with a pot.

I found Rob's thread, the one with the pots and the lcd, but AFAIR it wasn't commented so I got lost =)


By the way, in PWM I very often see: (period) -1. Why?

While tinkering with it I got better results without the -1.

For example:


CCR0 = 1024;


instead of


CCR0 = 1024-1;

Share this post

Link to post
Share on other sites

Thanks Rob, but I've already seen that project.

I'll try it once i get some RGB leds.


For now this one helped me understand many things about ADC, also the modifications I made worked on 1st try :mrgreen:

Damned P1SEL2 :evil:


(And sorry for spamming your thread watergius :oops: )

Share this post

Link to post
Share on other sites

@SoulSLayer : it's okay..

and about your previous comment about CCR0, when you make the CCR0 = 1024, I guess you wouldn't be able to get duty cycle 100% since the CCR1 value is 1023 * (Vin/Vref)-> which means that the maximum value is 1023.. well, that's just my opinion.. :)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...