51单片机秒表

#include

#define uchar unsigned char
#define uint unsigned int

uint count,count_ms=10;
// 0 1 2 3 4 5 6 7 8 9 10
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit BY1 = P3^2;
sbit BY2 = P3^3;
sbit BY3 = P3^4;

/*
* 延时函数 毫秒级
*/
void delay(uint z)
{
uint x,y;
for(x=z;z>0;z--)
for(y=110;y>0;y--);
}
/********************************************************************
* 名称 : Timer1Init()
* 功能 : 定时器1初始化
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Timer1Init(void) //10毫秒@11.0592MHz
{
// AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0xDC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; //enable timer1 interrupt
}

/********************************************************************
* 名称 : display()
* 功能 : 数码管显示函数
* 输入 : Dis_data1,Dis_data2,Dis_data3,Dis_data4
* 输出 : 无
***********************************************************************/
void display(uint Dis_data1,uint Dis_data2,uint Dis_data3,uint Dis_data4)
{
P0=table[Dis_data1];
P2_0 = 0;
delay(2);
P2_0 = 1;

P0=table1[Dis_data2];
P2_1 = 0;
delay(2);
P2_1 = 1;

P0=table[Dis_data3];
P2_2 = 0;
delay(2);
P2_2 = 1;

P0=table[Dis_data4];
P2_3 = 0;
delay(2);
P2_3 = 1;

}

/********************************************************************
* 名称 : main()
* 功能 : 主函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void main()
{
Timer1Init();
count=0;
count_ms=10;
while(1)
{
if(BY1==0) //判断是否按下键盘
{
delay(60); //延时,软件去干扰
if(BY1==0) //确认按键按下
{
EA = 1;
}
}
while(BY1==0);//按键锁定.
if(BY2==0) //判断是否按下键盘
{
delay(60); //延时,软件去干扰
if(BY2==0) //确认按键按下
{
TR1 = ~TR1;
}
}
while(BY2==0);//按键锁定.
if(BY3==0) //判断是否按下键盘
{
delay(60); //延时,软件去干扰
if(BY3==0) //确认按键按下
{
count = 0;
}
}
while(BY3==0);//按键锁定.
display(count/1000,count%1000/100,count%1000%100/10,count%1000%100%10);

}
}

/********************************************************************
* 名称 : void tm1_isr() interrupt 3 using 1
* 功能 : 定时器1中断服务函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void tm1_

isr() interrupt 3 using 1
{
TL1 = 0x00; //设置定时初值
TH1 = 0xDC; //设置定时初值
if (count_ms-- == 0) //1ms * 1000 -> 1s
{
count_ms=10;
if(count==0)
{
count=0;
}
count ++; //reset counter
}
}

相关文档
最新文档