Accurate software delay method in IAR FOR AVR

Publisher:脑洞飞翔Latest update time:2018-05-04 Source: eefocusKeywords:IAR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:IAR Reference address:Accurate software delay method in IAR FOR AVR

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

Building your own AVR RTOS (Part 7: Preemptive Kernel)
Chapter 7: Preemptive Kernel (with Delayed Service Only) Once everyone understands the task scheduling method of time-slice round-robin scheduling, the principle of preemptive kernel is already within reach. First think about where the preemptive kernel implements task scheduling. Yes, it can schedule tasks in the t
[Microcontroller]
AVR microcontroller analog comparator initialization configuration and description
The avr analog comparator compares the value of the positive AIN0 with the value of the negative AIN1.  When the voltage on the analog comparator is high, the output of the analog comparator ACO is set. The output of the comparator can be used to trigger the input capture function of timer/  counter  1. In addition,
[Microcontroller]
Features, advantages and functions of AVR microcontrollers
High reliability, powerful functionality, high speed, low power consumption and low price have always been important indicators for measuring the performance of microcontrollers, and are also necessary conditions for microcontrollers to occupy the market and survive. The early single-chip microcomputers adopted a sa
[Microcontroller]
Features, advantages and functions of AVR microcontrollers
HT48R50A LCD Display Sample Program
HT48R50A  LCD Display Sample Program ; ********************************************  ; Function :       LCD显示示例程序           *  ; MICroProcessor : HT48R50A-1/28SKDIP-A      *  ; Author:         www.picavr.com             *  ; Crystal :       4MHz                       *  ; ********************************************
[Microcontroller]
HT48R50A LCD Display Sample Program
Analysis of the reset function inside the AVR microcontroller
Reset of AVR microcontroller: watchdog reset, power-on reset, power-off reset. I see that everyone is not very clear about the reset function, especially the reset function inside the AVR microcontroller. Now I will briefly talk about it here, and I hope you can have a general understanding at home. There are four t
[Microcontroller]
Analysis of the reset function inside the AVR microcontroller
AVR microcontroller fuse setting problem
Beginners often don't understand fuses. AVR chips use fuses to set the clock, boot time, enable some functions, BOOT area settings, and of course the most troublesome bit for beginners. If you don't set it well, it will be very troublesome to lock the chip. If you want to minimize the power consumption of the MCU, you
[Microcontroller]
Design of intelligent information recorder based on AVR microcontroller
0 Introduction With the development of computer technology, the monitoring of the use of various instruments is becoming more and more intelligent. Aiming at the shortcomings of the time recorder on the market, such as high cost, poor scalability, small storage capacity, and complex programming, this paper desig
[Microcontroller]
Design of intelligent information recorder based on AVR microcontroller
What is R-type transformer insulation?
With the rapid development of science and technology, the application of electrical equipment such as motors and power transformers is becoming more and more extensive, but the reliability and service life of electronic transformers depend to a large extent on insulating materials. We also call insulating materials
[Embedded]
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号