首页 > 解决方案 > 如何用 8051 微控制器制作数字时钟

问题描述

我目前有一个为 8051 微控制器编写代码的项目,但是我不太确定如何使时序按预期变慢。我尝试了很多方法来调整计时器,但我不太确定如何降低确切的计时或如何设置 4 位七段显示器的第三位限制,以限制增加到第二位之前的 5增加 1。请问有没有人可以帮助我?

代码如图所示:

#include <c8051f200.h>
#include <setSystem.c>

//display ones/tens/hundreds/thousand at different 7-segment
void starting_display(unsigned int num1, unsigned int num2, unsigned int num3, unsigned int num4)
{
unsigned int x,z;
for(z=0;z<1;z++)
{
    P2 = 0xE0|  num1;
    for (x=0;x<1000;x++);
    P2 = 0xD0 | num2;
    for (x=0;x<1000;x++);
    P2 = 0xB0 | num3;
    for (x=0;x<1000;x++);
    P2 = 0x70 | num4;
    for (x=0;x<1000;x++);
}
}

//Split number to ones/tens/hundreds/thousands
void display(unsigned int x)
{
unsigned int a,b,c,d;
a = x / 1000;
b = (x % 1000) / 100;
c = (x % 100) / 10;
d = (x % 10);
starting_display(a,b,c,d); //Function call to starting_display()
}

void main()
{
unsigned int x = 0, overflow_count;
setSystem();

P1 = 0x00;
TMOD=0x10; //Initialize and configure Timer
TR1=1; //Starts Timer 1
TF1=0; //Clear Timer 1 overflow flag
while(1)
{
    TH1=0x00; //Reload values into TH1
    TL1=0x00; //Reload values into TL1
    while (TF1==0) //Wait for overflow to occur
    {
        display(x); //Function call to split the number
    }

    TF1=0; //Clear overflow flag
    x++;
    if(overflow_count == 100)
    {
        x++;
        overflow_count = 0;
    }
    if (x>2359) //Reset the number
    x=0;
}
}

标签: c

解决方案


将变量拆分x为两个变量,并将它们命名minuteshours. 如果是时候增加minutes,请这样做。如果达到 60,则将其重置为 0 并递增hours。达到 24 时,将其重置为 0。

要“减慢时间”,您可以提高overflow_count. 它的宽度足以数到 65535,因为您选择了unsigned int它的类型。


推荐阅读