单片机51c语言程序实例(可调控的跑马灯)+Proteus仿真+源代码

可调控的跑马灯

   本例实现目标:利用51单片机控制16只LED灯按8种不同的模式滚动,模式选择由K1控制,加速按键由K2控制,减速按键由K3控制。

  本例电路图如下:

单片机51c语言程序实例(可调控的跑马灯)+Proteus仿真+源代码_第1张图片

                         电路图-1

单片机51c语言程序实例(可调控的跑马灯)+Proteus仿真+源代码_第2张图片

电路图-2

在Proteus中的元器件:

单片机51c语言程序实例(可调控的跑马灯)+Proteus仿真+源代码_第3张图片


源代码:

 //Ãû³Æ£º¿ÉÒÔµ÷¿ØµÄ×ßÂíµÆ
 //˵Ã÷£ºµ¥»÷ģʽ°´¼ü¿ÉÔÚ0-7ºÅģʽÖÐÑ¡Ôñ£¬¼ÓËٺͼõËÙ°´¼ü¿Éµ÷ÕûLEDÁ÷¶¯
 
 #include
 #define uchar unsigned char
 #define uint unsigned int
uchar ModeNo;  //ģʽ±àºÅ
   uchar Speed;
   uchar tCount = 0;  
   uchar rdx;  //ËÙ¶ÈÈ¡ÖµË÷Òý
   uchar mb_Count =0; //Òƶ¯Î»Êý
   bit Direct= 1; //¹ö¶¯·½Ïò
 
 //¶ÎÂë±í
 uchar code DSY_CODE[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
 
 //µ÷ËÙ±í
 uint code sTable[]={0,1,3,5,7,9,15,30,50,100,200,230,280,300,350};
 
 //ÑÓʱ
 void Delay(uint x)
 {
   uchar i;
while(x--)
{
  for(i=0;i<120;i++);
}
   
 }
 //¼ì²â°´¼ü
 uchar Getkey()
 {
  uchar K;
if(P2==0xFF)return 0;
Delay(10);
switch(P2)
{
case 0xFE:K=1;break;
case 0xFD:K=2;break;
case 0xFB:K=3;break;
default : K=0;
}
  while(P2!=0xFF);    //µÈ´ýÊÍ·Å°´¼ü
return K;
 }


 //16Ö»LEDÏÔʾ
 void Led_Demo(uint Led16)
 {
  P1=(uchar)(Led16 & 0x00FF); //ÏÔʾµÍ8λ
P0=(uchar)(Led16 >>8);//ÏÔʾ¸ß8λ
 }
 
 //¶¨Ê±Æ÷2
 void T0_INT() interrupt 1
 {
if(++tCount < Speed) return;
tCount = 0;
switch (ModeNo)
{
case 0:Led_Demo(~0x0001< case 1:Led_Demo(~0xB000>>mb_Count);break;
case 2:if(Direct)Led_Demo(~0x000F< else Led_Demo(~0xF000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 3:if(Direct)Led_Demo(-(0x000F< else Led_Demo(-(0xF000>>mb_Count));
if(mb_Count==15)Direct=!Direct;
break;
  case 4:if(Direct)Led_Demo(0x003F< else Led_Demo(0xFC00>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 5:if(Direct)Led_Demo(0x0001< else Led_Demo(0x8000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 6:if(Direct)Led_Demo(-(0x0001< else Led_Demo(-(0x8000>>mb_Count));
if(mb_Count==15) Direct=!Direct;
break;
case 7:if(Direct)Led_Demo(0xFFFE< else Led_Demo(0x7FFF>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
}
  mb_Count=(mb_Count+1)%16;
 }
 
 //¼üÅÌ°´¼ü´¦Àí
 void KeyProcess(uchar Key)
 {
   switch(Key)
{
     case 1: //ÖØÉèģʽ
Direct=1;mb_Count=0;
ModeNo=(ModeNo+1)%8;
P3=DSY_CODE[ModeNo];
break;
case 2://¼ÓËÙ
if(rdx>1)Speed=sTable[--rdx];break;
case 3://¼õËÙ
if(rdx<15)Speed=sTable[++rdx];break;
}
 
 }
 
 //Ö÷³ÌÐò
 void main()
 {
  uchar Key;
P0=P1=P2=P3=0xFF;
ModeNo=0;rdx=4;
Speed=sTable[rdx];
P3=DSY_CODE[ModeNo];
IE=0xB2;
TMOD=0X00;
TR0=1;
while(1)
{
  Key=Getkey();
if(Key!=0) KeyProcess(Key);
}
 }

你可能感兴趣的:(51单片机实例,可调控的跑马灯)