开始时,第一个灯闪烁,时间间隔为1s。当按下按键后,八个灯闪烁,时间间隔为 2s;
这段程序为什么没法实现上面的要求。。仿真图在下面。
单片机源程序如下:
#include <REGX51.H>
int count0=0,count1=0,num0=0,num1=0;
int temp=1;
void delay()
{
unsigned char i,j;
for(i=0;i<20;i++){
for(j=0;j<120;j++);
}
}
void inittimer0()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
EA=1;
ET0=1;
}
void button_scan(){
delay();
if(P1!=0) temp=0;
while(P1==0);
}
void main(){
inittimer0();
while(1){
button_scan();
if(temp==1){
P2_0=num0;
}
else{
P2=num1;
}
}
}
void timer0_isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count0++;
count1++;
if(count0==20&&temp){
num0=num0?0:1;
count0=0;
}
else if(count1==40&&!temp){
num1=~num1;
count1=0;
}
}
|