❶ 设计一个由51单片机控制的一到五秒的程序延时程序!再加个数码管显示延时的秒数。
#include <reg51.h> //调用51单片机的头文件
#define uchar unsigned char
#define uint unsigned int
unsigned char code LED[]=
{ //定义表格一定要使用code,这样会做到程序存储区中
0x3F, //"0"的字形表,0B00111111
0x06, //"1"的字形表,0B00000110
0x5B, //"2"的字形表,0B01011011
0x4F, //"3"的字形表,0B01001111
0x66, //"4"的字形表,0B01100110
0x6D, //"5"的字形表,0B01101101
0x7D, //"6"的字形表,桐族0B01111101
0x07, //"7"的字形表,0B00000111
0x7F, //"8"的激轮桐字形表,0B01111111
0x6F, //"9"的字形表,0B01101111
0x00,
};
uchar num;
uint fz1,fz2,xs1,xs2;
uint ms,fz,xs,kg;
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit key4=P2^3;
void delayms(uint xms) // 延时函数
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void sjcl() // 时钟函数
{
if(num==20)
{ num=0;
led5=0;
ms++;
if(ms>=60)
{
ms=0;
fz++;
}
if(fz>=60)
{
fz=0;
xs++;
}
if(xs>=12)
{
xs=0;
}
}
if(num==10)
{ led5=1;
}
}
void smgxs() // 显示函数
{
if(kg==0)
{
fz1=xs/明坦10;
fz2=xs%10;
xs1=fz/10;
xs2=fz%10;
}
else if(kg==1)
{
fz1=11;
fz2=11;
xs1=fz/10;
xs2=fz%10;
}
else if(kg==2)
{
fz1=xs/10;
fz2=xs%10;
xs1=11;
xs2=11;
}
led1=0;
P0=LED[fz1];
delayms(5);
led1=1;
led2=0;
P0=LED[fz2];
delayms(5);
led2=1;
led3=0;
P0=LED[xs1];
delayms(5);
led3=1;
led4=0;
P0=LED[xs2];
delayms(5);
led4=1;
}
void ajcl()
{
if(key1==0)
{
delayms(10);
if(key1==0)
{
TR0=0;
kg++;
while(!key1);
}
if(kg==3)
{
kg=0;
TR0=1;
}
}
if(kg!=0)
if(kg==1)
{
smgxs();
if(key2==0)
delayms(10);
if(key2==0)
{
fz=fz+1;
while(!key2);
smgxs();
}
if(key3==0)
delayms(10);
if(key3==0)
{
fz=fz-1;
while(!key3);
smgxs();
}
}
if(kg==2)
{
smgxs();
if(key2==0)
delayms(10);
if(key2==0)
{
xs=xs+1;
while(!key2);
smgxs();
}
if(key3==0)
delayms(10);
if(key3==0)
{
xs=xs-1;
while(!key3);
smgxs();
}
}
}
void main() // 主函数
{ TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
sjcl();
smgxs();
ajcl();
}
}
void T0time()interrupt 1 //中断函数
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
改一下就ok