Explanation and correction of MSP430 MCU __delay_cycles precise delay

Publisher:BlissfulCharmLatest update time:2018-05-04 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    Here, I will discuss the issue of using __delay_cycles delay in MSP430 microcontroller.

    IAR for MSP430 compiler provides a compiler built-in precise delay function (not a real

Function) to provide users with accurate delay, the function prototype is:

        __intrinsic void __delay_cycles(unsigned long __cycles);

This internal function implements a delay of __cycles CPU cycles, but I would like to state the setting of this parameter:

    __cycles requires us to pass the number of cycles that the CPU runs


    The common usage on the Internet is:

        #define CPU_CLOCK 8000000

        #define delay_us(us) __delay_cycles(CPU_CLOCK/1000000*(us))

        #define delay_ms(ms) __delay_cycles(CPU_CLOCK/1000*(ms))


    When the CPU main clock frequency is 8MHz, this is indeed no problem, but the following writing:

        #define CPU_CLOCK 8000000

This makes it easy for people to think that the parameters of different main frequency systems can be unified by modifying its value.

This is incorrect! For example, modify it to #define CPU_CLOCK 32768 to achieve a delay of 32KHz main frequency...


    Let's do the calculation:

        When the system main clock frequency CPU_CLOCK is 8MHz:

        Frequency f = 8MHz = 8,000,000Hz

        Machine cycle Tm = 1/f = 1/8MHz = 1/8us

    That is to say, the duration of a machine cycle (nop) is 1/8us, so the delay of 1us is 8*Tm, the same as above:

        #define delay_us(us) __delay_cycles(8*(us))

        #define delay_ms(ms) __delay_cycles(8000*(ms))


    According to the above macro definition method, we define CPU_CLOCK as 32768, then:

        Frequency f = 32KHz = 32,768Hz

        Machine cycle Tm = 1/f = 1/32768Hz ~= 30.5us

    As you can imagine, the shortest CPU instruction execution cycle is 30.5us. At this time, is it possible to delay it by 1us?

So, simply change the above definition to

        #define CPU_CLOCK 32768

is absolutely wrong.


    Similarly, some friends have achieved a delay of 0.5us, which is also true when f = 1MHz = 1000000Hz

It is unrealistic, in this case the machine cycle Tm = 1us. When f = 8Mhz, 4 machine cycles are 0.5us which is acceptable.


    Therefore, to avoid incorrect usage or incorrect understanding, it is better to define the macro as follows:

        #if CPU_CLOCK == 8000000

            #define delay_us(us) __delay_cycles(8*(us))

            #define delay_ms(ms) __delay_cycles(8000*(ms))

        #else

            #pragma error "CPU_CLOCK is defined implicitly!"

        #endif


in addition:

  __delay_cycles is not a real function, it is just provided for compiler inline expansion.

Variable parameters are not supported, and parameters can only be constants.


Keywords:MSP430 Reference address:Explanation and correction of MSP430 MCU __delay_cycles precise delay

Previous article:MCU Program Crash and Runaway Troubleshooting Guide
Next article:430 Precision Delay Issue

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号