#include
and you can use it. This header file defines two levels of delay functions:
void
void
But don't be happy too soon, because there are conditions for using them correctly in your avr-gcc. I will explain it slowly below.
This parameter is related to the F_CPU value in the Makefile. The value of the F_CPU variable defined by the Makefile will be passed to the compiler. If you use AVR_studio 4.1X to edit and debug, use the embedded AVR-GCC for compilation, and let AVR_studio automatically generate Makefile for you, then you can go to:
Write down the value of your F_CPU. The value of F_CPU indicates the operating frequency of your AVR microcontroller. The unit is Hz, not MHZ. Don't make a mistake. For example, if it is 7.3728M,
will find a definition like this in the "delay.h" header file:
#ifndef F_CPU
# warning "F_CPU not defined for
# define F_CPU 1000000UL
#endif
This is to provide a default frequency value of 1MHz when you do not define the variable F_CPU (including null) or AVR_studio Frequency does not give a value. This will prevent the compiler from making mistakes when compiling.
Below are the entities of these two functions:
void _delay_us(double __us)
{
uint8_t __ticks;
double __tmp = ((F_CPU) / 3e6) * __us;
(__tmp < 1.0)
else if (__tmp > 255)
else
_delay_loop_1(__ticks);
}
void _delay_ms(double __ms)
{
uint16_t __ticks;
double __tmp = ((F_CPU) / 4e3) * __ms;
(__tmp < 1.0)
else if (__tmp > 65535)
else
_delay_loop_2(__ticks);
}
You will find that they both call the
are as follows:
void
{
__asm__ volatile (
);
}
At 1MHz:
At 8MHz:
............
F_CPU
and so on.
{
__asm__ volatile (
);
}
At 1MHz:
At 8MHz:
............
F_CPU
and so on.
Similarly, _delay_loop_2(0) and _delay_loop_2(65536) have the same delay!! The delays of these functions are all the longest delays.
These two functions are written in the inline assembly format of avr-gcc. I will not go into the specific syntax rules. You can refer to avr-libc. However, these two functions are very simple and easy to understand. One is byte decrement and the other is word decrement. If you look carefully at the above functions, you will find that there are the following conditions for using them correctly: 1.
the maximum value of __ms MAX_VALUE
Only with the above conditions can you correctly use the delay functions _delay_us () and _delay_ms (). For the third condition, why should we choose constants, and why should we turn on the optimization option for the second condition. This is to allow the compiler to calculate the delay value when compiling, rather than compiling it into the program and calculating it at runtime. In that case, it will increase the length of the code, and it will also make the delay time of your delay program longer or unpredictable. Timing errors will occur.
_delay_us(double __us)
{
}
_delay_ms(double __ms)
{
}
Previous article:Understanding of hardware stack and software stack in AVR
Next article:AVR bit operation record
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- [National Technology N32G457 Review] SPI Transceiver Review
- CH224K evaluation and solution application optimization
- VCC error in AD09
- Please teach me how to design an "ideal transformer"
- [Anxinke NB-IoT Development Board EC-01F-Kit] 6. Connecting to Tmall Genie
- Can this demo support calling the administrator?
- 【ESP32-Korvo Review】+ 04 Implementing TTS-Completed
- The base address of the register must be added with the offset address. What is the meaning of the offset address?
- TI DSP EMIF interface address bus problem
- MSP430 non-continuous IO parallel port output matrix keyboard