单片机中断程序问题,新手,刚学中断,求教。
时间:10-02
整理:3721RD
点击:
程序如下,添加了中断后,按键按下有反应,可是数码管一直不动,为什么?
#include<reg51.h>
void clock();
void xianshi();
void delay(unsigned char i);
void key();
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit P32=P3^2;
unsigned char adjsec=0,num;
unsigned char sec=0,min=0,hour=0;
unsigned char leg[]={0,0,20,0,0,20,0,0};
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x70,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf};
void main()
{
TMOD=0X10;
TH1=0XD8;
TL1=0XF0;
TR1=1;
P2=0XFF;
P0=0X00;
EA=1;
ET1=1;
while(1)
{
clock();
xianshi();
delay(1);
//key();
}
}
void clock()
{
if(TF1==1)
{
TF1=0;
adjsec++;
TH1=0XD8;
TL1=0XF0;
}
if(adjsec==100)
{
adjsec=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour>23)
{
hour=0;
}
}
}
}
leg[0]=sec%10;
leg[1]=sec/10;
leg[3]=min%10;
leg[4]=min/10;
leg[6]=hour%10;
leg[7]=hour/10;
}
void xianshi()
{
static unsigned char led_shfit=0x00;
P2=0XFF;
P0=led[leg[led_shfit]];
switch(led_shfit)
{
case 0:P20=0;;break;
case 1:P21=0;;break;
case 2:P22=0;break;
case 3:P23=0;;break;
case 4:P24=0;break;
case 5:P25=0;;break;
case 6:P26=0;break;
case 7:P27=0;;break;
default:break;
}
led_shfit++;
if(led_shfit==0x08)
{
led_shfit=0x00;
}
}
void timer()interrupt 3
{
if(P32==0)
{
delay(10);
if(P32==0)
{
P1=0X00;
}
}
TH1=0XD8;
TL1=0XF0;
}
void delay(unsigned char i)
{
unsigned char k,j;
for(k=0;k<i;k++)
for(j=0;j<255;j++);
}
/*void key()
{
if(num==1)
{
P1=0XFE;
delay(255);
P1=0XFF;
}
} */
#include<reg51.h>
void clock();
void xianshi();
void delay(unsigned char i);
void key();
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit P32=P3^2;
unsigned char adjsec=0,num;
unsigned char sec=0,min=0,hour=0;
unsigned char leg[]={0,0,20,0,0,20,0,0};
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x70,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf};
void main()
{
TMOD=0X10;
TH1=0XD8;
TL1=0XF0;
TR1=1;
P2=0XFF;
P0=0X00;
EA=1;
ET1=1;
while(1)
{
clock();
xianshi();
delay(1);
//key();
}
}
void clock()
{
if(TF1==1)
{
TF1=0;
adjsec++;
TH1=0XD8;
TL1=0XF0;
}
if(adjsec==100)
{
adjsec=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour>23)
{
hour=0;
}
}
}
}
leg[0]=sec%10;
leg[1]=sec/10;
leg[3]=min%10;
leg[4]=min/10;
leg[6]=hour%10;
leg[7]=hour/10;
}
void xianshi()
{
static unsigned char led_shfit=0x00;
P2=0XFF;
P0=led[leg[led_shfit]];
switch(led_shfit)
{
case 0:P20=0;;break;
case 1:P21=0;;break;
case 2:P22=0;break;
case 3:P23=0;;break;
case 4:P24=0;break;
case 5:P25=0;;break;
case 6:P26=0;break;
case 7:P27=0;;break;
default:break;
}
led_shfit++;
if(led_shfit==0x08)
{
led_shfit=0x00;
}
}
void timer()interrupt 3
{
if(P32==0)
{
delay(10);
if(P32==0)
{
P1=0X00;
}
}
TH1=0XD8;
TL1=0XF0;
}
void delay(unsigned char i)
{
unsigned char k,j;
for(k=0;k<i;k++)
for(j=0;j<255;j++);
}
/*void key()
{
if(num==1)
{
P1=0XFE;
delay(255);
P1=0XFF;
}
} */