atmega16(AVR)单片机LED练习作业

作业要求:

程序代码:

#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;
*/
	 
	 }
}

原理图连接:

有问题请评论或者私信。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值