The explanation and correction of the accurate delay of the __delay_cycles of MSP430 single-chip microcomputer

Source: Internet
Author: User

Here, let me discuss the problem of using __delay_cycles delay for MSP430 MCU.
The IAR for MSP430 compiler provides a compiler inline precision delay function (not a true
function) to provide the user with a precise delay, the function prototype is:
__intrinsic void __delay_cycles (unsigned long __cycles);
This intrinsic function implements the delay of __cycles CPU cycles, but for the setting of this parameter, I would like to state:
__cycles need us to pass the number of cycles that the CPU is running

The popular 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 master clock frequency is 8MHz, this does not matter, but this way:
#define Cpu_clock 8000000
It's easy to think that you can modify its value to achieve the unity of different frequency system parameters, in fact
This is not true! For example, change to # define Cpu_clock 32768 to achieve 32KHz frequency delay ...

Here's a look at the calculation:
When the system main clock frequency cpu_clock to 8MHz:
Frequency f = 8MHz = 8,000,000hz
Machine Cycle Tm = f/F = 1/8mhz = 1/8us
In other words, 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 macro definition method above, we define Cpu_clock as 32768, then:
Frequency f = 32KHz = 32,768hz
Machine Cycle Tm = f/f = 1/32768hz ~= 30.5us
It can be imagined that the minimum CPU instruction execution period is 30.5us, then, want to delay 1us, this is possible?
So, simply change the definition above to
#define Cpu_clock 32768
is absolutely wrong.

Also, some friends achieve a 0.5us delay, which is also true when f = 1MHz = 1000000Hz
is unrealistic, at this time the machine cycle TM = 1us. At f = 8Mhz, 4 machine cycles are 0.5us fair.

Therefore, to avoid the use of errors or incorrect understanding, it is most appropriate to define macros 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

Other than that:
__delay_cycles is not really a function, just provides a compiler inline unwind, and the function
Variable arguments are not supported, and their parameters can only be constants.

The explanation and correction of the accurate delay of the __delay_cycles of MSP430 single-chip microcomputer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.