AVR's precise delay program

Publisher:hxcp18Latest update time:2018-05-18 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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!


Keywords:AVR Reference address:AVR's precise delay program

Previous article:AVR Learning Notes ------ SPI Bus and AVR SPI
Next article:ICCAVR compilation error! E(197): area 'bss' not large enough

Recommended ReadingLatest update time:2024-11-16 12:00

AVR bit operation record
To be honest, this is the first time I've seen it used this way, Mark.   #define BIT(x) (1   (x)) #define GET_PORT(x, bit) (PORT ## x)  #define GET_PIN( x, bit) (PIN ## x)    #define GET_BIT(x,bit) (bit) #define PORT_OUT0(portx_bit) ( GET_PORT(portx_bit) &= ~BIT(GET_BIT(portx_bit)) )  #define PORT_OUT1
[Microcontroller]
AVR_USART
USART data register UDR: When reading this register, it is actually the receive data buffer register (RXB). When writing to this register, it is actually the transmit data buffer register (TXB). USART Control and Status Register A: UCSRA (RXC, TXC, UDRE, FE, DOR, PE, U2X, MPCM) RXC: USART reception is complete. When t
[Microcontroller]
AVR_USART
The complexity of fuse settings for AVR download and programming
Why can't I download the program to AVR16? I used AVR Studio to download it, but the fuse bit SPIEN turned gray and I couldn't tick it. Asker adoption     You have set the fuse to external crystal oscillator. Find a signal generator to connect the 1MHZ 5V signal to any pin of the 16-pin crystal oscillator. Use a d
[Microcontroller]
The complexity of fuse settings for AVR download and programming
Design and implementation of solar energy water heater controller
Introduction: A fully automatic solar water heater controller is designed. The system uses low-power and high-performance RISC microcontroller AVR as the core of the control circuit to monitor water temperature and water level in real time, and can realize temperature and water level detection management, fault and re
[Microcontroller]
Design and implementation of solar energy water heater controller
How to unlock the AVR16 chip
The Changxue AVRM16 core board itself integrates a powerful chip self-unlocking function module. When the chip is locked and cannot be used normally due to incorrect fuse bit settings, the unlocking function on the Changxue AVR16 core board can be used to unlock the chip. (If the chip is not locked, you can skip thi
[Microcontroller]
How to unlock the AVR16 chip
AVR IO output digital tube scanning program
System functions Use AVR to scan four digital tubes, dynamic scanning, dynamic display, left scan, right scan, back and forth scan... hardware design AVR main control circuit schematic diagram  Digital tube dynamic scanning circuit schematic diagram software design The following part is copied from TXT and
[Microcontroller]
AVR IO output digital tube scanning program
AVR high voltage parallel programmer manufacturing method
I just happened to have a few locked M8s, and I guess even RESET was disabled, so these chips can no longer be restored through the download line. In order to reduce losses (the current online price of atmega8 is more than 20, and I am not sure it is a brand new original product), I searched on the Internet for a whil
[Microcontroller]
AVR high voltage parallel programmer manufacturing method
How to make your own 51, AVR microcontroller development board
Let's not talk about anything else first. Here are two pictures of the real thing. front     On the other hand   The original intention of doing this is that the 51 development board I bought can no longer download programs, so I want to make a minimum system for dow
[Microcontroller]
How to make your own 51, AVR microcontroller development board
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号