51单片机定时器秒表设计程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

51单片机定时器秒表设计程序#include

typedef unsigned char UINT8;

typedef unsigned int UINT16;

code UINT8 SEGMENT[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

code UINT8 SHU[10] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

code UINT8 SELECT[8] ={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

#define S1 0x0e

#define S2 0x0d

#define S3 0x0b

#define S4 0x07

sbit SPEAK=P3^5;

sbit P3_3=P3^3;

UINT8 mSecond,Second;

void Delay(UINT16 t)

{

UINT16 i,j;

for(i=0;i

for(j=0;j<114;j++);

}

UINT8 Scankey(void)

{

U INT8 key;

i f((P3&0x0f)==0x0f)

return(0xff);

D elay(10);

if((P3&0x0f)==0x0f)

return(0xff);

k ey=P3&0x0f;

w hile((P3&0x0f)!=0x0f);

r eturn(key);

}

void Display(void)

{

s tatic UINT8 num=0;

P2=0xff;

s witch(num)

{

case 0:

P0=0xff;

break;

case 1:

P0=0xff;

break;

case 2:

P0=0xff;

break;

case 3:

P0=0xff;

break;

case 4:

P0=0xff;

break;

case 5:

P0=SHU[Second%100];

break;

case 6:

P0=SEGMENT[mSecond%100/10];

break;

P0=SEGMENT[mSecond%10];

break;

}

P2=SELECT[num];

n um++;

num%=8;//if(num==8) num=0;

}

/*定时器初始化程序

void timer_10ms(void)//定时器T0定时10ms {

TMOD=0x01;

T H0=-10000/256;

T L0=-10000%256;

T R0=1;//启动T0

}*/

main()

{

s tatic UINT8 a=0;

UINT8 i,j;

U INT16 z;

mSecond = 0;

Second = 0;

TMOD=0x01;

T H0=0xdc;

T L0=0x32;

T R0=1;//启动T0

EA=0;

while(1)

{

Display();

Delay(2);

i = Scankey();

if(i==S1)//启动

{

EA=1;

}

else if(i == S2)//停止

{

EA=0;

}

else if(i == S3)//清除{

EA=0;

Second=0;

mSecond=0;

}

if(i == S4)//暂停

{

EA=0;

if(a==0)

{

a=1;

}

else if(a==1)

a=0;

EA=1;

}

}

if(Second==10)

{

while(1)

{

for(j=0;j<100;j++)

{

SPEAK=0;

Delay(1);

SPEAK=1;

Delay(1);

}

for(z=0;z<100;z++)

{

P2=0xfb;

P0=SHU[9];

Delay(5);

P2=0xfd;

P0=SEGMENT[9];

Delay(5);

P2=0xfe;

P0=SEGMENT[9];

Delay(5);

}

P2=0XFF;

for(j=0;j<100;j++)

{

SPEAK=1;

Delay(1);

SPEAK=1;

Delay(1);

}

//Delay(1000);

}

}

}

//}

}

void int_0() interrupt 1

{

T H0=0xdc;

T L0=0x32;

m Second++;

i f(mSecond==100)

{

Second++;

mSecond=0;

if(Second==10)

EA=0;

}

} 精品文档word文档可以编辑!谢谢下载!

相关文档
最新文档