作业要求:
程序代码:
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define interrupt_handler exter1:3
void delay(uint x)
{
uint tt;
for(tt=0;tt<x;tt++);
for(tt=0;tt<x;tt++);
//for(tt=0;tt<x;tt++);
}
uchar moshi1[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};四个花样数组,可自行更改
uchar moshi2[8]={0xfc,0xf3,0xcf,0x3f,0xfc,0xf3,0xcf,0x3f};
uchar moshi3[8]={0xfa,0xf5,0xeb,0xd7,0xaf,0x5f,0xbe,0x7d};
uchar moshi4[8]={0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55};
uchar kind=0;///流水灯种类变量
uchar k=0;
uchar i=0;///延时函数变量
void main(void)
{
DDRB=0xff;B口输出
PORTB=0xff;
DDRA=0xff;//
PORTA=0x00;
DDRD=0x00;///输入
PORTD=0xff;//上拉输入
k=8;
kind=1;
while(1)
{
for(i=0;i<k;i++)
{
if(PIND==0xfe)
{
kind=1;
}
else if(PIND==0xfd)
{
kind=2;
}
else if(PIND==0xfb)
{
kind=3;
}
else if(PIND==0xf7)
{
kind=4;
}
else
{
switch(kind)
{
case 1:PORTB &=moshi1[i];
delay(250);///改变数字大小可以改变延时时间,从而控制流水灯速度
PORTB |=0xff;
break;
case 2:PORTB &=moshi2[i];
delay(250);
PORTB |=0xff;
break;
case 3:PORTB &=moshi3[i];
delay(250);
PORTB |=0xff;
break;
case 4:PORTB &=moshi4[i];
delay(250);
PORTB |=0xff;
break;
}
}
}
/* PORTB &=0xfe;
delay(100);
PORTB &=0xfd;
delay(100);
PORTB &=0x00;
delay(100);
PORTB |=0xff;
*/
}
}
原理图连接:
有问题请评论或者私信。