搜索
您的当前位置:首页正文

实验三 流水灯控制实验

来源:筏尚旅游网


实验三

功能:使用查询和中断方式编写程序,每按动一次K1键,演示不同流水效果*

一、 中断式

/*功能:使用查询方式编写程序,每按动一次K1键,演示不同流水效果*

KEY=1:全亮

KEY=2:单管点亮;

KEY=3:依次点亮;

KEY=4依次熄灭;

KEY=5:整体闪烁;

KEY=6:双管点亮;*/

#include

#define uchar unsigned char

uchar key=0;

void delay(char c);

void INT_1() ;

void xiaoguo1() ;

void xiaoguo2() ;

void xiaoguo3() ;

void xiaoguo4() ;

void xiaoguo5() ;

void xiaoguo6() ;

void main()

//主函数

{

EA=1;

EX1=1;

IT1=1;

P1=0x00;

while(1)

{

switch(key)

{

case 1:xiaoguo1();break;

case 2:xiaoguo2();break;

case 3:xiaoguo3();break;

case 4:xiaoguo4();break;

case 5:xiaoguo5();break;

case 6:xiaoguo6();

}

}

}

/************************************延时函数 ***********************************/

void delay(char c) //延时函数

{

unsigned int i,j;

for(;c>0;c--)

{

for(j=142;j>0;j--)

{

for(i=2;i>0;i--);

}

}

}

/************************************中断函数***********************************/

void INT_1() interrupt 2 //中断函数

{

EX1=0;

delay(20);

EX1=1;

key++;

if(key>6)key=1;

}

/************************************效果1***********************************/

void xiaoguo1() //效果全亮

{

P1=0xff;

delay(300);

}

/************************************效果2***********************************/

void xiaoguo2()//效果先全灭然后单管点亮

{

uchar a;

uchar code table1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};

P1=0x00;

for(a=0;a<8;a++)

{

P1=table1[a];

delay(300);

}

}

/************************************效果3 ***********************************/

void xiaoguo3() //效果先全灭,然后依次点亮

{

uchar b;

uchar code table2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

P1=0x00;

for(b=0;b<8;b++)

{

P1=table2[b];

delay(300);

}

}

/************************************效果4 ***********************************/

void xiaoguo4() //效果全亮,依次熄灭

{

uchar c;

uchar code table3[]={0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01};

P1=0xff;

for(c=0;c<8;c++)

{

P1=table3[c];

delay(300);

}

}

/************************************效果5***********************************/

void xiaoguo5() //效果 全灭,整体闪烁

{

uchar d;

uchar code table4[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};

P1=0x00;

for(d=0;d<8;d++)

{

P1=table4[d];

delay(300);

}

}

/************************************效果 6***********************************/

void xiaoguo6() //效果.自定义双管循环

{ uchar e;

uchar code table5[]={0xcc,0x33,0xcc,0x33,0xcc,0x33,0xcc,0x33};

P1=0x00;

for(e=0;e<8;e++)

{

P1=table5[e];

delay(300);

}

}

查询式

/*功能:使用查询方式编写程序,每按动一次K1键,演示不同流水效果*

KEY=1:全亮

KEY=2:单管点亮;

KEY=3:依次点亮;

KEY=4依次熄灭;

KEY=5:整体闪烁;

KEY=6:双管点亮;*/

#include

#define uchar unsigned char

sbit sw=P3^3;

uchar key=0;

void delay(char c);//函数声明

void keyscan();

void xiaoguo1() ;

void xiaoguo2() ;

void xiaoguo3() ;

void xiaoguo4() ;

void xiaoguo5() ;

void xiaoguo6() ;

/*************************************主函数***********************************/

void mian() //主函数

{

EA=1;

EX1=1;

IT1=1;//设置为下降沿触发

P1=0x00;

while(1)

{

keyscan();//键盘检测

switch(key)

{

case 1:xiaoguo1();break;

case 2:xiaoguo2();break;

case 3:xiaoguo3();break;

case 4:xiaoguo4();break;

case 5:xiaoguo5();break;

case 6:xiaoguo6();break;

}

}

}

/************************************************************************/

void keyscan() //键盘检测程序

{

if(sw==0)

{

key++; //按键防抖

if(key>6) key=1;

键盘检测程序

}

while(sw==0); //等待按键按下,防止一次按键多次处理

}

/************************************延时函数***********************************/

void delay(char c) //延时函数

{

unsigned int i,j;

for(;c>0;c--)

{

for(j=142;j>0;j--)

{

for(i=2;i>0;i--);

}

}

}

/************************************效果1***********************************/

void xiaoguo1() //效果全亮

{

P1=0xff;

delay(300);

}

/************************************效果2***********************************/

void xiaoguo2()//效果先全灭然后单管点亮

{

uchar a;

uchar code table1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};

P1=0x00;

for(a=0;a<8;a++)

{

P1=table1[a];

delay(300);

}

}

/************************************效果3 ***********************************/

void xiaoguo3() //效果先全灭,然后依次点亮

{

uchar b;

uchar code table2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

P1=0x00;

for(b=0;b<8;b++)

{

P1=table2[b];

delay(300);

}

}

/************************************效果 4 ***********************************/

void xiaoguo4() //效果全亮,依次熄灭

{

uchar c;

uchar code table3[]={0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01};

P1=0xff;

for(c=0;c<8;c++)

{

P1=table3[c];

delay(300);

}

}

/************************************效果5***********************************/

void xiaoguo5() //效果 全灭,整体闪烁

{

uchar d;

uchar code table4[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};

P1=0x00;

for(d=0;d<8;d++)

{

P1=table4[d];

delay(300);

}

}

/************************************效果6 ***********************************/

void xiaoguo6() //效果.自定义双管循环

{ uchar e;

uchar code table5[]={0xcc,0x33,0xcc,0x33,0xcc,0x33,0xcc,0x33};

P1=0x00;

for(e=0;e<8;e++)

{

P1=table5[e];

delay(300);

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top