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.

msp432 duty cycle blink led

Hi all, 

i have msp432 and i'm trying to understand the pwm method. Is there any example that uses this method to make led blinking? I don't understand some parts and i think that with an example very simple this will be solved. In particular i don't understand what pins can i use. Sorry if i ask a complete example, but i need one very simple

  • Hi Luca!

    PWM is more for dimming the LED and not for blinking. But of course a timer is the right module to use for both things. Code examples can be downloaded here.

    Dennis

  • then you're referring to the fact that the led has different intensities in the period determined by PWM?
    In the examples posted what i have to see?
  • Blinking is different from dimming. For varying the intensity of the LED you want to use a PWM. To make advantage of the hardware PWM you should use portpins that have a timer module as special function. For example TA0.1 at P2.4. For blinking, means switching the LED on and off, you will use a timer that toggles the LED after a certain amount of time has elapsed. Both can be combined, of course.

    In terms of the code examples you should have a look at the timer related programs.

    Dennis
  • oK :) the PWM can be used also for adc sampling? Also in this case can i use timer ISR that starts conversion or it is better use pwm? In my project i have an adc that starts and ends when TimerA0 counts determinated values (for example starts from 0 and ends to 500) where these values are in microseconds (timerA smclk = 1Mhz). Is it better use adc14sc inside timer routine or use pwm and adc14 triggered?
  • I don't see any relation between PWM and the ADC. What exactly do you refer to? Starting the ADC by the timer is common practice, but PWM? Maybe I simply do not get your intention...
  • I post my code so you can understand my problem.

    // ==========Configuration clock 24Mhz - Smclk 12Mhz==========
    CSKEY = 0x695A;                    // Unlock CS module for register access
    CSCTL0 = 0;                        // Reset tuning parameters
    CSCTL0 = DCORSEL_4;                // Set DCO to 24MHz
    CSCTL1 = CSCTL1 & ~(SELS_M | DIVS_M); // Clear existing registers
    CSCTL1 = CSCTL1 | (SELS_3 | DIVS_1); // For SMCLK = 12Mhz, select DCO as source then divide by 2
    CSKEY = 0;                         // Lock CS module from unintended accesses
    
    // ==========Configurazione ADC 14 bit (12Mhz)==========
    P6SEL1 |= BIT1
    P6SEL0 |= BIT1;    // p1.6 ad
    ADC14CTL0 &= ~ADC14ENC;  //Turn off Enable. With few exceptions, the ADC14 control bits can only be modified when ADC14ENC = 0
    ADC14CTL0 = ADC14ON | ADC14SHT0_3 | ADC14CONSEQ_2 | ADC14SSEL_4 | ADC14SHP | ADC14SHS_0;   // Turn on ADC14, set sampling time (SH = 32), repeat single channel (ssel = smclk), adc14sc start
    ADC14CTL1 = ADC14RES__14BIT;                      // Use sampling timer, 14-bit conversion results (16 clock cycles)
    ADC14MCTL0 = ADC14VRSEL_0 | ADC14INCH_14;         // A0 ADC input select; Vref=AVCC=3.3V
    ADC14IER0 |= ADC14IE0;
    ADC14CTL0 |= ADC14ENC;                          // ADC enable
    
    // I think total time is: (32 + 16)/12 = 4us < 5us
    
    // ==========Configurazione TimerA0==========
    NVIC_ISER0 = 1 << ((INT_TA0_0 - 16) & 31);  // Enable TA0_0 interrupt in NVIC module
    TA0CCTL0 &= ~CCIFG;                         // Interrupt disable
    TA0CCR0 = 5;                                // Overflow (step dell'interrupt)
    TA0CTL = TASSEL__SMCLK | MC__UP | ID_2;     // SMCLK, UP mode, division 4 (12 MHz / 4 = 3 MHz)
    TA0EX0 |= TAIDEX_2;                         // division 3 (3 MHz / 3 = 1 MHz)
    
    // ==========Timer A0 interrupt service routin==========
    void TimerA0_0IsrHandler(void) {
    TA0CCTL0 &= ~CCIFG;
    // I want adc sampling only in this period (0-1000). Every 5us i have time to have sample
    if(microsecondi > 0 && microsecondi <= 1000) {
        ADC14CTL0 |= ADC14SC; // Start conversion;
        values[index_N] = ADC14MEM0;
        index_N++;
    }
    microsecondi += 5;
    // There is a rtc that repeat this cycle (this works)
    if (microsecondi >= 10000){
        microsecondi = 0;
    }

    I tell you adc because some people suggest me to use PWM (TA0CCR0, TA0CCR1, OUTMOD_X) to have correct sampling at correct frequency. In my code i want assure that adc conversion is only from 0 to 1000 at rate of 1sample/5us for 1000us (200samples). I don't know if this method to use timer ISR and ADC is correct or it is better use PWM or trigger. 

    Thanks

  • From this discussion, this is what I've understood:

    You want to take 200 samples in 1mS. Don't you?

    In the above case, you can use a timer module to trigger ADC once every 5uS (Be sure to complete ADC reading in < 5uS). You don't have to use PWM.

    PWM, is generally used to control the brightness of LED. You can change "Duty Cycle" on a pin, for example, using PWM. So, if you want to dim an LED or control motor speed, you use PWM.

    I suggest you go through Timers chapter of this training series. I believe it'll help you deeply understand how timers work. If you've time, kindly complete training series before continuing.

  • Ok thanks for the timers chapter :). the problem is that i don't find simple examples to trigger ADC with a Timer. With my code i think that i am not sampling every 5us perfectly but 4us (32+16 / 12Mhz) so not 200samples/1ms but 250samples/1ms. If i want sample every 5us (or other) is it better trigger adc with timer?

  • Can you "Start ADC" (ADC14CTL0 |= ADC14SC) in timer ISR? and read the value after sampling is completed?

  • This is what i do, isn't it? ADC14CTL0 |= ADC14SC is int timer ISR and i assure that it makes the conversion only for 1ms every 5us. This is my doubt.
  • Did you measure the timer frequency? Are you sure that timer is being triggered every 5uS?

    P.S. : You need to wait for ADC to complete before taking the reading.
  • The timerA i'm sure of his frequency and yes, i read the value of adc (because a part of code not written consists in send the values...). My doubt is about the sampling rate, the sample&hold time that allows to have adc samples <= 5us.
  • Time required for ADC conversion to complete will depend on ADC Clock and ADC14SHT bits. 

    Time required will approximately be (16 + SHT)/(ADC Clk Freq.)

    Eg: If ADC Clk = 12MHz, SHT = 32, then, (16+32)/(12MHz) = 4uS.

    Can you try using faster ADC Clock?

  • Ok perfect, in fact this calculation is that i use. 

    Yes, i can. I could try using adc clock = 24Mhz or 48Mhz (from DCO and then giving to one source clock of adc14).

    I have a question: what change to give adc clock faster or slower but keeping the same Time required (16+sht)/(adc clock freq)?? and what change, in terms of quality, use Sht higher or lower?

    thanks,

    Luca

  • 1. Can you use MODOSC as clock source?

    As explained you in earlier discussion, "The sample-and-hold time determines how many cycles the ADC will spend on sampling the signal before it is converted. Smaller values lead to quicker but less dependable/accurate results."

    P.S.: The parameters in the example were matched with your parameters intentionally, Sir. :)

  • In my code posted before, i start from DCO = 24Mhz then i divide it to have SMCLK = 12Mhz and source ADC. Do you suggest me not using SMCLK for adc source frequency bud modosc (always starting from DCO....)?
  • Generally, ADC Clock will be sourced from MODOSC. However, it's perfectly fine to use SMCLK.

    It's recommended to complete this training on 432. It'd clear almost all of your doubts regarding 432

  • Ok thanks for help. I think that my code is correct and what i'm doing is right. One last question: if i want to sample at 2Mbps what adc clock frequency is suggested? 12Mhz will be 16 for conversion and remain 8 clock cycle for SHT. Is it better give higher frequency?
  • You're welcome. Yes, use high frequency clock.
  • Reading the user's guide i have noted that SHT depends from Capacitance also. Is it correct? i refer to pag. 668 paragraph 20.2.6.3

**Attention** This is a public forum