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.

Blinking LED SW Delay

Other Parts Discussed in Thread: MSP430FR4133, MSP430G2553

Hello,

I just bought MSP430FR4133 launchpad and downloaded CCSV6. I was bale to blink the LED using the example code. However, I am having trouble understanding how the SW delay is calculated. What formula do I need to use to calculate the delay so the LED blinks at a desired rate.

Thank you,

Sahar 

  • Which sw delay you are talking about? Please show code fragment
  • Below please see the example code for blinking the LED. I want to know, for example, if I want the LED to blink every 0.5 second exactly, how do I manipulate the line "i=10000 //SW delay". Is there a formula that helps me determine what I need to assign "i" to for my desired blinking frequency.


    #include <msp430.h>

    int main(void) {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
    // to activate previously configured port settings
    P1DIR |= 0x01; // Set P1.0 to output direction

    for(;;) {
    volatile unsigned int i; // volatile to prevent optimization

    P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR

    i = 10000; // SW Delay
    do i--;
    while(i != 0);
    }

    return 0;
    }
  • We do not even try to know exact timing of such generic while() or for() loop timing because it depends on compiler and it' s settings. Agressively optimizing compiler can even remove such dummy loop, giving no delay at all.

    If you care about timing - don' t use such loops. Instead use __delay_cycles(). In this case argument is count of CPU cycles.  For instance at 1MHz CPU clock (MCLK) __delay_cycles(500000) will be 0.5 sec delay.

  • But keep in mind that those delay cycles aren't the way to go in further programs where you also want the program to do other things than blinking a LED - this software loop causes your program to stuck for the time it is counting. OK for the very first steps, but also a good point to have a look at the inlcuded timer modules of your processor. The timer modules let you generate precise time intervals.
  • Thank you very much Ilmar. It worked.
    regarding the CPU clock, the launchpad user guide mentions that the default MCLK is set to 2MhZ. I did some research and found out that the MCLK clock frequency can be changed to 8Mhz by using:
    BCSCTL1 = CALBC1_8MHZ;
    DCOCTL = CALDCO_8MHZ;

    This code was unidentifiable for MSP430FR4133.
    Do you have any advice on how I need to find the correct way to change this specific microcontroller's clock frequency?
  • Sahar,

    the statements

    BCSCTL1 = CALBC1_8MHZ;
    DCOCTL  = CALDCO_8MHZ;

    are not valid for your type of processor. They would be possible for a MSP430G2553, just to mention one example of the F2x series. Your MSP430FR4133 is different. Look at the user's guide for understanding the clock system of the processor. It is described in chapter 3. The code examples show how to change the frequency to 8MHz - this is the example msp430fr413x_CS_01.c:

    //******************************************************************************
    //  MSP430FR413x Demo - Configure MCLK for 8MHz sourced from DCO.
    //
    //  Description: Default DCODIV is MCLK and SMCLK source.
    //  By default, FR413x select XT1 as FLL reference.
    //  If XT1 is present, the XIN and XOUT pin needs to configure.
    //  If XT1 is absent, switch to select REFO as FLL reference automatically.
    //  XT1 is considered to be absent in this example.
    //  f(DCOCLK) = 2^FLLD * (FLLN+1) * (fFLLREFCLK / n).
    //  FLLD = 0, FLLN =243, n=1, DIVM =1, f(DCOCLK) = 2^0 * (243+1)*32768Hz = 8MHz,
    //  f(DCODIV) = (243+1)*32768Hz = 8MHz,
    //  ACLK = default REFO ~32768Hz, SMCLK = MCLK = f(DCODIV) = 8MHz.
    //  Toggle LED to indicate that the program is running.
    //
    //           MSP430FR4133
    //         ---------------
    //     /|\|               |
    //      | |               |
    //      --|RST            |
    //        |          P1.0 |---> LED
    //        |          P1.4 |---> MCLK  = 8MHz
    //        |          P8.0 |---> SMCLK = 8MHz
    //        |          P8.1 |---> ACLK  = 32768Hz
    //
    //
    //   William Goh
    //   Texas Instruments Inc.
    //   March 2014
    //   Built with IAR Embedded Workbench v6.10 & Code Composer Studio v6.0
    //******************************************************************************
    #include <msp430.h>
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;               // Stop watchdog timer
    
        __bis_SR_register(SCG0);                // disable FLL
        CSCTL3 |= SELREF__REFOCLK;              // Set REFO as FLL reference source
        CSCTL0 = 0;                             // clear DCO and MOD registers
        CSCTL1 &= ~(DCORSEL_7);                 // Clear DCO frequency select bits first
        CSCTL1 |= DCORSEL_3;                    // Set DCO = 8MHz
        CSCTL2 = FLLD_0 + 243;                  // DCODIV = 8MHz
        __delay_cycles(3);
        __bic_SR_register(SCG0);                // enable FLL
        while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)); // Poll until FLL is locked
    
    
        CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
                                                // default DCODIV as MCLK and SMCLK source
    
        P1DIR |= BIT0 | BIT4;                   // set MCLK and LED pin as output
        P1SEL0 |= BIT4;                         // set MCLK pin as second function
        P8DIR |= BIT0 | BIT1;                   // set ACLK and SMCLK pin as output
        P8SEL0 |= BIT0 | BIT1;                  // set ACLK and SMCLK pin as second function
    
        PM5CTL0 &= ~LOCKLPM5;                   // Disable the GPIO power-on default high-impedance mode
                                                // to activate previously configured port settings
    
        while(1)
        {
            P1OUT ^= BIT0;                      // Toggle P1.0 using exclusive-OR
            __delay_cycles(10000000);           // Delay for 10000000*(1/MCLK)=1.25s
        }
    }
  • Dennis,
    Thank you very much. That was very helpful. Also, for having control over the duty cycle, is PWM the best way to go?
  • A PWM consits of a frequency and a duty cycle. When using the hardware controlled timer module PWM in OUTMOD_7 (reset / set), then the frequency setting determines when your PWM signal is set to high. This is controlled by the value written to the CCR0 register. The value in CCR1 (CCR2, 3, ...) sets the high time of your signal. That means, the PWM signal goes low again when counting to the value given in the register. The rest of the time, until the timer hits CCR0 again, your signal stays low. Normally you once set a fixed frequency via CCR0 and alter CCR1 for varying the duty-cycle. But of course you can also change the frequency during runtime.
  • But a PWM is not used for blinking a LED. For controlling the blinking interval you should use the timer in continuous- or up-mode, toggling the output pin in the ISR. PWM is used for brightness control.

**Attention** This is a public forum