基於51微控制器的秒錶和數顯時鐘
阿新 • • 發佈:2019-01-05
#include<reg52.h>
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<reg52.h>
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<reg52.h>
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();
}
}
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<reg52.h>
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<reg52.h>
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();
}
}