c - 如何用 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;
}
}
解决方案
将变量拆分x
为两个变量,并将它们命名minutes
为hours
. 如果是时候增加minutes
,请这样做。如果达到 60,则将其重置为 0 并递增hours
。达到 24 时,将其重置为 0。
要“减慢时间”,您可以提高overflow_count
. 它的宽度足以数到 65535,因为您选择了unsigned int
它的类型。
推荐阅读
- reactjs - 平面列表 - 索引操作
- c - 如何在c中打印一个长十进制数?
- javascript - alert() 和 return 为我的变量提供不同的值
- sql - 如何通过 SQL 中的总历史计数获取每天的总订单数
- python - (Discord.py) 制作一个记录器来保存机器人所在的对话
- c# - 如何在另一个方法调用中设置方法调用以返回不同的结果
- html - Html 弹窗不会滚动
- azure - 多源到 Azure 数据工厂中的复制活动
- c - 为什么我不能在不定义额外变量的情况下使用这个 C 程序?
- android - Android Alpha 内部版本无法登录。我在哪里可以查看日志文件?