Note: __delay_cycles(x), x must be a constant or a constant expression. If it is a variable, the compiler will report an error!
Delay functions are often used when using microcontrollers, and 430 is no exception. Common forms are:
void delay(unsigned int ms)
{
unsigned int i,j;
for( i=0;i
for(j=0;j<1141;j++); //8MHz crystal oscillator
}
Copy code //The above program segment can be used in situations where the delay accuracy is not required.
However, in the IAR software 430 compiler, we can use its internal delay subroutine to achieve the high-precision software delay we want. The method is as follows:
(1): Copy the following code to your .C source file.
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
Copy code (2): Modify
the #define CPU_F ((double)8000000) statement. Change 8000000 to the main frequency of your current MSP430 CPU, that is, the MCLK of the CPU.
The unit is Hz. In this example, 8000000 means MCLK=8MHZ.
(3): Usage example
delay_us(1); //1 microsecond delay
delay_ms(1); //1 millisecond delay
delay_us(3.5); //3.5 microsecond
delay delay_ms(3.5); //3.5 millisecond delay
delay_ms(1000); //delay 1 second
reason:
__delay_cycles() is a compiler system "function" and will be replaced with the corresponding loop code when IAR is compiled.
Look at the picture, the instruction (clock) cycle delay of 100 is required, and after compilation, it is exactly 100 cycles.
The result is: (1+2)*33 + 1 = 100
Previous article:AVR microcontroller reads and writes CPLD
Next article:AVR microcontroller (ATMEGA16) matrix keyboard scanning program
Recommended ReadingLatest update time:2024-11-16 16:54
- Popular Resources
- Popular amplifiers
- Virtualization Technology Practice Guide - High-efficiency and low-cost solutions for small and medium-sized enterprises (Wang Chunhai)
- Intelligent computing systems (Chen Yunji, Li Ling, Li Wei, Guo Qi, Du Zidong)
- Arduino UNO R4 WiFi Engineering Code
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- KiCad Production File Generator
- [AT-START-F403A Evaluation] 3. QR code routine verification under Keil environment
- TI Cup 2019 National Undergraduate Electronic Design Competition Retest Notice (Retest List Has Been Announced):
- TMS320F280049C Study Notes 15 DAC
- What if the uploaded image is not used? See here
- [Xingkong Board Python Programming Learning Main Control Board] Flip Tomato Clock
- How to send more than 20 bytes of data at a time with blue_nrg
- I stayed up late to sort it out. Here is the servo information on the national competition list.
- How to make an overvoltage protection circuit?
- Detailed explanation of starting surge current