如何延迟组装AVR微控制器? [英] How to make a delay in assembly for avr microcontrollers?
问题描述
我在计算延迟时遇到问题.当我为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=1
,NOP=1
和BRNE=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
的值).因此,这种情况下的累积延迟为1000ms
或1
秒.
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
长一点,因为我没有考虑Delay2
和Delay1
指令的时间.影响很小,但是对于精确的1sec
延迟,必须对这些指令进行计数,并且必须调整dly1
,dly2
和dly3
的值以确保算法恰好是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屋!