#include
This header file defines two levels of delay functions:
; In 51, our delay functions are all written by ourselves
void _delay_us (double __us); // microsecond level
void _delay_ms (double __ms); // millisecond level
void _delay_ms (double __ms);
sp;
But don't be happy too soon, because there are conditions for using them correctly in your avr-gcc, which I will explain slowly below.
This parameter is related to the F_CPU value in the Makefile. The value of the F_CPU variable defined in the Makefile will be passed to the compiler. If you use AVR_studio 4.1X to edit and debug, use the built-in AVR-GCC to compile, and let AVR_studio automatically generate the Makefile for you, then you can:
, either in assembly or in C. Although there are models
Write down your F_CPU value. 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, then F_CPU = 7372800.
You will find a definition like this in the "delay.h" header file:
#ifndef F_CPU; In 51, our delay functions are all written by ourselves
# warning "F_CPU not defined for"
# define F_CPU 1000000UL // 1MHz
#endif
board, sometimes it's still a bit annoying. Haha. But in the application of av# 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 F_CPU variable (including null) or AVR_studio Frequency does not have a value, so that the compiler will not make mistakes when compiling.
sp;
Here are the bodies of these two functions:
void _delay_us(double __us) // microseconds
{; In 51, our delay functions are all written by ourselves
uint8_t __ticks;
double __tmp = ((F_CPU) / 3e6) * __us; // 3e6 is because the called _delay_loop_1() is three instructions
if (__tmp < 1.0) &nb
__ticks = 1;
else if (__tmp > 255)
__ticks = 0;; In 51, our delay functions are all written by ourselves
else
__ticks = (uint8_t)__tmp;
_delay_loop_1(__ticks);, whether in assembly or in C language. Although there is a module
}
{; In 51, our delay functions are all written by ourselves
uint8_t __ticks;
double __tmp = ((F_CPU) / 3e6) * __us;
if (__tmp < 1.0)
else if (__tmp > 255)
else
_delay_loop_1(__ticks);, whether in assembly or in C language. Although there is a module
}
void _delay_ms(double __ms)
{
uint16_t __ticks;
double __tmp = ((F_CPU) / 4e3) * __ms; // 4e3 is because the called _delay_loop_2() is four instructions
if (__tmp < 1.0)
else if (__tmp > 65535)
else; In 51, our delay functions are all written by ourselves
_delay_loop_2(__ticks);
}
You will find that they both call the two functions _delay_loop_1(); and _delay_loop_2();
The two functions are as follows:
void _delay_loop_1(uint8_t __count) board, sometimes it's a bit annoying. Haha. However, when using av
{
__asm__ volatile (
"1: dec %0" " ", whether in assembly or in C language. Although there is a module
"brne 1b"
: "=r" (__count)
: "0" (__count)sp;
);
}
{
__asm__ volatile (
);
}
void _delay_loop_2(uint16_t __count), both in assembly and in C. Although there is a modulo
{
__asm__ volatile (
"1: sbiw %0,1" " " &nb
"brne 1b"
: "=w" (__count)
: "0" (__count)sp;
);
}
{
__asm__ volatile (
);
}
board, sometimes it's still a bit annoying. Haha. But in the application of av
Both functions are written in avr-gcc inline assembly format. I won't 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:
sp;
For the 4th scope, here is an example:
sp;
Only when the above conditions are met can you use the delay functions _delay_us() and _delay_ms() correctly. For the third condition, why do we use constants? And for the second condition, why do we need to turn on the optimization option? 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, the length of the code will be increased, and the delay time of your delay program will be longer or unpredictable, resulting in timing errors.
I found that it took me more than two hours to write, which doesn't seem long. It seems easy to say, but it still takes some effort to write it down. I hope it helps you. Haha. Share happiness and make progress together.
Previous article:About the use of ADC of AVR microcontroller
Next article:SPWM Program for AVR Microcontroller
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
Guess you like
- AM5708, FFT routine problem with IPC
- Discussion on bitwise operation writing of Hetai MCU
- Learn about TMS320F28335 and understand the detailed explanation of DMA configuration
- 2019, what is your New Year's resolution?
- Autobot Badge
- [2022 Digi-Key Innovation Design Competition] Automatic Curtain Closer
- MSP430G2553 pinout for easy use
- GNU Tools for ARM Embedded Processors (arm-none-eabi-gcc) development environment setup and lighting
- I haven’t experienced 5G much yet, but is 6G commercialization not far away?
- Factors causing LCD screen flicker