资源描述
#include "reg51.h"
#include<intrins.h>
#define uint unsigned char
#define uchar unsigned char
#define THCO 0xee
#define TLCO 0x0
uchar i,n;
uint H,L;
void delay(uint);
uchar code table[]={0xfd,0x09,0x04,0xfd,0x09,0x04,
0xfd,0x34,0x04,0xfd,0x83,0x04,
0xfd,0x83,0x04,0xfd,0x34,0x04,
0xfd,0x09,0x04,0xfc,0xad,0x04,
0xfc,0x44,0x04,0xfc,0x44,0x04,
0xfc,0xad,0x04,0xfd,0x09,0x04,
0xfd,0x09,0x04,0xfc,0xad,0x04,
0xfc,0xad,0x08,
0xfd,0x09,0x04,0xfd,0x09,0x04,
0xfd,0x34,0x04,0xfd,0x83,0x04,
0xfd,0x83,0x04,0xfd,0x34,0x04,
0xfd,0x09,0x04,0xfc,0xad,0x04,
0xfc,0x44,0x04,0xfc,0x44,0x04,
0xfc,0xad,0x04,0xfd,0x09,0x04,
0xfc,0xad,0x04,0xfc,0x44,0x04,
0xfc,0x44,0x08};
unsigned char code Duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//共阴极数码管,0-9段码表
unsigned char Data_Buffer[10]={0,1,2,3,4,5,6,7,8,9};//四个数码管显示数值,数组变量定义
static unsigned char Bit=0; //静态变量,退出程序后,值保留
static unsigned char Bit1=0;
static unsigned char Bit2=0;
static unsigned char Bit3=0;
static unsigned char Bit4=0;
static unsigned char Bit5=0;
static unsigned char Bit6=0;
static unsigned char Trouch=0;
sbit P10=P1^0; //四个数码管的位码口定义
sbit P27=P2^7;
sbit P26=P2^6;
sbit P25=P2^5;
sbit P24=P2^4;
sbit P23=P2^3;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P3_2=P3^2;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4; //四个数码管的位码口定义
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
void delay(int i)
{
unsigned int a,b;
for(a=i;a>0;a--);
for(b=100;b>0;b--);
}
void change6()
{ P26=0;P25=0;
delay(150000) ;
do{
if(P21==0)
Bit6++;
if(Bit6==2)Bit6=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50);
} while(P20==1);P26=1;P25=1;
delay(25000);
}
void change5()
{ P24=0; P26=0;
delay(150000) ;
do{
if(P21==0)
Bit5++;
if(Bit5==4)Bit5=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50);
} while(P20==1);P24=1; P26=1;
delay(25000);
change6();
}
void change4()
{ P24=0;
delay(150000) ;
do{
if(P21==0)
Bit4++;
if(Bit4==6)Bit4=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50);
} while(P20==1); P24=1;
delay(25000);
change5();
}
void change3()
{ P25=0;P26=0;
delay(150000) ;
do{
if(P21==0)
Bit3++;
if(Bit3==10)Bit3=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50);
} while(P20==1);P25=1;P26=1;
;delay(25000);
change4();
}
void change2()
{ P25=0;
delay(150000) ;
do{
if(P21==0)
Bit2++;
if(Bit2==6)Bit2=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50); P2=0xdf;
} while(P20==1);P25=1;
delay(25000);
change3();
}
void change1()
{ P26=0;
delay(150000) ;
do{
if(P21==0)
Bit1++;
if(Bit1==10)Bit1=0;
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf; delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;delay(50);
} while(P20==1);P26=1;
delay(25000);
change2();
}
void main()
{
PX0=1;
EX0=1; //沿触发方式
IT0=1;
TMOD=0x01; //定时器0初始化
TH0=THCO;
TL0=TLCO;
TR0=1;
ET0=1;
EA=1;
TMOD=0x10;
EA=1;//开总中断
ET1=1;//开T1中断
for(i=0;i<30;i++) //一共30个音符
{
H=table[i*3]; //取计数初值高字节
L=table[i*3+1]; //取计数初值低字节
TH1=H;
TL1=L;
TR1=1;
n=table[i*3+2]; //取节拍数
do
{
delay(50);
}
while(n--);
}
TR1=0;
while(1) ;
}
void timer0() interrupt 1
{
TH0=THCO;
TL0=TLCO;
Bit++;
if(Bit>=100)
{
Bit=0;
Bit1++;
if(Bit1>=9)
{Bit1=0;Bit2++;}
if(Bit2>=6)
{Bit2=0;Bit3++;}
if(Bit3>=9)
{Bit3=0;Bit4++;}
if(Bit4>=6)
{ Bit4=0;Bit5++;}
if(Bit5>=4)
{Bit5=0;Bit6++;}
if(Bit6>=2)
{Bit6=0;} } //先关位码
P1=0xff; //先关位码
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf;delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe; delay(50);
}
void time1() interrupt 3
{
TH1=H;
TL1=L;
P3_2=~P3_2;
}
void int0() interrupt 0
{
do
{P27=!P27; delay(50);
P1=0xff; //先关位码
P0=Duan[Data_Buffer[Bit1]]; //开段码
P1=0xdf;delay(50);
P0=Duan[Data_Buffer[Bit2]];
P1=0xef; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit3]]; //开段码
P1=0xf7;delay(50);
P0=Duan[Data_Buffer[Bit4]];
P1=0xfb; delay(50); //先关位码
P0=Duan[Data_Buffer[Bit5]]; //开段码
P1=0xfd;delay(50);
P0=Duan[Data_Buffer[Bit6]];
P1=0xfe;
} while(P20==1);
P27=1;
change1();
}
展开阅读全文