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.

MSP430G2553: Led brightness with PWM signal

Part Number: MSP430G2553


Hello everyone,

I want to control the led brightness with potentiometer. I take analog input from P1.3 pin, the arrange the duty cycle according to digital equivalent of measured analog data.

Our ADC10MEM has 10-bit so max decimal value of this register is 1023. Considering this information I set the period 1023 (TA0CCR0 = 1023). By this way, brightness will max level when I arrange the max voltage in pot.

However my code does not work. Can anyone help me where I am wrong ?

#include <msp430.h> 

int main(void)

{

WDTCTL = WDTPW | WDTHOLD;                 // stop watchdog timer

BCSCTL1 = CALBC1_1MHZ;                      // Set range

DCOCTL = CALDCO_1MHZ;                       // Set DCO step + modulation

// -- Port Setup

P1DIR |= BIT6;                               // Assign P1.6 as output

P1OUT &= ~BIT6;                              // Turn green led off

P1DIR |= BIT6;                               // Set P1.6 as output

P1SEL |= BIT6;                               // P1.6 selected Timer0_A Out1

// -- Timer0_A Setup

TA0CCR0  |= 1023;                            // PWM Period

TA0CCTL1 |= OUTMOD_7;                        // TA0CCR1 output mode = 7

TA0CCR1  |= 511;                             // TA0CCR1 PWM duty cycle (Initiliaze)

TA0CTL   |= TASSEL_2 + ID_2 + MC_1 + TACLR + TAIE ;     // SMCLK, Up Mode

// -- ADC Setup

ADC10CTL0 |= ADC10ON + ADC10IE;                     // ADC10 active and interrupt enable

ADC10CTL1 |= INCH_3 + ADC10DIV_7 + ADC10SSEL_3 ;    // ADC10CLK = SMCLK / 8

ADC10AE0 |= INCH_3;                                 // Channel 0 analog input enable

    ADC10CTL0 |= ENC + ADC10SC;                         // Start Conversion

    _BIS_SR(LPM0_bits +GIE);

}

#pragma vector = ADC10_VECTOR

    __interrupt void adc10_interrupt(void){

        TA0CCR1 = ADC10MEM;

        ADC10CTL0 |= ENC + ADC10SC;                   // Start Conversion Again

    }

#include <msp430.h> 


int main(void){ WDTCTL = WDTPW | WDTHOLD;                 // stop watchdog timer BCSCTL1 = CALBC1_1MHZ;                      // Set range DCOCTL = CALDCO_1MHZ;                       // Set DCO step + modulation

// -- Port Setup
P1DIR |= BIT6;                               // Assign P1.6 as output P1OUT &= ~BIT6;                              // Turn green led off

P1DIR |= BIT6;                               // Set P1.6 as output P1SEL |= BIT6;                               // P1.6 selected Timer0_A Out1

// -- Timer0_A Setup
TA0CCR0  |= 1023;                            // PWM Period TA0CCTL1 |= OUTMOD_7;                        // TA0CCR1 output mode = 7 TA0CCR1  |= 511;                             // TA0CCR1 PWM duty cycle (Initiliaze) TA0CTL   |= TASSEL_2 + ID_2 + MC_1 + TACLR + TAIE ;     // SMCLK, Up Mode // -- ADC Setup
ADC10CTL0 |= ADC10ON + ADC10IE;                     // ADC10 active and interrupt enable ADC10CTL1 |= INCH_3 + ADC10DIV_7 + ADC10SSEL_3 ;    // ADC10CLK = SMCLK / 8 ADC10AE0 |= INCH_3;                                 // Channel 0 analog input enable    ADC10CTL0 |= ENC + ADC10SC;                         // Start Conversion    _BIS_SR(LPM0_bits +GIE);
}
#pragma vector = ADC10_VECTOR    __interrupt void adc10_interrupt(void){
        TA0CCR1 = ADC10MEM;        ADC10CTL0 |= ENC + ADC10SC;                   // Start Conversion Again    }

  • Hello,

    Welcome to E2E. Whenever you post code, please use the Syntax Highlighter (icon shown below) to make it more readable. Other community members may be more inclined to help if that's done.

    user6366746 said:
    However my code does not work. Can anyone help me where I am wrong ?

    Can you provide more details about what's not working? Do the LEDs turn on? If so, are they bright? Dim? How do they respond? What's the max pot voltage?

    Regards,

    James

  • Are you using a Launchpad? If so, P1.3 is connected to a pushbutton. On some Launchpad revisions, this button has a pullup resistor that will interfere with your readings. Try moving your input to A4 (P1.4).

    --------

    > ADC10AE0 |= INCH_3;                                 // Channel 0 analog input enable

    ADC10AE0 is a bit vector, not a number. [Ref User Guide (SLAU144J) Sec 22.3.3] To enable A3, use instead:

    > ADC10AE0 |= (1 << 3);                                 // Channel A3 analog input enable

**Attention** This is a public forum