简体   繁体   English

Keil Proteus 中的数字时钟模拟

[英]Digital clock simulation in Keil Proteus

This C programme is supposed to simulate a digital clock.这个 C 程序应该模拟一个数字时钟。 There gonna be 3 7segment in the Proteus programme to show the hour, minute and second (like a digital clock) Proteus 程序中会有 3 个 7segment 来显示小时、分钟和秒(就像一个数字时钟)

#include<reg51.h>

void main()
{
int t, i, j, k, a, b, c, d, e;
e = 0;
P3 = 0x00;
P2 = 0x00;
P0 = 0x00;
while (1)
{
    P0 = 0x00;
    for (c = 0; c<3; c++)
    {
        for (d = 0; d<10; d++)
        {
            for (a = 0; a<6; a++)
            {
                for (b = 0; b<10; b++)
                {
                    for (t = 0; t<6; t++)
                    {
                        for (i = 0; i<10; i++)
                        {
                            for (k = 0; k<1000; k++)
                                for (j = 0; j<142; j++);
                            P3++;
                        }
                        P3 = P3 + 0x06;
                    }
                    P3 = 0x00;
                    P2++;
                }
                P2 = P2 + 0x06;
                }
                P2 = 0x00;
                P0++;
                if (P0 == 0x24){
                    P0 = 0x00;
                    e = 1;
                }
                if (e == 1)
                    break;
            }
            if (e == 1){
                e = 0;
                break;
            }
            P0 = P0 + 0x06;
        }
    }
}

Would it be OK if someone explains the code?如果有人解释代码就可以了吗? I don't understand the nested for loops at the beginning of the code?我不明白代码开头的嵌套for循环? Moreover, what are these increasing?此外,这些增加的是什么?

P2 = P2 + 0x06;

What is 0x06?什么是 0x06? finally, what is e supposed to do in this code?最后,在这段代码中, e应该做什么?

The following snippet has no side-effect以下代码段没有副作用

for (k = 0; k<1000; k++)
    for (j = 0; j<142; j++);

as a result it's probably used as a desperate man's delay and the innermost 3 loops can be converted to this因此它可能被用作一个绝望的人的延迟,最里面的 3 个循环可以转换为这个

for (i = 0; i<10; i++)
{
    delay(142000);
    P3++;
}
P3 = P3 + 0x06;

In practice the whole delay loops will be removed completely after optimization, so the real delay function from the library should be called instead实际上,优化后整个延迟循环将被完全删除,因此应调用库中的真正延迟函数

From what I can guess P3 is the BCD output for the units digit in 3 numbers.从我可以猜到的 P3 是 3 个数字的个位数字的 BCD 输出。 It's increased by 1 each 142000 cycles.每 142000 个周期增加 1。 Once it's been increased 10 times, ie an overflow has happen, 0x06 would be added to the sum to adjust the BCD addition (move the carry to the next digit) as I explained here .一旦它增加了 10 倍,即发生了溢出,0x06 将被添加到总和中以调整 BCD 加法(将进位移动到下一个数字),正如我在这里解释的那样

If you write the P3 after each cycle in hex it'll be easier to get 0x00 0x01 0x02 0x03 ... 0x08 0x09 0x10 0x11 ... 0x19 0x20 : after 10 cycles we'll get 0x0A + 0x06 = 0x10 .如果您在每个周期后以十六进制写入 P3,则更容易得到0x00 0x01 0x02 0x03 ... 0x08 0x09 0x10 0x11 ... 0x19 0x20 :10 个周期后我们将得到0x0A + 0x06 = 0x10

However if we continue to the next line但是如果我们继续下一行

P3 = 0x00;
P2++;

we can see that P3 is reset after 10 cycles, so it means only the low 4 bits of the least significant digit is stored in P3, the next digit will be stored in the low nibble of P2.我们可以看到 P3 在 10 个周期后被复位,所以这意味着 P3 中只存储了最低有效位的低 4 位,下一位将存储在 P2 的低半字节中。 Why?为什么? because the minute's tens digit ranges only from 0 to 5 (counted by t ).因为分钟的十位数字范围仅从 0 到 5(按t计数)。 The way P2 and P0 are increased and carried is also the same as P3. P2和P0的增加和携带方式也与P3相同。

Finally P0 is reset to 0 when it reaches 24, so P0 contains the whole hour最后 P0 在达到 24 时重置为 0,因此 P0 包含整个小时

if (P0 == 0x24){
    P0 = 0x00;
    e = 1;
}

Now you can easily guess what e is used for现在您可以轻松猜出e的用途了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM