刚刚学过51单片机最重要的两个部分,定时器和中断,学完后应用定时器和数码管,首先,我们可以做出一个秒表程序,做出后,我们再在秒表的基础上,做一个数显时钟
#include
sbit k1=P3^1;
//定义四个按键
sbit k2=P3^0;
sbit k3=P3^2;
sbit k4=P3^3;
sbit LSA=P2^2;
//38译码器数码管位选
sbit LSB=P2^3;
sbit LSC=P2^4;
typedef unsigned char u8;
//对数据类型进行声明定义
typedef unsigned int u16;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
//不带小数点
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 code smgduan1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; //带小数点
u16 intnum;
//定义时间变量
/****************************延时函数**************************/
void delay(u16 t)
{
u8 x,y;
for(x=t;x>0;x--)
{
for(y=110;y>0;y--)
{
}
}
}
/**********************显示函数*******************************/
void display()
{
u16 wei[4];
wei[0]=intnum%10;
//对各个位取模
wei[1]=intnum/10%10;
wei[2]=intnum/100%10;
wei[3]=intnum/1000%10;
LSA=0; LSB=0; LSC=0; P0=smgduan[wei[0]];delay(10); P0=0x00;
LSA=1; LSB=0; LSC=0; P0=smgduan[wei[1]];delay(10); P0=0x00;
LSA=0; LSB=1; LSC=0; P0=smgduan1[wei[2]];delay(10); P0=0x00;
LSA=1; LSB=1; LSC=0; P0=smgduan[wei[3]];delay(10); P0=0x00;
}
/********************定时器0初始化函数************************/
void init_t0()
{
TMOD=0x01;
TH0=(65536-10000)/256;
//给TH0赋初值
TH1=(65526-10000)%256;
//给TH1赋初值
EA=1;
//开总中断
ET0=1;
//开定时器中断
TR0=0;
//关闭定时器0
}
/**********************按键函数******************************/
void keydown()
{
if(k1==0)
{
delay(10);
if(k1==0)
{
TR0=1;
//开启定时器
}
}
if(k2==0)
{
delay(10);
if(k2==0)
{
TR0=0;
//关闭定时器
}
}
if(k3==0)
{
delay(10);
if(k3==0)
{
TR0=0;
//关闭定时器
intnum=0;
//时间值清零
}
}
if(k4==0)
{
delay(10);
if(k4==0)
{
TR0=0;
//关闭定时器
intnum=0;
//时间值清零
}
}
}
/***************定时器0中断服务函数***************/
void int_t0() interrupt 1
{
TH0=(65536-10000)/256;
TH1=(65526-10000)%256;
intnum++;
}
/**************主函数****************************/
void main()
{
init_t0();
intnum=0;
while(1)
{
keydown();
display();
}
}
下面,我们在秒表的基础上,对代码进行改进,将它改成一个数显时钟
代码如下
#include
sbit k1=P3^1;
//定义四个按键
sbit k2=P3^0;
sbit k3=P3^2;
sbit k4=P3^3;
sbit LSA=P2^2;
//38译码器数码管位选
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P1^5;
typedef unsigned char u8;
//对数据类型进行声明定义
typedef unsigned int u16;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
//不带小数点
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 code smgduan1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; //带小数点
u16 intnum,second,minute,hour;
//定义时间变量
/****************************延时函数**************************/
void delay(u16 t)
{
u8 x,y;
for(x=t;x>0;x--)
{
for(y=110;y>0;y--)
{
}
}
}
/**********************显示函数*******************************/
void display()
{
LSA=0; LSB=0; LSC=0; P0=smgduan[second%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=0; P0=smgduan[second/10]; delay(10); P0=0x00;
LSA=0; LSB=1; LSC=0; P0=smgduan1[minute%10]; delay(10); P0=0x00;
LSA=1; LSB=1; LSC=0; P0=smgduan[minute/10]; delay(10); P0=0x00;
LSA=0; LSB=0; LSC=1; P0=smgduan1[hour%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=1; P0=smgduan[hour/10]; delay(10); P0=0x00;
}
/********************定时器0初始化函数************************/
void init_t0()
{
TMOD=0x01;
TH0=(65536-10000)/256;
//给TH0赋初值
TH1=(65526-10000)%256;
//给TH1赋初值
EA=1;
//开总中断
ET0=1;
//开定时器中断
TR0=1;
//开启定时器0
}
/**********************按键函数******************************/
void keydown()
{
if(k1==0)
{
delay(200);
if(k1==0)
{
second=0;
intnum=0;
}
}
if(k2==0)
{
delay(200);
if(k2==0)
{
hour++;
}
}
if(k3==0)
{
delay(200);
if(k3==0)
{
minute++;
}
}
if(k4==0)
{
delay(200);
if(k4==0)
{
minute--;
}
}
}
/***************定时器0中断服务函数***************/
void int_t0() interrupt 1
{
TH0=(65536-10000)/256;
TH1=(65526-10000)%256;
intnum++;
if(intnum>=100)
{
intnum=0;
second++;
}
if(second>=60)
{
second=0;
minute++;
}
if(minute>=60)
{
minute=0;
hour++;
}
if(hour>=24)
{
hour=0;
}
}
/**************主函数****************************/
void main()
{
init_t0();
intnum=minute=second=hour=0;
while(1)
{
keydown();
display();
}
}
#include
sbit k1=P3^1;
//定义四个按键
sbit k2=P3^0;
sbit k3=P3^2;
sbit k4=P3^3;
sbit LSA=P2^2;
//38译码器数码管位选
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P1^5;
typedef unsigned char u8;
//对数据类型进行声明定义
typedef unsigned int u16;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
//不带小数点
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 code smgduan1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; //带小数点
u16 intnum,second,minute,hour;
//定义时间变量
/****************************延时函数**************************/
void delay(u16 t)
{
u8 x,y;
for(x=t;x>0;x--)
{
for(y=110;y>0;y--)
{
}
}
}
/**********************显示函数*******************************/
void display()
{
LSA=0; LSB=0; LSC=0; P0=smgduan[second%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=0; P0=smgduan[second/10]; delay(10); P0=0x00;
LSA=0; LSB=1; LSC=0; P0=smgduan1[minute%10]; delay(10); P0=0x00;
LSA=1; LSB=1; LSC=0; P0=smgduan[minute/10]; delay(10); P0=0x00;
LSA=0; LSB=0; LSC=1; P0=smgduan1[hour%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=1; P0=smgduan[hour/10]; delay(10); P0=0x00;
}
/********************定时器0初始化函数************************/
void init_t0()
{
TMOD=0x01;
TH0=(65536-10000)/256;
//给TH0赋初值
TH1=(65526-10000)%256;
//给TH1赋初值
EA=1;
//开总中断
ET0=1;
//开定时器中断
TR0=1;
//开启定时器0
}
/**********************按键函数******************************/
void keydown()
{
if(k1==0)
{
delay(200);
if(k1==0)
{
second=0;
intnum=0;
}
}
if(k2==0)
{
delay(200);
if(k2==0)
{
hour++;
}
}
if(k3==0)
{
delay(200);
if(k3==0)
{
minute++;
}
}
if(k4==0)
{
delay(200);
if(k4==0)
{
minute--;
}
}
}
/***************定时器0中断服务函数***************/
void int_t0() interrupt 1
{
TH0=(65536-10000)/256;
TH1=(65526-10000)%256;
intnum++;
if(intnum>=100)
{
intnum=0;
second++;
}
if(second>=60)
{
second=0;
minute++;
}
if(minute>=60)
{
minute=0;
hour++;
}
if(hour>=24)
{
hour=0;
}
}
/**************主函数****************************/
void main()
{
init_t0();
intnum=minute=second=hour=0;
while(1)
{
keydown();
display();
}
}
#include
sbit k1=P3^1;
//定义四个按键
sbit k2=P3^0;
sbit k3=P3^2;
sbit k4=P3^3;
sbit LSA=P2^2;
//38译码器数码管位选
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P1^5;
typedef unsigned char u8;
//对数据类型进行声明定义
typedef unsigned int u16;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
//不带小数点
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 code smgduan1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; //带小数点
u16 intnum,second,minute,hour;
//定义时间变量
/****************************延时函数**************************/
void delay(u16 t)
{
u8 x,y;
for(x=t;x>0;x--)
{
for(y=110;y>0;y--)
{
}
}
}
/**********************显示函数*******************************/
void display()
{
LSA=0; LSB=0; LSC=0; P0=smgduan[second%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=0; P0=smgduan[second/10]; delay(10); P0=0x00;
LSA=0; LSB=1; LSC=0; P0=smgduan1[minute%10]; delay(10); P0=0x00;
LSA=1; LSB=1; LSC=0; P0=smgduan[minute/10]; delay(10); P0=0x00;
LSA=0; LSB=0; LSC=1; P0=smgduan1[hour%10]; delay(10); P0=0x00;
LSA=1; LSB=0; LSC=1; P0=smgduan[hour/10]; delay(10); P0=0x00;
}
/********************定时器0初始化函数************************/
void init_t0()
{
TMOD=0x01;
TH0=(65536-10000)/256;
//给TH0赋初值
TH1=(65526-10000)%256;
//给TH1赋初值
EA=1;
//开总中断
ET0=1;
//开定时器中断
TR0=1;
//开启定时器0
}
/**********************按键函数******************************/
void keydown()
{
if(k1==0)
{
delay(200);
if(k1==0)
{
second=0;
intnum=0;
}
}
if(k2==0)
{
delay(200);
if(k2==0)
{
hour++;
}
}
if(k3==0)
{
delay(200);
if(k3==0)
{
minute++;
}
}
if(k4==0)
{
delay(200);
if(k4==0)
{
minute--;
}
}
}
/***************定时器0中断服务函数***************/
void int_t0() interrupt 1
{
TH0=(65536-10000)/256;
TH1=(65526-10000)%256;
intnum++;
if(intnum>=100)
{
intnum=0;
second++;
}
if(second>=60)
{
second=0;
minute++;
}
if(minute>=60)
{
minute=0;
hour++;
}
if(hour>=24)
{
hour=0;
}
}
/**************主函数****************************/
void main()
{
init_t0();
intnum=minute=second=hour=0;
while(1)
{
keydown();
display();
}
}