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.
Previous article:MCU Program Crash and Runaway Troubleshooting Guide
Next article:430 Precision Delay Issue
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- go go go, make the LED of RSL10 start blinking (there is a little easter egg at the end of the article)
- AURIX Tri-core Controller Usage Summary (I)
- 【TI Wireless】+ CC2541 Remote Control
- Why does the ringing occur due to the incontinuous operation?
- Application of Mitsubishi AC servo spindle drive system in CNC machining center
- What LED electronic engineers must know (technical principles + chip knowledge + circuit encyclopedia)
- Module Power Structure Design Guide
- A week of highlights: 2018.7.2-7.8
- 【Help】ADC acquisition
- MSP430G2755 Main Bootloader UART Porting Guide