之前没用中断的时候有误差,想减少误差用了中断,不知道哪里不对,误差更大了。谢谢!
#include <REG51.H>
unsigned char Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit P37=P3^3;
sbit P36=P3^4;
sbit P35=P3^5;
sbit P34=P3^7;
sbit beep=P3^2;
char zhongduan_cnt;
void init()
{
TMOD=0x01; //定时器初始化 选择timer0做16位定时器
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //开放中断
ET0=1; //开放timer0中断
TR0=1; //启动timer0
}
void timer0(void) interrupt 1 using 1
{
TH0=(65536-50000)/256+TH0;
TL0=(65536-50000)%256+TL0;
zhongduan_cnt++;
}
void Delay_beep(char x)
{ char t; while(x--) for(t=0;t<120;t++); } //按周期t发音
void Play(char t)
{ char i; for(i=0;i<100;i++) { beep=~beep; Delay_beep(t); } beep=0; }
//10ms延时子程序(12MHZ)
void delay_ms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=4;j>0;j--)
for(k=248;k>0;k--);
}
void display(unsigned int fen_gao,fen_di,miao_gao,miao_di)
{
P1 = 0X00;
P37=1;
P1=Tab[fen_gao];
delay_ms() ;
P37=0;
P1 = 0X00;
P36=1;
P1=Tab[fen_di];
delay_ms() ;
P36=0;
P1 = 0X00;
P35=1;
P1=Tab[miao_gao];
delay_ms() ;
P35=0;
P1 = 0X00;
P34=1;
P1=Tab[miao_di];
delay_ms() ;
P34=0;
}
/*********主程序********************/
void main() {
unsigned int fen_gao; //分十位
unsigned int fen_di; //分个位
unsigned int miao_gao; //秒十位
unsigned int miao_di; // 秒个位
beep=0;
miao_di=0;
miao_gao=0;
fen_di=0;
fen_gao=0;
P35=0;
P37=0;
P34=0;
P36=0;
init();
while(1)
{
if (miao_di==0)
if (miao_gao==0)
if (fen_di==0)
if (fen_gao==5)
{
while(1)
{
Play(1);
}
}
if(zhongduan_cnt==20) //如果到了1S
{
zhongduan_cnt=0; //清零
miao_di++; //秒数累加
}
if (miao_di>9)
{
miao_gao=miao_gao+1;
miao_di=0;}
if (miao_gao>5)
{
fen_di=fen_di+1;
miao_gao=0;
}
if (fen_di>9)
{
fen_gao=fen_gao+1;
fen_di=0;}
display(fen_gao,fen_di,miao_gao,miao_di);
}
}
|