如何延迟组装AVR微控制器? [英] How to make a delay in assembly for avr microcontrollers?

查看:90
本文介绍了如何延迟组装AVR微控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算延迟时遇到问题.当我为atmega128微控制器使用1MHz时钟速度时,我想延迟1 sec.

I am having a problem in calculating delays. I want to make a delay for 1 sec when I am using 1MHz clock speed for my atmega128 microcontroller.

我使用proteus进行仿真,使用avr studio进行atmel微控制器的汇编编码.

I use proteus for simulation and avr studio for coding in assembly for atmel microcontroller.

例如,此代码用于8MHz时钟微控制器

For example this code is for 8MHz clock microcontroller

Delay_15mS:                 ; For CLK(CPU) = 8 MHz
    LDI     dly1,   120     ; One clock cycle;
Delay1:
    LDI     dly2,   250     ; One clock cycle
Delay2:
    DEC     dly2            ; One clock cycle
    NOP                     ; One clock cycle
    BRNE    Delay2          ; Two clock cycles for true 1 clock for false

    DEC     dly1            ; One clock Cycle
    BRNE    Delay1          ; Two clock cycles for true 1 clock for false
RET

您能教我如何计算此延迟所需的时间吗?所以我可以在1 MHz的频率下延迟1秒钟获得1

Can you teach me how to calculate the time this delay will take? So I could make 1 for 1 sec delay @ 1 MHz

谢谢

推荐答案

要计算延迟,您需要计算周期时间,然后计算达到所需延迟所需的周期数.

To calculate a delay, you need to calculate the cycle time and then count how may cycles you need to reach the wanted delay.

在您的情况下,1MHz时钟表示每秒1000000个周期.因此,1周期等于1/1000000秒或1us.要获得1秒的延迟,您需要1000000个周期1us个周期,因此这意味着您必须创建一个1000000个周期的算法.

In your case, 1MHz clock means 1000000 cycles per second. So 1 cycle equals 1/1000000 seconds or 1us. To get 1 second delay, you need 1000000 cycles of 1us, so it means that you have to create an algorithm of 1000000 cycles.

以您的示例为基础,1秒的延迟@ 1MHz时钟为:

Building on your example, a 1 sec delay @ 1MHz clock would be:

Delay_1sec:                 ; For CLK(CPU) = 1 MHz
    LDI     dly1,   8       ; One clock cycle;
Delay1:
    LDI     dly2,   125     ; One clock cycle
Delay2:
    LDI     dly3,   250     ; One clock cycle
Delay3:
    DEC     dly3            ; One clock cycle
    NOP                     ; One clock cycle
    BRNE    Delay3          ; Two clock cycles when jumping to Delay3, 1 clock when continuing to DEC

    DEC     dly2            ; One clock cycle
    BRNE    Delay2          ; Two clock cycles when jumping to Delay2, 1 clock when continuing to DEC

    DEC     dly1            ; One clock Cycle
    BRNE    Delay1          ; Two clock cycles when jumping to Delay1, 1 clock when continuing to RET
RET

在这种情况下,存在一个内部循环Delay3,该循环的长度为4个周期,因为当跳转到Delay3时,DEC=1NOP=1BRNE=2.因此,重复250次(dly3的值)的4个周期是1000个周期或1000us = 1ms.

In this case there is the internal loop Delay3 that is 4 cycles long because DEC=1, NOP=1 and BRNE=2 when jumping to Delay3. So, 4 cycles repeated 250 times (the value of dly3) are 1000 cycles or 1000us = 1ms.

然后循环Delay2重复Delay3 125次(dly2的值).因此,这种情况下的累积延迟为125ms.

Then the loop Delay2 repeats the Delay3 125 times (the value of dly2). So the accumulated delay in this case is 125ms.

最后,循环Delay1重复Delay2 8次(dly1的值).因此,这种情况下的累积延迟为1000ms1秒.

And finally, the loop Delay1 repeats the Delay2 8 times (the value of dly1). So the accumulated delay in this case is 1000ms or 1 second.

注意:该示例延迟实际上比1sec长一点,因为我没有考虑Delay2Delay1指令的时间.影响很小,但是对于精确的1sec延迟,必须对这些指令进行计数,并且必须调整dly1dly2dly3的值以确保算法恰好是1000000个周期长

NOTE: This example delay is actually a little bit longer than 1sec because I didn't consider the time of the instructions of Delay2 and Delay1. The influence is very small, but for a precise 1sec delay, these instructions must be counted and the values of dly1, dly2 and dly3 must be adjusted to guarantee that the algorithm is exactly 1000000 cycles long.

注2:使用此算法,微控制器在执行延迟时无法执行任何其他操作,因为您正在使用它来计数周期.如果您想在延迟时做其他事情,请查看定时器和中断微控制器.

NOTE2: With this algorithm the microcontroller can't do anything else while executing the delay because you are using it to count cycles. If you want to do other things while doing a delay, take a look at timers and interrupts of the microcontroller.

这篇关于如何延迟组装AVR微控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