以下是源程序,具体疑问在switch语句中#include<STC15.H>
int n;
sbit key = P1^0 ; //按键1
void ex01_init();
void delay(int x); //x * 1ms
void delay_1(int x);
void main()
{
n = 0;
P0M0 = 0Xff;
P0M1 = 0X00; //P0为推挽模式
P0 = 0Xff; //初始状态 关
P1 = 0Xff;
ex01_init();
while(1)
{
switch(n)
{
case 0 : P07 = 1; P00 = ~P00; break; //关闭P07灯,P00灯闪烁
case 1 : P00 = 1; P01 = ~P01; break;
case 2 : P01 = 1; P02 = ~P02; break;
case 3 : P02 = 1; P03 = ~P03; break;
case 4 : P03 = 1; P04 = ~P04; break;
case 5 : P04 = 1; P05 = ~P05; break;
case 6 : P05 = 1; P06 = ~P06; break;
case 7 : P06 = 1; P07 = ~P07; break;
}
if(n == 8) //溢出归零
n = 0;
delay(250);
}
}
void ex01_init()
{
IT0 = 1; //外部中断为下降沿触发
EX0 = 1; //允许外部中断0中断
EA = 1; //开总中断
}
void delay(int x)
{
int i,j;
for(i = x; i > 0; i--) //1000
{
for(j = 1000; j > 0; j--); //循环1000次 1ms
if(key == 0)
{
delay_1(5); //去抖延时
if(key == 0)
n++;
while(key==0);
}
}
}
void delay_1(int x)
{
int i, j;
for(i = x; i > 0; i--) //1000
for(j = 1000; j > 0; j--);
}
void int0_isr() interrupt 0 //按键2触发外部中断1,led关闭
{
while(1)
{
P0 = 0Xff;
P1 = 0Xff;
if(key == 0)
{
delay_1(5); //去抖延时
if(key == 0)
{
break; //按键1触发跳出外部中断
}
while(key == 0);
}
}
}
一周热门 更多>