Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

enstrum

New Coder
Hello, I am currently trying to control an RGB LED's color by using 3 potientiometers with an MSP430 (Turning one would control red, turning the second would control green). How can I code the Timer A module to use PWM on the three rgb pins? Thank you!

C:
//ADC
int VALUE; // Integer variable "VALUE" declared
int DELAY; // Integer variable "DELAY" declared
int Map;

/*  MAIN FUNCTION *****************************************************/
int main(void) {
    int32_t  longResult;
    uint32_t tmp0, tmp1;
    WDTCTL  = WDTPW | WDTHOLD;
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL  = CALDCO_1MHZ;

    init_Display();


    //init_CounterTimer();

    //RGB
    P1DIR = BIT4 + BIT5 + BIT1; //GREEN input
    P1OUT = BIT4 + BIT5 + BIT1;



    //ADC
    ADC10CTL1 = ADC10CTL1 | INCH_6;     // Pin P1.7 selected as input channel
    ADC10CTL0 = ADC10CTL0 | ADC10ON;    // ADC is turned on and the Sample
    ADC10CTL0 = ADC10CTL0 | ADC10SHT_2; // and Hold Time is extended long
                                            // enough to be able to see the flash

        while(1)       // This is a loop ... "while(TRUE)" keep looping
        {
           for(DELAY = 240; DELAY > 0; DELAY --); // The DELAY has the value of
                                                  // 240 and as long as DELAY is
                                                  // greater than 0 each time thru
                                                  // it gets decremented by 1.
                                                  // When 0 the program continues.
        ADC10CTL0 = ADC10CTL0 | ENC;              // ADC is enabled
        ADC10CTL0 = ADC10CTL0 | ADC10SC;          // ADC starts a converion
        for(DELAY = 240; DELAY > 0; DELAY --); // Same delay as above. Gives it
                                                  // time to place the "VALUE" in
                                                  // the holding register.
        VALUE = ADC10MEM;                         // A number between 0 and 1023 is
                                                  // placed in the holding register
                                                  // "ADC10MEM


        Map = ((VALUE-0)*(255-0)/(1023-0));      ///map value to 255



                                      // P1.1 output
          P1SEL |= BIT1;                            // P1.1 TA1/2 options
          CCR0 = 1000-1;                             // PWM Period
          CCTL1 = OUTMOD_7;                         // CCR1 reset/set
          CCR1 = Map;                               // CCR1 PWM duty cycle
          TACTL = TASSEL_2 + MC_1 + TAIE;                  // SMCLK, up mode, Timer_A interrupt enabled, Timer_A interrupt pending

          _BIS_SR(CPUOFF + GIE);                          // Enter LPM0 with interrupts




        }

return 0;

}
 
Hello
Sorry, this won't be exactly an answer to your question, but one to your problem:
Do you really need to rewrite all the code from scratch or use THIS µC?
Because if you use Arduino IDE with a compatible µC (for example a ESP32 which is really powerful and costs less than 4€ in China and less than 10€ anywhere else), you can easily do what you want with less than 15 lines of code.
Getting the "value" of a potentiometer connected to an analog input pin is just
C:
val=analogRead(PIN_NUMBER);
Setting the PWM value of a LED (with a resistor to adapt the voltage) is just (this is a code for a genuine Arduino, but for ESP32 it is not more complicated)
C:
analogWrite(PIN_NUMBER,value);
Check https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial and https://create.arduino.cc/projecthub/muhammad-aqib/arduino-rgb-led-tutorial-fc003e
If you need more info, don't hesitate
David
 

Buy us a coffee!

Back
Top Bottom