開始時,第一個燈閃爍,時間間隔為1s。當按下按鍵后,八個燈閃爍,時間間隔為 2s;
這段程序為什么沒法實現上面的要求。。仿真圖在下面。
1.png (39.21 KB, 下載次數: 35)
下載附件
2022-11-22 22:52 上傳
單片機源程序如下:
#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;
}
}
|