In C language, it is not easy to use an accurate delay program. In IAR, there is a function called __delay_cycles(), which is defined in the header file intrinsics.h. The function is to delay N instruction cycles. Based on this function, an accurate delay function can be implemented (but it cannot be 100% accurate).
Implementation method:
Create a delay.h header file:
#ifndef __IAR_DELAY_H
#define __IAR_DELAY_H
#include
#define XTAL 8 //Can be defined as the crystal frequency you use (unit: MHz)
#define delay_us(x) __delay_cycles ( (unsigned long)(x * XTAL) )
#define delay_ms(x) __delay_cycles ( (unsigned long)(x * XTAL*1000UL) )
#define delay_s(x) __delay_cycles ( (unsigned long) (x * XTAL*1000000UL) )
#endif
Note: __delay_cycles(x), x must be a constant or a constant expression. If it is a variable, a compilation error will occur!
Authentication method:
1. Set up the IAR compiler as follows:
After entering the software simulation by pressing Ctrl+D, you can call up the running time of the analysis function in the main menu View->Proifing.
Press the Activate button in the red circle in the picture
, and also turn on "Auto Refresh" on the far right. After running for a while with F5 (Debug->Go), press Debug->Break to interrupt the execution of the program, and the cycles of all functions can be listed.
2. Write a test function
Empty Function
void delay()
{
}
It is found that the instruction cycle used by this empty function is 4. I used IAR AVR 5.20 to test it.
Try different test values, test us, ms, s level delays, and then subtract 4 from the instruction cycle of delay() to get the instruction cycle executed by delay_us(), delay_ms(), delay_s()
void delay()
{
delay_us(100);
//delay_ms(100);
// delay_s(100);
}
The test found that the accuracy is relatively high and the error is less than 1us.
With this method, you don't need to adjust the delay program when changing the crystal oscillator!
Previous article:AVR Learning Notes ------ SPI Bus and AVR SPI
Next article:ICCAVR compilation error! E
Recommended ReadingLatest update time:2024-11-16 12:00
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Metronom Real-Time Operating System RTOS for AVR microcontrollers
- Learn C language for AVR microcontrollers easily (with video tutorial) (Yan Yu, Li Jia, Qin Wenhai)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
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
- [2022 Digi-Key Innovation Design Competition] Part 1: Unboxing the Smart Desktop Interactive Robot
- Discussion on STM32F103 power supply 3.3V routing in PCB design
- 【TI recommended course】#LED function introduction and LED driver design considerations#
- 12V power supply EMC design
- IPC-7525 Chinese Document
- 6μA Quiescent Current 1.8A Switching Current Boost Converter
- JTAG/SWD/ISP/SWIM details the four programming methods of MCU
- Op amp amplifier - input offset voltage
- [Zhongke Bluexun AB32VG1 RISC-V board "run into" RTT evaluation] 2: Button control LED flashing
- Research and Implementation of Serial Data Communication between C6000 and C2000 Series DSP