初学51单片机写出的时钟设计:
下面是新手小白通过学习51单片机的前几章做出的一份时钟设计的代码,若有不足还望各位不吝赐教。
时钟设计:
思路:
1.首先对全部的数码管进行显示的代码编写。
2.再对计时方面代码的编写。
3.最终对按键方面的代码编写。
初期的设想便是在调整时间的过程中能够通过数码管的闪烁去确定调整的是时或分或秒,因为是初学单片机对许多的地方还不能够特别熟练应用于是便用发光二极管去帮助识别(此处只是希望各位大佬能够提供一种方法实现初步的设想)。
时钟的功能:能够实现基本的加、减和清零操作;
按键键盘的使用:通过s2按键的次数来判断是对哪个数值进行操作,若按下按键则会停止计时,按一次发光二极管的第一个便发亮…直到第四次按下则调整时间结束计时开始。通过s3对时间进行加;s4进行减;s5进行清零操作。
代码:
#include<reg52.h>
#define uint unsigned int
#define unchar unsigned char
uint num,i,j,miao,fen,shi,c,cishu,miaoshi,miaoge,fenshi,fenge,shishi,shige;
unchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;
void chushi(void); /*初始化*/
void xianshimiao(uint miaoshi,uint miaoge); /*显示最后两个数码管为秒*/
void xianshifen(uint fenshi,uint fenge); /*显示中间两个数码管为分*/
void xianshidelay(void);/*显示的延迟函数*/
void xianshishi(uint shishi,uint shige);/*显示最前两个数码管为时*/
void xiaodoudelay();/*消抖的延迟函数*/
void keycan(void);/*键盘操作函数*/
void main()
{
chushi();
while(1)
{
keycan();
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
}
void time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
}
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
}
}
void chushi()
{
miao=0;
fen=0;
shi=0;
cishu=0;
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}
void xianshimiao(uint miaoshi,uint miaoge)
{
dula=1;
P0=table[miaoshi];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
xianshidelay();
dula=1;
P0=table[miaoge];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
xianshidelay();
}
void xianshifen(uint fenshi,uint fenge)
{
dula=1;
P0=table[fenshi];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
xianshidelay();
dula=1;
P0=table[fenge];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
xianshidelay();
}
void xianshishi(uint shishi,uint shige)
{
dula=1;
P0=table[shishi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
xianshidelay();
dula=1;
P0=table[shige];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
xianshidelay();
}
void xiaodoudelay()
{
for(i=0;i<5;i++)
for(j=0;j<110;j++);
}
void xianshidelay()
{
for(i=0;i<5;i++)
for(j=0;j<10;j++);
}
void keycan()
{
if(key1==0)
{
if(key1==0)
{
cishu++;/*通过对按键次数的操作进行判断操作时分秒其中一个*/
if(cishu==1)
{
TR0=0;/*停止计时*/
P1=0xfe;/*对发光二极管操作*/
}
else if(cishu==2)
P1=0xfd;
else if(cishu==3)
P1=0xfb;
else if(cishu==4)
{
P1=0xff;
cishu=0;/*次数归零,便于下次操作*/
TR0=1;/*开始计时*/
}
while(!key1);
}
}
if(cishu!=0)
{
if(key2==0)
{
xiaodoudelay();
if(key2==0)
{
while(!key2);
if(cishu==1)
{
shi++;
if(shi==24)
shi=0;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}/*此处再利用显示函数对按键后的数字进行及时的显示改变,下面相同*/
else if(cishu==2)
{
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
else if(cishu==3)
{
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
}
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
}
}
if(key3==0)
{
xiaodoudelay();
if(key3==0)
{
while(!key3);
if(cishu==1)
{
if(shi==0)
shi=24;
shi--;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
else if(cishu==2)
{
if(fen==0)
fen=60;
fen--;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
else if(cishu==3)
{
if(miao==0)
miao=60;
miao--;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
}
}
if(key4==0)
{
xiaodoudelay();
if(key4==0)
{
while(!key4);
if(cishu==1)
{
shi=0;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
else if(cishu==2)
{
fen=0;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
else if(cishu==3)
{
miao=0;
miaoshi=miao/10;
miaoge=miao%10;
fenshi=fen/10;
fenge=fen%10;
shishi=shi/10;
shige=shi%10;
xianshimiao(miaoshi,miaoge);
xianshifen(fenshi,fenge);
xianshishi(shishi,shige);
}
}
}
}
}