怎样延迟PIC ASM工作?我真的困惑 [英] How does Delay work in PIC ASM? I'm truly baffled

查看:217
本文介绍了怎样延迟PIC ASM工作?我真的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不理解的事。所有code我在网上看到莫名其妙的评论有它为什么这样做的随机数,很多次,但没有字面上解释为什么什么做什么或者做什么谁。我真的不知道。

I really don't understand it at all. All the code I see online somehow in the comments has random numbers of why it's doing that many cycles, but there is literally no explanation as to why what does what or where what does who. I really have no idea.

例如:

cblock  
Delay1  
Delay2  
Delay3  
endc  

Start:  

.  
.  
.  

Delay  
movlw .2  
movwf Delay1  ;&&&&
movlw .3  
movwf Delay2  
movlw .4       <------ Important note here. For some reason it loops back to the
movwf Delay3   <------ "&&&&" mark when it reads this piece of code. NO IDEA why

DelayLoop  
decfsz Delay1,f    <----- Explain what this does. Where does it get the time from?
goto   DelayLoop   <----- Thanks
decfsz Delay2,f    <-----
goto   DelayLoop  

end

任何帮助将是非常美妙的。

Any help would be fantastic.

推荐答案

我真不明白它。所有code我在网上看到莫名其妙的评论有它为什么这样做,许多随机数周期,但字面上没有解释为什么什么做什么或者做什么谁,我真的不知道。

"I really don't understand it at all. All the code I see online somehow in the comments has random numbers of why it's doing that many cycles, but there is literally no explanation as to why what does what or where what does who. I really have no idea."

我明白,但我喜欢,不知道你在哪里得到精确定时从你怎么计算的呢?在哪里,这些数字从何而来。如果我设置VAR1到15,会发生什么情况,什么样的变化。如果我设置三个延迟变量为4,16,12,在那里它的定时来自所发生的那些编号,以使循环是在一定的时间谢谢 - ?吉米佩奇

"I understand that, but I like, don't know where you get EXACT timing from. How do you calculate it? Where do these numbers come from. If I set VAR1 to 15, what happens, what changes. If I set the three delay variables to 4, 16, 12, where does the the timing come from? What happens to those numbers to cause the loop to be a certain time? Thanks – Jimmy Page"

如果我理解你的问题,然后:

If I understand your questions then:

该16F690数据表说:

The 16F690 data sheet says:

一个指令周期由4 MHz的振荡器频率个振荡周期,这为1μs正常指令执行时间。所有指令都在一个指令周期内执行,除非条件测试为真,或者改变程序计数器作为指令的结果。如果发生这种情况,执行指令需要两个指令周期,以作为NOP执行的第二个周期。

One instruction cycle consists of four oscillator periods for an oscillator frequency of 4 MHz, this gives a normal instruction execution time of 1 μs. All instructions are executed within a single instruction cycle, unless a conditional test is true, or the program counter is changed as a result of an instruction. When this occurs, the execution takes two instruction cycles, with the second cycle executed as a NOP.

因此​​,可以说延迟1的值是3,我们有这样的循环:

So lets say Delay1 has the value 3 and we have this loop:


Top
decfsz Delay1,f
goto Top

每个我们执行DECFSZ时候我们得到的股票的一个周期。如果F是零,我们必须跳过那么它就变成一个两周期指令。每当我们执行转到PC变为所以它是一个2周期的指令。所以,如果我们走循环,我显示此使用格式

Each time we execute decfsz we get the stock one cycle. IF f is zero and we have to skip then it becomes a two cycle instruction. Each time we execute the goto the pc changes so it is a 2 cycle instruction. So if we walk the loop and I show this using the format

在用来执行指令,指令周期延迟1值,总周期

Delay1 value before instruction, instruction, cycles used to execute, total cycles


3,decfsz,1,1
2,goto,2,3
2,decfsz,1,4
1,goto,2,6
1,decfsz,2,8

所以这个循环本身与延迟1起3花了8个周期或8US如果我们在4MHz运行。计算循环,再调整为部分的时钟速率,即在1MHz运行时,在2MHz的或32us运行时相同code可以是16us是重要的。

So that loop by itself with Delay1 starting at 3 took 8 cycles or 8us if we were running at 4mhz. It is important to compute cycles first, then adjust for the clock rate of the part, that same code could be 16us when running at 2mhz or 32us when running at 1mhz.

因此​​,通过观察,我们可以看到,对于不属于一个F值DECFSZ +转到对2 + 1 = 3个周期。一次性我们击中DECFSZ为1的值将是2个周期。因此,与第3延迟1开始会有2个非酮条目值(3,2)。并增加2个周期,我们打DECFSZ最后一次和跳过,总周期((3-1)* 3)+ 2 = 8。

So by inspection we can see that for f values that are not one the decfsz+goto pair are 2+1=3 cycles. The one time we hit decfsz with a value of 1 it will be 2 cycles. So starting with Delay1 of 3 there will be 2 non-one entry values (3,2). And add 2 cycles for the last time we hit decfsz and skip, total cycles ((3-1)*3)+2=8.

如果我们已经进入此循环与延迟1设定为11,这将是((11-1)* 3)+ 2 = 32个周期,一个7将是20个周期。

If we had entered this loop with Delay1 set to 11, it would be ((11-1)*3)+2=32 cycles, a 7 would be 20 cycles.

