楼主犯了定时器中断的几乎所有的大忌:
1.一个定时器能解决的问题不用两个。
2.中断是打一枪留个标记就跑,绝不停留,任务放在主循环运行。
3.中断中绝不可以使用超过中断周期的延时和循环。
给你把程序修改了一下,花样流水灯自己补充扩展。
- #include <AT89X51.H>
- //#include<stc.h>
- #define unc unsigned char
- bit ldelay=0;
- bit lldelay=0;
- sbit key1=P1^3;
- sbit key2=P1^4;
- unc t=0,j=0,k;
- unc i=0;
- unsigned int count=0;
- unsigned char time[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};/*0,1,2,3,4,5,6,7,8,9*/
- unsigned char led0[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- unsigned char led1[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
- unsigned char led2[]={0x81,0x42,0x24,0x18,0x81,0x42,0x24,0x18};
- unsigned char led3[]={0x18,0x24,0x42,0x81,0x18,0x24,0x42,0x81};
- /*
- void delay(unsigned int time)
- {
- unsigned char hs=0;
- for(;time>0;time--)
- for(;hs<120;hs++);
- }*/
- void Timer0Init(void)//5毫秒@12.000MHz
- {
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x78; //设置定时初值
- TH0 = 0xEC; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- EA=1;
- ET0=1;
- }
- void main()
- {
- Timer0Init();
- while(1)
- {
- if(t%2==0)
- {
- key1=0;
- P0=time[count/10];
- key2=1;
- }
- else
- {
- key2=0;
- P0=time[count%10];
- key1=1;
- }
- if(ldelay==1)
- {
- ldelay=0;
- k++;
- if(k>=2)
- {
- k=0;
- count++;
- if(count==16)
- {
- count=0;
- j++;
- if(j>=4)
- j=0;
- }
- }
- if(j==0)
- P2=led0[i];
- if(j==1)
- P2=led1[i];
- if(j==2)
- P2=led2[i];
- if(j==3)
- P2=led3[i];
- i++;
- if(i>=8)
- i=0;
- }
- }
- }
- time_fuck() interrupt 1
- {
- TL0 = 0x78; //设置定时初值
- TH0 = 0xEC; //设置定时初值
- t++;
- if(t>=100) //0.5s
- {
- t=0;
- ldelay=1;
- }
- }
- /*
- time_Q() interrupt 3
- {
- // TR1=0;
- j++;
- while(1)
- {
- j++;
- i++;
- P2=led[i];
- delay(500);
- if(i==10)
- i=0;
- if(j==250)
- {
- j=0;
- break;
- }
- }
- TH1=0X3D;
- TL1=0Xb1;
- // TR1=1;
- }*/
复制代码
|