我们学单片机,一般都会写数字时钟编程。它能帮助我们:
a. 能够系统性地总结掌握的知识,将单元模块知识有机的结合在一起。
b. 能够充分协调好硬件与软件之间的相互结合,合理设计硬件电路。
仿真中我写了简单的选位调时、分、秒,根据我选的时、分、秒。进行加减。
也可以在实物打板中,实现数字时钟。
这是小编做的简易51单片机板,而且这种10x10以下规格的打板是不用花费钱的。感兴趣的读者可以打板玩玩。想知道怎么打板的读者留言告诉小编,小编出一个打板教程。文章来源:https://www.toymoban.com/news/detail-510416.html
接下来小编的代码有需要的读者自取。 文章来源地址https://www.toymoban.com/news/detail-510416.html
#include<reg52.h>
sbit key1 = P3^0;//换位
sbit key2 = P3^1;//加
sbit key3 = P3^2;//减
sbit LED = P1^7;
unsigned char kz1,kz2,kz3,kz4,kz5,w,k,nz,h;
unsigned char ss = 2, sg = 3, fs = 5, fg = 9, ms = 5, mg = 5;
//char SMG[18] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E,0x00,0X02} ;//实物
char SMG[12] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00} ;//仿真
char are[] = {0Xfe,0Xfd,0Xfb,0Xf7,0Xef,0Xdf,0Xbf,0X7f};
void zhongduan();
void keykongzhi();
void naoshizhong();
void LedDriver();
void delay(unsigned int i)
{
while(i--);
}
void main()
{
zhongduan();
while(1)
{
naoshizhong();
keykongzhi();
LedDriver();
}
}
void naoshizhong()
{
P0 = SMG[ss];P2 = are[0]; delay(500); P2 = 0Xff;
P0 = SMG[sg];P2 = are[1]; delay(500); P2 = 0Xff;
P0 = SMG[10];P2 = are[2]; delay(500); P2 = 0Xff;
P0 = SMG[fs];P2 = are[3]; delay(500); P2 = 0Xff;
P0 = SMG[fg];P2 = are[4]; delay(500); P2 = 0Xff;
P0 = SMG[10];P2 = are[5]; delay(500); P2 = 0Xff;
P0 = SMG[ms];P2 = are[6]; delay(500); P2 = 0Xff;
P0 = SMG[mg];P2 = are[7]; delay(500); P2 = 0Xff;
}
void keykongzhi()
{
if(key1 == 0 && kz1 == 0)
{
kz1 = 1;
}
else if(key1 == 1 && kz1 == 1)
{
kz1 = 0;
w++;
w &= 0x03;
}
if(key2 == 0 && kz2 == 0)
{
kz2 = 1;
}
else if(key2 == 1 && kz2 == 1)
{
kz2 = 0;
switch(w)
{
case 1: mg = 0; ms = 0; break;
case 2: fg ++; break;
case 3: sg ++; break;
}
}
if(key3 == 0 && kz3 == 0)
{
kz3 = 1;
}
else if(key3 == 1 && kz3 == 1)
{
kz3 = 0;
switch(w)
{
case 1: mg = 0; ms = 0; break;
case 2: fg --; break;
case 3: sg --; break;
}
}
if(w == 1 && nz == 0) //时钟调秒
{
P0 = SMG[ms]+128;P2 = are[6]; delay(800); P2 = 0Xff;
P0 = SMG[mg]+128;P2 = are[7]; delay(800); P2 = 0Xff;
}
if(w == 2 && nz == 0) //时钟调分
{
P0 = SMG[fs]+128;P2 = are[3]; delay(800); P2 = 0Xff;
P0 = SMG[fg]+128;P2 = are[4]; delay(800); P2 = 0Xff;
}
if(w == 3 && nz == 0) //时钟调时
{
P0 = SMG[ss]+128;P2 = are[0]; delay(800); P2 = 0Xff;
P0 = SMG[sg]+128;P2 = are[1]; delay(800); P2 = 0Xff;
}
}
void zhongduan()
{
TMOD=0X01;
TH0=0X3C;//50ms
TL0=0XB0;
ET0=1;
TR0=1;
EA=1;
}
void LedDriver()
{
if (k >= 40)
{
k = 0;
mg ++;
}
if (mg >= 10)
{
mg = 0;
ms ++;
}
if (ms >= 6)
{
ms = 0;
fg ++;
}
if (fg >= 10)
{
fg = 0;
fs ++;
}
if (fs >= 6)
{
fs = 0;
sg ++;
}
if (sg >= 10)
{
sg = 0;
ss ++ ;
}
if (ss ==2 && sg == 4)
{
ss = 0;
sg = 0;
}
}
void time0() interrupt 1 using 0
{
TH0=0X3C;
TL0=0XB0;
k ++;
}
到了这里,关于51单片机数字时钟的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!