如果你走得更远,环绕另一个你继续繁衍执行周期的头号DECFSZ循环。如果延迟1是3进入和延迟2是2

If you go further and wrap one decfsz loop around another you continue to multiply the number of executed cycles. If Delay1 is a 3 going in and Delay2 is a 2


Top
decfsz Delay1,f
goto Top
decfsz Delay2,f
goto Top

然后第一时间通过延迟1 DECFSZ,转到循环中,我们所知道的是8个周期。第一DECFSZ延迟2,女,因为延迟2不是1就是1个周期,我们有多达9个总。 goto的是2个,总共11个。假设在F的内存是8位,然后我们打的延迟1圈我们进入一个0第二次,把它作为数学给我们一个为0x100或256((256-1)的 3)+ 2 = 767以上的周期,778总为止。延迟2现在是1,所以这是最后DECFSZ延迟2,女让我们花费2,共780个周期。我们可以想出一个算法计算周期为到一个接近((Delay2-1)的(((256-1)* 3)+2))+(((延迟1)-1) * 3)+2)+((Delay2-1)* 3)+2个周期。

Then the first time through the Delay1 decfsz,goto loop we know is 8 cycles. The first decfsz Delay2,f because Delay2 is not a 1 is 1 cycle, we are up to 9 total. the goto is 2 more, 11 total. Assuming the f memory is 8 bit, then the second time we hit the Delay1 loop we enter with a 0, think of it as a 0x100 or 256 for the math giving us ((256-1)3)+2=767 more cycles, 778 total so far. Delay2 is now a 1, so this is the final decfsz Delay2,f so that costs us 2, a total of 780 cycles. And we could come up with an algorithm for computing the cycles for that Something close to ((Delay2-1)(((256-1)*3)+2))+(((Delay1)-1)*3)+2)+((Delay2-1)*3)+2 cycles.

虽然我的循环是一个循环DECFSZ比你小,如果你让我的像你,因为它与其他一些指令启动:

And although my loop is smaller by one decfsz loop than yours, if you make mine resemble yours in that it starts with some other instructions:


Entry
movlw .3
movwf Delay1
movl2 .2
movwf Delay2

Top
decfsz Delay1,f
goto Top
decfsz Delay2,f
goto Top

我们需要为这两个MOVLW的和两个MOVWF的加4个周期以上的整体方程的执行周期数。

We need to add 4 more cycles for the two movlw's and two movwf's to the overall equation for number of cycles to execute.

所以,从字面上看,还有就是为什么它被执行这么多周期的解释。这解释是数据表的设备

So, literally, there is an explanation why it is executing so many cycles. And that explanation was in the datasheet for the device.

让我们这个进一步。让我们得到patrickmdnet链接到生成780个周期的code发生器:

Lets take this further. Lets get the code generator that patrickmdnet linked to to generate 780 cycles:



; Delay = 780 instruction cycles
; Clock frequency = 4 MHz

; Actual delay = 0.00078 seconds = 780 cycles
; Error = 0 %

    cblock
    d1
    d2
    endc

            ;778 cycles
    movlw   0x9B
    movwf   d1
    movlw   0x01
    movwf   d2
Delay_0
    decfsz  d1, f
    goto    $+2
    decfsz  d2, f
    goto    Delay_0

            ;2 cycles
    goto    $+1

此循环架构有点不同。

This loop is architected a bit different.

我们开始装载在F寄存器的4个周期前,我们得到Delay_0

we start with 4 cycles of loading the f registers before we get to Delay_0

内循环DECFSZ goto语句不直接跳转到Delay_0在你的问题中code和我上面的解释,这一跳过DECFSZ D2,F。因此对于非酮穿过该环有1个周期为DECFSZ,2个周期为转到$ + 2和2个周期用于转到Delay_0,共5个循环为每个非酮D1。并添加两个更多的时间D1是1。这给了我们((0x9B-1)* 5)+ 2 = 772个周期这一点,我们有多达776个周期之前添加4个周期。

The inner decfsz loops goto does not branch up to Delay_0 directly as the code in your question and my explanation above, this one skips over decfsz d2,f. So for the non-one passes through that loop there is 1 cycle for the decfsz, 2 cycles for the goto $+2 and 2 cycles for the goto Delay_0, total 5 cycles for each non-one d1. And add two more for the time that d1 is a 1. this gives us ((0x9B-1)*5)+2 = 772 cycles add the 4 cycles before this we are up to 776 cycles.

有趣的是,去年DECFSZ D1,女击中DECFSZ D2,女同D2组为0x01,这意味着它是保证跳过。这是2个周期,一个goto $ + 2与另一转到$ + 1,而不是MOVLW / MOVWF加载D2会做同样的事情。无论如何2个周期的这个单指令给我们带来了长达总共778个循环

Interestingly that last decfsz d1,f hits a decfsz d2,f with d2 set to 0x01 which means it is guaranteed to skip. it is 2 cycles, a goto $+2 with another goto $+1 instead of the movlw/movwf to load d2 would have done the same thing. Anyway this single instruction of 2 cycles brings us up to a total of 778 cycles

我们需要两个周期去780和一个与后藤$ + 1在这里完成,goto方法修改PC所以他们总是2个周期。我们有780次,我问程序来生成。

We need two more cycles to get to 780 and that is done here with a goto $+1, gotos modify the pc so they are always 2 cycles. We have the 780 cycles that I asked the program to generate.

这篇关于怎样延迟PIC ASM工作?我真的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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