实验内容:
实验内容为3项,其中第1、2项必做。
1、基本的流水灯。
根据图1电路,编写一段程序,使8个发光二极管D1、D2、D3、D4、D5、D6、D7、D8顺序(正序)点亮:先点亮D1,再点亮D2、D3……D8、D1……,循环点亮。每点亮一个LED,采用软件延时一段时间。
2、简单键控的流水灯。
不按键,按正序点亮流水灯;按下K1不松手,按倒序点亮流水灯,即先点亮D8,再顺序点亮D7、D6……D1、D8……。松手后,又按正序点亮流水灯。
3、键控的流水灯。
上电,不点亮LED,按一下K1键,按正序点亮流水灯。按一下K2键,按倒序点亮流水灯,按一下K3键,全部关闭LED。
实验原理图:
实验代码
1、基本的流水灯
实现思路:循环输入数值并延时
#include "reg51.h"
#define uchar unsigned char
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
void delay(unsigned int ms){
unsigned int x,y;
for(x=0;x<ms;x++)
for(y=0;y<110;y++);
}
//8421
void k1yes()
{
P0=0xfe;//1111 1110
delay(1000);
P0=0xfc;//1111 1100
delay(1000);
P0=0xf8;//1111 1000
delay(1000);
P0=0xf0;//1111 0000
delay(1000);
P0=0xe0;//1110 0000
delay(1000);
P0=0xc0;//1100 0000
delay(1000);
P0=0xe80;//1000 0000
delay(1000);
P0=0x00;//0000 0000
delay(1000);
}
void k2yes()
{
P0=0x00;//0000 0000
delay(500);
P0=0xe80;//1000 0000
delay(500);
P0=0xc0;//1100 0000
delay(1000);
P0=0xe0;//1110 0000
delay(1000);
P0=0xf0;//1111 0000
delay(1000);
P0=0xf8;//1111 1000
delay(1000);
P0=0xfc;//1111 1100
delay(1000);
P0=0xfe;//1111 1110
delay(1000);
}
void main(void)
{
while(1)
{
//if(k1==0&&k2!=0&&k3!=0&&k4!=0)
// {k2yes();}
if(k1==1)
{k1yes();}
else{k2yes();}
}
}
实现思路:移位
//更好的版本二
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(unsigned int c);
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit D0=P0^0;
sbit D1=P0^1;
sbit D2=P0^2;
sbit D3=P0^3;
sbit D4=P0^4;
sbit D5=P0^5;
sbit D6=P0^6;
sbit D7=P0^7;
void delay(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=50;b>0;b--)
{
for (a=100;a>0;a--);
}
}
}
void main()
{
unsigned char LED;
LED = 0xfe;
while (1)
{
P0 = LED; //P0=11111110
delay(50);
LED = LED << 1;
if (P0 == 0x00)
{
LED = 0xfe;
}
}
}
2、简单键控的流水灯。
实现思路:由于是按下键不松手,键值一直保留,所以判断if(K1==0),即是按下K1,
采用如例题所示的if(K1==0)或if(K1==1),后接while(1)循环的模式。
//连续点亮版本
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(unsigned int c);
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit D0=P0^0;
sbit D1=P0^1;
sbit D2=P0^2;
sbit D3=P0^3;
sbit D4=P0^4;
sbit D5=P0^5;
sbit D6=P0^6;
sbit D7=P0^7;
void delay(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=50;b>0;b--)
{
for (a=100;a>0;a--);
}
}
}
void main()
{
unsigned char LED;
if(K1==0)
{
LED = 0xfe;
while (1)
{
P0 = LED; //P0=11111110
delay (50);
LED = LED << 1;
if (P0 == 0x00)
{
LED = 0xfe;
}
if(K1==1) {
break;
}
}
}
if(K1==1)
{
LED = 0x7F; //P0=011111111
while (1)
{
P0 = LED;
delay (50);
LED = LED >> 1;
if (P0 == 0x00)
{
LED = 0x7F;
}
if(K1==0)
{
break;
}
}
}
}
//依次点亮版
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
void delay(uint i){
uchar t;
while(i--){
for(t=0;t<120;t++);
}
}
void main(){
data uchar a;
a=1;
while(P2==0xff){
P0=~a;
a=a<<1;
if(a==0)
a=1;
delay(250);
}
while(P2==0xfe)
{ P0=~a;
a=a>>1;
if(a==0)
a=128;
delay(250);
}
}
3、键控的流水灯
实现思路:
由于是按一下键后马上松手,所以要设置一个变量K保留按键键值,由于
单片机本身原理图所致,K17、K18、K19、K20四个键已经固定为K17=P2^0、
K18=P21、K19=P22、K20=P2^3所以直接运用,利用如if(K1==0)的条件判
断语句来判断是否按下K1、K2、K3按键,实现键控流水灯。
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(unsigned int c);
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit D0=P0^0;
sbit D1=P0^1;
sbit D2=P0^2;
sbit D3=P0^3;
sbit D4=P0^4;
sbit D5=P0^5;
sbit D6=P0^6;
sbit D7=P0^7;
void delay(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=50;b>0;b--)
{
for (a=100;a>0;a--);
}
}
}
void main()
{
unsigned char LED;
unsigned char i;
LED = 0xff;
if(K1==0)
{
while (1)
{
LED = 0xfe;
delay(20);
for (i = 0 ;i < 9 ; i++)
{
P0 = LED;
delay (20);
LED = LED << 1;
//LED = LED | 0x01; //控制是一连串还是一个个
if(K2==0) break;
if(K3==0) break;
}
if(K2==0) break;
if(K3==0) break;
}
}
if(K2==0)
{
while(1)
{
LED = 0xff;
delay(20);
for (i = 0 ;i < 9 ; i++)
{
P0 = LED;
delay (20);
LED = LED >> 1;
//LED = LED | 0x80;
if(K1==0) break;
if(K3==0) break;
}
if(K1==0) break;
if(K3==0) break;
}
}
if(K3==0)
P0=0x00;
if(K3==1)
P0=0xff;
}