❶ 設計一個由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