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.

adc to pwm on msp430g2231

Hi everyone.

Can i controll the brightness of four different LED by using PWM according to changing values read from four different variable POT with the help of ADC?

I think it is impossible to do it with four different channel, at least how can i do it only one channel namely let the brightness of LED changes according to a variable POT.

  • mehmet celik said:
    Can i controll the brightness of four different LED by using PWM according to changing values read from four different variable POT with the help of ADC?


    Of the G series, the 2x53 devices are the only ones with two Timers and three CCR units each. Since one of the CCR units of each tiemr is required to set the PWM frequencies, you can do four PWM outputs with the 2x53 devices, but only one with all other G-series MSPs, as these only have one timer with two CCR units.

    However, you can do sort of a PWM in plain software for any number of signals you have free GPIO pins available. It will require tight coding and use a significant amount of CPU time, but it is possible.

    For a single PWM output, things are relatively simple. Use the ADC to obtain an input voltage, e.g. with a pot between VCC and GND. So you'll get a value of 0..1023 for 0..100%.

    Now write 1022 (!) to TACCR0 and let the timer run in up mode. Then program TACCTL1 to RESET/SET mode and configure the port pin of TA0.1 for module output.

    TACCR1 gets the value from the ADC.For any ADC value x from 1 to 1023 this will result in a duty cycle of x/1023. The only exception is x=0. In this case you'll have to switch the port pin off manually.

    But keep in mind that the human perception of brightness is not linear. So the optical impression of the resulting LED brightness won't be what you'd expect from the POT position.
    So for a better result, vreate a translation table that converts all possible 1024 ADC results into a value from 0 to 100. Write 99 to TACCR0 instead of 1022. And the 1..100 result (for 0, again you'll have to switch the port pin off manually) to TACCR1. THis allows for a fine control of the brightness according the human perception courve.

  • Thanks for your answer Michael.

    Now I'm a little confused. Because there should a timer both of operation of ADC and PWM. How can I settle the timer.

  • mehmet celik said:
    Because there should a timer both of operation of ADC and PWM. How can I settle the timer.

    It depends on your exact requirements.

    A timer has a cycle time which you can control eithe rby limiting the number of ticks per cycle (using CCR0 and tierm up mode) or by changing the speed of the timer ticks (the source clock speed).
    However, all periodical events always happen once per cycle. If you want a timer to trigger your ADC conversions as well as producing a PWM output, you need to find a common base. The timer conversion frequency then needs to be the PWM cycle frequency. Also, the trigger signal for the timer is produced by a CCR unit, which also produces the PWM output. So you need to use a constant trigger point, e.g. the raising edge of the PWM signal at begin of the cycle. However, a PWM DC of 0 would actually stop the ADC triggering then.

    Arternatively you can trigger an interrupt at tiemr overrun and manually start the conversions inside the ISR. THis will introduce some jitter ot the start of each conversion, but the average sampling frequency will still be correct.

    You need to determine your exact parameters. Required PWM frequency, required sampling frequency etc. However, it is possible that you won't find a combination that fits all of your requirements. Then you have the wrong processor and need one with more timers (or with timers with more CCR units)

**Attention** This is a public forum